Add default keys to local pickup settings to prevent PHP warning (#46182)

This commit is contained in:
Thomas Roberts 2024-04-05 12:08:54 +01:00 committed by GitHub
parent d874575c86
commit 66ac06e9fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 10 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Prevent PHP warning if local pickup has not been set up in your store

View File

@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\BlockTypes;
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
/**
* Cart class.
@ -246,8 +247,8 @@ class Cart extends AbstractBlock {
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );
$this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones(), true );
$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true );
$pickup_location_settings = LocalPickupUtils::get_local_pickup_settings();
$this->asset_data_registry->add( 'localPickupEnabled', $pickup_location_settings['enabled'], true );
// Hydrate the following data depending on admin or frontend context.
if ( ! is_admin() && ! WC()->is_rest_api_request() ) {

View File

@ -267,7 +267,7 @@ class Checkout extends AbstractBlock {
if ( ( ! empty( $post->post_type ) && ! empty( $post->post_name ) && 'page-checkout' !== $post->post_name && 'wp_template' === $post->post_type ) || false === has_block( 'woocommerce/checkout', $post ) ) {
return;
}
$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', array() );
$pickup_location_settings = LocalPickupUtils::get_local_pickup_settings();
if ( ! isset( $pickup_location_settings['title'] ) ) {
return;
@ -350,9 +350,9 @@ class Checkout extends AbstractBlock {
$this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );
$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true );
$this->asset_data_registry->add( 'localPickupText', wc_clean( $pickup_location_settings['title'] ) ?? __( 'Local Pickup', 'woocommerce' ), true );
$pickup_location_settings = LocalPickupUtils::get_local_pickup_settings();
$this->asset_data_registry->add( 'localPickupEnabled', $pickup_location_settings['enabled'], true );
$this->asset_data_registry->add( 'localPickupText', $pickup_location_settings['title'], true );
$is_block_editor = $this->is_block_editor();

View File

@ -293,7 +293,7 @@ class ShippingController {
}
$settings = array(
'pickupLocationSettings' => get_option( 'woocommerce_pickup_location_settings', array() ),
'pickupLocationSettings' => LocalPickupUtils::get_local_pickup_settings(),
'pickupLocations' => $formatted_pickup_locations,
'readonlySettings' => array(
'hasLegacyPickup' => $has_legacy_pickup,

View File

@ -7,14 +7,41 @@ namespace Automattic\WooCommerce\StoreApi\Utilities;
*/
class LocalPickupUtils {
/**
* Gets the local pickup location settings.
*/
public static function get_local_pickup_settings() {
$pickup_location_settings = get_option(
'woocommerce_pickup_location_settings',
[
'enabled' => 'no',
'title' => __( 'Local Pickup', 'woocommerce' ),
]
);
if ( empty( $pickup_location_settings['title'] ) ) {
$pickup_location_settings['title'] = __( 'Local Pickup', 'woocommerce' );
}
if ( empty( $pickup_location_settings['enabled'] ) ) {
$pickup_location_settings['enabled'] = 'no';
}
// All consumers of this turn it into a bool eventually. Doing it here removes the need for that.
$pickup_location_settings['enabled'] = wc_string_to_bool( $pickup_location_settings['enabled'] );
$pickup_location_settings['title'] = wc_clean( $pickup_location_settings['title'] );
return $pickup_location_settings;
}
/**
* Checks if WC Blocks local pickup is enabled.
*
* @return bool True if local pickup is enabled.
*/
public static function is_local_pickup_enabled() {
$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
return wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' );
$pickup_location_settings = self::get_local_pickup_settings();
return $pickup_location_settings['enabled'];
}
/**
* Gets a list of payment method ids that support the 'local-pickup' feature.
@ -24,7 +51,7 @@ class LocalPickupUtils {
public static function get_local_pickup_method_ids() {
$all_methods_supporting_local_pickup = array_reduce(
WC()->shipping()->get_shipping_methods(),
function( $methods, $method ) {
function ( $methods, $method ) {
if ( $method->supports( 'local-pickup' ) ) {
$methods[] = $method->id;
}