/** * Create a single Shipping Zone. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Request|WP_Error */ public function create_item($request) { $zone = new WC_Shipping_Zone(null); if (!is_null($request->get_param('name'))) { $zone->set_zone_name($request->get_param('name')); } if (!is_null($request->get_param('order'))) { $zone->set_zone_order($request->get_param('order')); } $zone->create(); if ($zone->get_id() !== 0) { $request->set_param('id', $zone->get_id()); $response = $this->get_item($request); $response->set_status(201); $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $zone->get_id()))); return $response; } else { return new WP_Error('woocommerce_rest_shipping_zone_not_created', __("Resource cannot be created. Check to make sure 'order' and 'name' are present.", 'woocommerce'), array('status' => 500)); } }
/** * Test legacy zone functions. * * @since 2.7.0 * * @expectedDeprecated WC_Shipping_Zone::read * @expectedDeprecated WC_Shipping_Zone::create * @expectedDeprecated WC_Shipping_Zone::update */ public function test_wc_shipping_zone_legacy() { // Create a single zone. $zone = new WC_Shipping_Zone(); $zone->set_zone_name('Local'); $zone->set_zone_order(1); $zone->add_location('GB', 'country'); $zone->add_location('CB*', 'postcode'); $zone->save(); $zone_id = $zone->get_id(); $zone_read = new WC_Shipping_Zone(); $zone_read->read($zone_id); $this->assertEquals($zone_id, $zone_read->get_id()); $zone = new WC_Shipping_Zone(); $zone->set_zone_name('Test'); $zone->set_zone_order(2); $zone->create(); $this->assertEquals('Test', $zone->get_zone_name()); $this->assertNotEmpty($zone->get_id()); $zone->set_zone_name('Test 2'); $zone->update(); $this->assertEquals('Test 2', $zone->get_zone_name()); }