From 08620cf8466054263d6975fb749c6b454d9dee34 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 27 Jul 2020 21:49:33 -0300 Subject: [PATCH 1/3] Make "woocommerce_shipping_cost_requires_address" strict --- includes/class-wc-cart.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 3b086137acd..eb546b925ee 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -1396,10 +1396,8 @@ class WC_Cart extends WC_Legacy_Cart { } if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) { - if ( ! $this->get_customer()->has_calculated_shipping() ) { - if ( ! $this->get_customer()->get_shipping_country() || ( ! $this->get_customer()->get_shipping_state() && ! $this->get_customer()->get_shipping_postcode() ) ) { - return false; - } + if ( ! $this->get_customer()->get_shipping_country() || ! $this->get_customer()->get_shipping_state() || ! $this->get_customer()->get_shipping_postcode() ) { + return false; } } From 0f091406df23bf7e7ba97e35b29e7b5f3a7961fa Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 27 Jul 2020 21:52:33 -0300 Subject: [PATCH 2/3] Test WC_Cart::show_shipping() --- tests/php/includes/class-wc-cart-test.php | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/php/includes/class-wc-cart-test.php diff --git a/tests/php/includes/class-wc-cart-test.php b/tests/php/includes/class-wc-cart-test.php new file mode 100644 index 00000000000..081b6aa3c54 --- /dev/null +++ b/tests/php/includes/class-wc-cart-test.php @@ -0,0 +1,59 @@ +cart->empty_cart(); + WC()->customer->set_is_vat_exempt( false ); + WC()->session->set( 'wc_notices', null ); + } + + /** + * Test show shipping. + */ + public function test_show_shipping() { + // Test with an empty cart. + $this->assertFalse( WC()->cart->show_shipping() ); + + // Add a product to the cart. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + + // Test with "woocommerce_ship_to_countries" disabled. + $default_ship_to_countries = get_option( 'woocommerce_ship_to_countries', '' ); + update_option( 'woocommerce_ship_to_countries', 'disabled' ); + $this->assertFalse( WC()->cart->show_shipping() ); + + // Test with default "woocommerce_ship_to_countries" and "woocommerce_shipping_cost_requires_address". + update_option( 'woocommerce_ship_to_countries', $default_ship_to_countries ); + $this->assertTrue( WC()->cart->show_shipping() ); + + // Test with "woocommerce_shipping_cost_requires_address" enabled. + $default_shipping_cost_requires_address = get_option( 'woocommerce_shipping_cost_requires_address', 'no' ); + update_option( 'woocommerce_shipping_cost_requires_address', 'yes' ); + $this->assertFalse( WC()->cart->show_shipping() ); + + // Set address for shipping calculation required for "woocommerce_shipping_cost_requires_address". + WC()->cart->get_customer()->set_shipping_country( 'US' ); + WC()->cart->get_customer()->set_shipping_state( 'NY' ); + WC()->cart->get_customer()->set_shipping_postcode( '12345' ); + $this->assertTrue( WC()->cart->show_shipping() ); + + // Reset. + update_option( 'woocommerce_shipping_cost_requires_address', $default_shipping_cost_requires_address ); + $product->delete( true ); + } +} From de383971eb5b91fa433ecb6df54f7bb3cfbafe30 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 27 Jul 2020 22:07:46 -0300 Subject: [PATCH 3/3] Reset all after test --- tests/php/includes/class-wc-cart-test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/php/includes/class-wc-cart-test.php b/tests/php/includes/class-wc-cart-test.php index 081b6aa3c54..466de397fa6 100644 --- a/tests/php/includes/class-wc-cart-test.php +++ b/tests/php/includes/class-wc-cart-test.php @@ -55,5 +55,8 @@ class WC_Cart_Test extends \WC_Unit_Test_Case { // Reset. update_option( 'woocommerce_shipping_cost_requires_address', $default_shipping_cost_requires_address ); $product->delete( true ); + WC()->cart->get_customer()->set_shipping_country( 'GB' ); + WC()->cart->get_customer()->set_shipping_state( '' ); + WC()->cart->get_customer()->set_shipping_postcode( '' ); } }