Made packages with incomplete destinations unshippable

This commit is contained in:
Christopher Allford 2019-11-26 16:44:49 -08:00 committed by vedanshujain
parent 93e3e5bfe1
commit e800b40ad6
3 changed files with 46 additions and 24 deletions

View File

@ -277,20 +277,33 @@ class WC_Shipping {
/** /**
* See if package is shippable. * See if package is shippable.
* *
* Packages are shippable until proven otherwise e.g. after getting a shipping country. * Packages must have a valid destination to be shipped.
* *
* @param array $package Package of cart items. * @param array $package Package of cart items.
* @return bool * @return bool
*/ */
protected function is_package_shippable( $package ) { public function is_package_shippable( $package ) {
// Packages are shippable until proven otherwise.
if ( empty( $package['destination']['country'] ) ) { if ( empty( $package['destination']['country'] ) ) {
return true; return false;
}
$country = $package['destination']['country'];
$countries = array_keys( WC()->countries->get_shipping_countries() );
if ( ! in_array( $country, $countries, true ) ) {
return false;
} }
$allowed = array_keys( WC()->countries->get_shipping_countries() ); $states = array_keys( WC()->countries->get_states( $country ) );
return in_array( $package['destination']['country'], $allowed, true ); if ( is_array( $states ) && ! in_array( $package['destination']['state'], $states, true ) ) {
return false;
}
$postcode = wc_format_postcode( $package['destination']['postcode'], $country );
if ( ! WC_Validation::is_postcode( $postcode, $country ) ) {
return false;
}
return true;
} }
/** /**

View File

@ -1,12 +1,13 @@
<?php <?php
/** /**
* Tests for the WC_Shopping_Zones class. * Tests for the WC_Shopping_Zone class.
* *
* @package WooCommerce\Tests\Shipping_Zone * @package WooCommerce\Tests\Shipping
*/ */
/** /**
* Class Shipping_Zone. * Class Shipping_Zone.
* @package WooCommerce\Tests\Shipping
*/ */
class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case { class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case {

View File

@ -1,11 +1,19 @@
<?php <?php
/**
* Tests for the WC_Shopping_Zones class.
*
* @package WooCommerce\Tests\Shipping
*/
/** /**
* Class Shipping_Zones. * Class Shipping_Zones.
* @package WooCommerce\Tests\Shipping_Zones * @package WooCommerce\Tests\Shipping
*/ */
class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case { class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
/**
* Set up tests.
*/
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -16,10 +24,10 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::get_zones * Test: WC_Shipping_Zones::get_zones
*/ */
public function test_get_zones() { public function test_get_zones() {
// Test // Test.
$zones = WC_Shipping_Zones::get_zones(); $zones = WC_Shipping_Zones::get_zones();
// Assert // Assert.
$this->assertTrue( is_array( $zones ) ); $this->assertTrue( is_array( $zones ) );
$this->assertTrue( 4 === count( $zones ) ); $this->assertTrue( 4 === count( $zones ) );
} }
@ -28,10 +36,10 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::get_zone * Test: WC_Shipping_Zones::get_zone
*/ */
public function test_get_zone() { public function test_get_zone() {
// Test // Test.
$zone = WC_Shipping_Zones::get_zone( 1 ); $zone = WC_Shipping_Zones::get_zone( 1 );
// Assert that the first zone is our local zone // Assert that the first zone is our local zone.
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone ); $this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
$this->assertEquals( $zone->get_zone_name(), 'Local' ); $this->assertEquals( $zone->get_zone_name(), 'Local' );
} }
@ -40,19 +48,19 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::get_zone_by * Test: WC_Shipping_Zones::get_zone_by
*/ */
public function test_get_zone_by() { public function test_get_zone_by() {
// Test // Test.
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 2 ); $zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 2 );
// Assert // Assert.
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone ); $this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
$this->assertEquals( $zone->get_zone_name(), 'Europe' ); $this->assertEquals( $zone->get_zone_name(), 'Europe' );
// Test instance_id // Test instance_id.
$instance_id = $zone->add_shipping_method( 'flat_rate' ); $instance_id = $zone->add_shipping_method( 'flat_rate' );
$zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id ); $zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
// Assert // Assert.
$this->assertInstanceOf( 'WC_Shipping_Zone', $zone ); $this->assertInstanceOf( 'WC_Shipping_Zone', $zone );
$this->assertEquals( $zone->get_zone_name(), 'Europe' ); $this->assertEquals( $zone->get_zone_name(), 'Europe' );
} }
@ -61,12 +69,12 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::get_shipping_method * Test: WC_Shipping_Zones::get_shipping_method
*/ */
public function test_get_shipping_method() { public function test_get_shipping_method() {
// Test // Test.
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 1 ); $zone = WC_Shipping_Zones::get_zone_by( 'zone_id', 1 );
$instance_id = $zone->add_shipping_method( 'flat_rate' ); $instance_id = $zone->add_shipping_method( 'flat_rate' );
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id ); $shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
// Assert // Assert.
$this->assertInstanceOf( 'WC_Shipping_Flat_Rate', $shipping_method ); $this->assertInstanceOf( 'WC_Shipping_Flat_Rate', $shipping_method );
} }
@ -74,11 +82,11 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::delete_zone * Test: WC_Shipping_Zones::delete_zone
*/ */
public function test_delete_zone() { public function test_delete_zone() {
// Test // Test.
WC_Shipping_Zones::delete_zone( 1 ); WC_Shipping_Zones::delete_zone( 1 );
$zones = WC_Shipping_Zones::get_zones(); $zones = WC_Shipping_Zones::get_zones();
// Assert // Assert.
$this->assertTrue( 3 === count( $zones ) ); $this->assertTrue( 3 === count( $zones ) );
} }
@ -86,7 +94,7 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
* Test: WC_Shipping_Zones::get_zone_matching_package * Test: WC_Shipping_Zones::get_zone_matching_package
*/ */
public function test_get_zone_matching_package() { public function test_get_zone_matching_package() {
// Test // Test.
$zone1 = WC_Shipping_Zones::get_zone_matching_package( $zone1 = WC_Shipping_Zones::get_zone_matching_package(
array( array(
'destination' => array( 'destination' => array(
@ -124,7 +132,7 @@ class WC_Tests_Shipping_Zones extends WC_Unit_Test_Case {
) )
); );
// Assert // Assert.
$this->assertEquals( 'Local', $zone1->get_zone_name() ); $this->assertEquals( 'Local', $zone1->get_zone_name() );
$this->assertEquals( 'Europe', $zone2->get_zone_name() ); $this->assertEquals( 'Europe', $zone2->get_zone_name() );
$this->assertEquals( 'California', $zone3->get_zone_name() ); $this->assertEquals( 'California', $zone3->get_zone_name() );