get_zone_by() public static method

Get shipping zone by an ID.
Since: 2.6.0
public static get_zone_by ( string $by = 'zone_id', integer $id ) : WC_Shipping_Zone | boolean
$by string zone_id or instance_id
$id integer
return WC_Shipping_Zone | boolean
 /**
  * Retrieve a Shipping Zone by it's ID.
  *
  * @param int $zone_id Shipping Zone ID.
  * @return WC_Shipping_Zone|WP_Error
  */
 protected function get_zone($zone_id)
 {
     $zone = WC_Shipping_Zones::get_zone_by('zone_id', $zone_id);
     if (false === $zone) {
         return new WP_Error('woocommerce_rest_shipping_zone_invalid', __("Resource doesn't exist.", 'woocommerce'), array('status' => 404));
     }
     return $zone;
 }
 /**
  * Get all Shipping Zones.
  *
  * @param WP_REST_Request $request
  * @return WP_REST_Response
  */
 public function get_items($request)
 {
     $rest_of_the_world = WC_Shipping_Zones::get_zone_by('zone_id', 0);
     $zones = WC_Shipping_Zones::get_zones();
     array_unshift($zones, $rest_of_the_world->get_data());
     $data = array();
     foreach ($zones as $zone_obj) {
         $zone = $this->prepare_item_for_response($zone_obj, $request);
         $zone = $this->prepare_response_for_collection($zone);
         $data[] = $zone;
     }
     return rest_ensure_response($data);
 }
Ejemplo n.º 3
0
 /**
  * Test: WC_Shipping_Zones::get_shipping_method
  */
 public function test_get_shipping_method()
 {
     // Setup
     WC_Helper_Shipping_Zones::create_mock_zones();
     // Test
     $zone = WC_Shipping_Zones::get_zone_by('zone_id', 1);
     $instance_id = $zone->add_shipping_method('flat_rate');
     $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id);
     // Assert
     $this->assertInstanceOf('WC_Shipping_Flat_Rate', $shipping_method);
     // Clean
     WC_Helper_Shipping_Zones::remove_mock_zones();
 }
 /**
  * Show instance settings
  * @param  int $instance_id
  */
 protected function instance_settings_screen($instance_id)
 {
     $zone = WC_Shipping_Zones::get_zone_by('instance_id', $instance_id);
     $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id);
     if (!$shipping_method) {
         wp_die(__('Invalid shipping method!', 'woocommerce'));
     }
     if (!$zone) {
         wp_die(__('Zone does not exist!', 'woocommerce'));
     }
     if (!$shipping_method->has_settings()) {
         wp_die(__('This shipping method does not have any settings to configure.', 'woocommerce'));
     }
     if (!empty($_POST['save'])) {
         if (empty($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'woocommerce-settings')) {
             echo '<div class="updated error"><p>' . __('Edit failed. Please try again.', 'woocommerce') . '</p></div>';
         }
         $shipping_method->process_admin_options();
         $shipping_method->display_errors();
     }
     include_once dirname(__FILE__) . '/views/html-admin-page-shipping-zones-instance.php';
 }
Ejemplo n.º 5
0
 /**
  * Save method settings
  */
 public static function shipping_zone_methods_save_settings()
 {
     if (!isset($_POST['wc_shipping_zones_nonce'], $_POST['instance_id'], $_POST['data'])) {
         wp_send_json_error('missing_fields');
         exit;
     }
     if (!wp_verify_nonce($_POST['wc_shipping_zones_nonce'], 'wc_shipping_zones_nonce')) {
         wp_send_json_error('bad_nonce');
         exit;
     }
     if (!current_user_can('manage_woocommerce')) {
         wp_send_json_error('missing_capabilities');
         exit;
     }
     $instance_id = absint($_POST['instance_id']);
     $zone = WC_Shipping_Zones::get_zone_by('instance_id', $instance_id);
     $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id);
     $shipping_method->set_post_data($_POST['data']);
     $shipping_method->process_admin_options();
     wp_send_json_success(array('methods' => $zone->get_shipping_methods(), 'errors' => $shipping_method->get_errors()));
 }