Cart & Checkout: conditionally register country and state settings (https://github.com/woocommerce/woocommerce-blocks/pull/1782)

* Conditionally register wcSettings in the Cart and Checkout blocks

* Add tests
This commit is contained in:
Albert Juhé Lluveras 2020-02-20 16:16:32 +01:00 committed by GitHub
parent 806cd76987
commit 9debaf8daf
4 changed files with 34 additions and 6 deletions

View File

@ -165,6 +165,16 @@ class AssetDataRegistry {
return $this->data;
}
/**
* Allows checking whether a key exists.
*
* @param string $key The key to check if exists.
* @return bool Whether the key exists in the current data registry.
*/
public function exists( $key ) {
return array_key_exists( $key, $this->data );
}
/**
* Interface for adding data to the registry.
*

View File

@ -49,8 +49,12 @@ class Cart extends AbstractBlock {
$data_registry = Package::container()->get(
\Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class
);
$data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() );
$data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() );
if ( ! $data_registry->exists( 'shippingCountries' ) ) {
$data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() );
}
if ( ! $data_registry->exists( 'shippingStates' ) ) {
$data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() );
}
\Automattic\WooCommerce\Blocks\Assets::register_block_script(
$this->block_name . '-frontend',
$this->block_name . '-block-frontend'

View File

@ -50,10 +50,18 @@ class Checkout extends AbstractBlock {
$data_registry = Package::container()->get(
\Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class
);
$data_registry->add( 'allowedCountries', WC()->countries->get_allowed_countries() );
$data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() );
$data_registry->add( 'allowedStates', WC()->countries->get_allowed_country_states() );
$data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() );
if ( ! $data_registry->exists( 'allowedCountries' ) ) {
$data_registry->add( 'allowedCountries', WC()->countries->get_allowed_countries() );
}
if ( ! $data_registry->exists( 'shippingCountries' ) ) {
$data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() );
}
if ( ! $data_registry->exists( 'allowedStates' ) ) {
$data_registry->add( 'allowedStates', WC()->countries->get_allowed_country_states() );
}
if ( ! $data_registry->exists( 'shippingStates' ) ) {
$data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() );
}
\Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' );
return $content;
}

View File

@ -31,6 +31,12 @@ class AssetDataRegistry extends WP_UnitTestCase {
$this->assertEquals( [ 'test' => 'foo' ], $this->registry->get() );
}
public function test_data_exists() {
$this->registry->add( 'foo', 'lorem-ipsum' );
$this->assertEquals( true, $this->registry->exists( 'foo' ) );
$this->assertEquals( false, $this->registry->exists( 'bar' ) );
}
public function test_add_lazy_data() {
$lazy = function () {
return 'bar';