Fix: get_checkout_fields() should return empty array if requested fieldset is not defined (#51508)

* If there is no fieldset, return an empty array instead of throwing notice

* changelog
This commit is contained in:
Mike Jolley 2024-09-23 17:02:21 +01:00 committed by GitHub
parent 3ac409e03f
commit 2c816457a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 10 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
get_checkout_fields() should return empty array if requested fieldset is not defined

View File

@ -220,16 +220,9 @@ class WC_Checkout {
}
/**
* Get an array of checkout fields.
*
* @param string $fieldset to get.
* @return array
* Initialize the checkout fields.
*/
public function get_checkout_fields( $fieldset = '' ) {
if ( ! is_null( $this->fields ) ) {
return $fieldset ? $this->fields[ $fieldset ] : $this->fields;
}
protected function initialize_checkout_fields() {
// Fields are based on billing/shipping country. Grab those values but ensure they are valid for the store before using.
$billing_country = $this->get_value( 'billing_country' );
$billing_country = empty( $billing_country ) ? WC()->countries->get_base_country() : $billing_country;
@ -311,8 +304,25 @@ class WC_Checkout {
}
}
}
}
return $fieldset ? $this->fields[ $fieldset ] : $this->fields;
/**
* Get an array of checkout fields.
*
* @param string $fieldset to get.
* @return array
*/
public function get_checkout_fields( $fieldset = '' ) {
if ( is_null( $this->fields ) ) {
$this->initialize_checkout_fields();
}
// If a fieldset is specified, return only the fields for that fieldset, or array if the field set does not exist.
if ( $fieldset ) {
return $this->fields[ $fieldset ] ?? array();
}
return $this->fields;
}
/**