Make sure we error out on read if a zone is not found.
This commit is contained in:
parent
6f51fce88b
commit
23a325ca8d
|
@ -56,17 +56,22 @@ class WC_Shipping_Zones {
|
|||
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
|
||||
switch ( $by ) {
|
||||
case 'zone_id' :
|
||||
return new WC_Shipping_Zone( $id );
|
||||
$zone_id = $id;
|
||||
break;
|
||||
case 'instance_id' :
|
||||
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
||||
$zone_id = $data_store->get_zone_id_by_instance_id( $id );
|
||||
if ( false !== $zone_id ) {
|
||||
return new WC_Shipping_Zone( $zone_id );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if ( false !== $zone_id ) {
|
||||
try {
|
||||
return new WC_Shipping_Zone( $zone_id );
|
||||
} catch ( Exception $e ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,6 +74,8 @@ class WC_Shipping_Zone_Data_Store_Table implements WC_Shipping_Zone_Data_Store_I
|
|||
$zone->read_meta_data();
|
||||
$zone->set_object_read( true );
|
||||
do_action( 'woocommerce_shipping_zone_loaded', $zone );
|
||||
} else {
|
||||
throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -286,7 +286,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/1' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/555555' );
|
||||
$request->set_body_params( array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
|
@ -332,7 +332,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue