get_zone() public static method

Get shipping zone using it's ID
Since: 2.6.0
public static get_zone ( integer $zone_id ) : WC_Shipping_Zone | boolean
$zone_id integer
return WC_Shipping_Zone | boolean
 /**
  * Save shipping and tax options.
  */
 public function wc_setup_shipping_taxes_save()
 {
     check_admin_referer('wc-setup');
     $enable_shipping = isset($_POST['woocommerce_calc_shipping']);
     $enable_taxes = isset($_POST['woocommerce_calc_taxes']);
     if ($enable_shipping) {
         update_option('woocommerce_ship_to_countries', '');
     } else {
         update_option('woocommerce_ship_to_countries', 'disabled');
     }
     update_option('woocommerce_calc_taxes', $enable_taxes ? 'yes' : 'no');
     update_option('woocommerce_prices_include_tax', sanitize_text_field($_POST['woocommerce_prices_include_tax']));
     if ($enable_shipping && !empty($_POST['shipping_cost_domestic'])) {
         // Create a domestic shipping zone
         $zone = new WC_Shipping_Zone($zone_data['zone_id']);
         $zone->set_zone_name(__('Domestic', 'woocommerce'));
         $zone->set_zone_order(1);
         $zone->add_location(WC()->countries->get_base_country(), 'country');
         $zone->save();
         // Add a flat rate shipping method to this domestic zone
         $instance_id = $zone->add_shipping_method('flat_rate');
         $shipping_method = new WC_Shipping_Flat_Rate($instance_id);
         $option_key = $shipping_method->get_instance_option_key();
         // Update rate settings
         $costs = array();
         $costs[] = wc_format_decimal(sanitize_text_field($_POST['shipping_cost_domestic']));
         if ($item_cost = sanitize_text_field($_POST['shipping_cost_domestic_item'])) {
             $costs[] = $item_cost . ' * [qty]';
         }
         $shipping_method->instance_settings['cost'] = implode(' + ', array_filter($costs));
         $shipping_method->instance_settings['enabled'] = 'yes';
         $shipping_method->instance_settings['type'] = 'order';
         update_option($option_key, $shipping_method->instance_settings);
     }
     if ($enable_shipping && !empty($_POST['shipping_cost_worldwide'])) {
         // Add a flat rate shipping method to the worldwide zone
         $zone = WC_Shipping_Zones::get_zone(0);
         $instance_id = $zone->add_shipping_method('flat_rate');
         $shipping_method = new WC_Shipping_Flat_Rate($instance_id);
         $option_key = $shipping_method->get_instance_option_key();
         // Update rate settings
         $costs = array();
         $costs[] = wc_format_decimal(sanitize_text_field($_POST['shipping_cost_worldwide']));
         if ($item_cost = sanitize_text_field($_POST['shipping_cost_worldwide_item'])) {
             $costs[] = $item_cost . ' * [qty]';
         }
         $shipping_method->instance_settings['cost'] = implode(' + ', array_filter($costs));
         $shipping_method->instance_settings['enabled'] = 'yes';
         $shipping_method->instance_settings['type'] = 'order';
         update_option($option_key, $shipping_method->instance_settings);
     }
     if ($enable_taxes && !empty($_POST['woocommerce_import_tax_rates'])) {
         $locale_info = (include WC()->plugin_path() . '/i18n/locale-info.php');
         $tax_rates = array();
         $country = WC()->countries->get_base_country();
         $state = WC()->countries->get_base_state();
         if (isset($locale_info[$country])) {
             if (isset($locale_info[$country]['tax_rates'][$state])) {
                 $tax_rates = $locale_info[$country]['tax_rates'][$state];
             } elseif (isset($locale_info[$country]['tax_rates'][''])) {
                 $tax_rates = $locale_info[$country]['tax_rates'][''];
             }
             if (isset($locale_info[$country]['tax_rates']['*'])) {
                 $tax_rates = array_merge($locale_info[$country]['tax_rates']['*'], $tax_rates);
             }
         }
         if ($tax_rates) {
             $loop = 0;
             foreach ($tax_rates as $rate) {
                 $tax_rate = array('tax_rate_country' => $rate['country'], 'tax_rate_state' => $rate['state'], 'tax_rate' => $rate['rate'], 'tax_rate_name' => $rate['name'], 'tax_rate_priority' => isset($rate['priority']) ? absint($rate['priority']) : 1, 'tax_rate_compound' => 0, 'tax_rate_shipping' => $rate['shipping'] ? 1 : 0, 'tax_rate_order' => $loop++, 'tax_rate_class' => '');
                 WC_Tax::_insert_tax_rate($tax_rate);
             }
         }
     }
     wp_redirect(esc_url_raw($this->get_next_step_link()));
     exit;
 }
Esempio n. 2
0
 /**
  * Test: WC_Shipping_Zones::get_zone
  */
 public function test_get_zone()
 {
     // Setup
     WC_Helper_Shipping_Zones::create_mock_zones();
     // Test
     $zone = WC_Shipping_Zones::get_zone(1);
     // Assert that the first zone is our local zone
     $this->assertInstanceOf('WC_Shipping_Zone', $zone);
     $this->assertEquals($zone->get_zone_name(), 'Local');
     // Clean
     WC_Helper_Shipping_Zones::remove_mock_zones();
 }
 /**
  * Show method for a zone
  * @param  int $zone_id
  */
 protected function zone_methods_screen($zone_id)
 {
     $wc_shipping = WC_Shipping::instance();
     $zone = WC_Shipping_Zones::get_zone($zone_id);
     $shipping_methods = $wc_shipping->get_shipping_methods();
     if (!$zone) {
         wp_die(__('Zone does not exist!', 'woocommerce'));
     }
     wp_localize_script('wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array('methods' => $zone->get_shipping_methods(), 'zone_id' => $zone->get_zone_id(), 'wc_shipping_zones_nonce' => wp_create_nonce('wc_shipping_zones_nonce'), 'strings' => array('unload_confirmation_msg' => __('Your changed data will be lost if you leave this page without saving.', 'woocommerce'), 'save_changes_prompt' => __('Do you wish to save your changes first? Your changed data will be discarded if you choose to cancel.', 'woocommerce'), 'save_failed' => __('Your changes were not saved. Please retry.', 'woocommerce'), 'add_method_failed' => __('Shipping method could not be added. Please retry.', 'woocommerce'), 'yes' => __('Yes', 'woocommerce'), 'no' => __('No', 'woocommerce'))));
     wp_enqueue_script('wc-shipping-zone-methods');
     include_once dirname(__FILE__) . '/views/html-admin-page-shipping-zone-methods.php';
 }
Esempio n. 4
0
 /**
  * Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.
  */
 public static function shipping_zone_add_method()
 {
     if (!isset($_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['method_id'])) {
         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;
     }
     // Check User Caps
     if (!current_user_can('manage_woocommerce')) {
         wp_send_json_error('missing_capabilities');
         exit;
     }
     $zone_id = absint($_POST['zone_id']);
     $zone = WC_Shipping_Zones::get_zone($zone_id);
     $instance_id = $zone->add_shipping_method(wc_clean($_POST['method_id']));
     wp_send_json_success(array('instance_id' => $instance_id, 'zone_id' => $zone_id, 'methods' => $zone->get_shipping_methods()));
 }
Esempio n. 5
0
 /**
  * Test: WC_Shipping_Zone::add_shipping_method
  */
 public function test_add_shipping_method()
 {
     // Setup
     WC_Helper_Shipping_Zones::create_mock_zones();
     // Test
     $zone = WC_Shipping_Zones::get_zone(1);
     $zone->add_shipping_method('flat_rate');
     $zone->add_shipping_method('free_shipping');
     // Assert
     $methods = $zone->get_shipping_methods();
     // Assert
     $this->assertTrue(2 === sizeof($methods));
     $this->assertInstanceOf('WC_Shipping_Method', current($methods));
     // Clean
     WC_Helper_Shipping_Zones::remove_mock_zones();
 }
 /**
  * Show method for a zone
  * @param  int $zone_id
  */
 protected function zone_methods_screen($zone_id)
 {
     if ('new' === $zone_id) {
         $zone = new WC_Shipping_Zone();
         $zone->set_zone_name('');
     } else {
         $zone = WC_Shipping_Zones::get_zone(absint($zone_id));
     }
     if (!$zone) {
         wp_die(__('Zone does not exist!', 'woocommerce'));
     }
     $allowed_countries = WC()->countries->get_allowed_countries();
     $wc_shipping = WC_Shipping::instance();
     $shipping_methods = $wc_shipping->get_shipping_methods();
     $continents = WC()->countries->get_continents();
     // Prepare locations
     $locations = array();
     $postcodes = array();
     foreach ($zone->get_zone_locations() as $location) {
         if ('postcode' === $location->type) {
             $postcodes[] = $location->code;
         } else {
             $locations[] = $location->type . ':' . $location->code;
         }
     }
     wp_localize_script('wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array('methods' => $zone->get_shipping_methods(), 'zone_name' => $zone->get_zone_name(), 'zone_id' => $zone->get_id(), 'wc_shipping_zones_nonce' => wp_create_nonce('wc_shipping_zones_nonce'), 'strings' => array('unload_confirmation_msg' => __('Your changed data will be lost if you leave this page without saving.', 'woocommerce'), 'save_changes_prompt' => __('Do you wish to save your changes first? Your changed data will be discarded if you choose to cancel.', 'woocommerce'), 'save_failed' => __('Your changes were not saved. Please retry.', 'woocommerce'), 'add_method_failed' => __('Shipping method could not be added. Please retry.', 'woocommerce'), 'yes' => __('Yes', 'woocommerce'), 'no' => __('No', 'woocommerce'))));
     wp_enqueue_script('wc-shipping-zone-methods');
     include_once dirname(__FILE__) . '/views/html-admin-page-shipping-zone-methods.php';
 }
    /**
     * Create WC Shipping Estimate settings table
     * thanks WC core - modeled off "Shipping Methods" table
     *
     * @since 1.0.0
     */
    public function add_settings()
    {
        // Get the estimates if they're saved already
        $method_estimate_from = get_option('wc_shipping_method_estimate_from', array());
        $method_estimate_to = get_option('wc_shipping_method_estimate_to', array());
        ?>
		<tr valign="top">
			<th scope="row" class="titledesc"><?php 
        esc_html_e('Estimate Ranges', 'woocommerce-shipping-estimate');
        ?>
</th>
			<td class="forminp">
				<table class="wc_shipping widefat wp-list-table" cellspacing="0">
				<?php 
        $zones = WC_Shipping_Zones::get_zones();
        ?>
				<?php 
        if (!empty($zones)) {
            ?>
				<?php 
            foreach ($zones as $zone_id => $zone_data) {
                ?>
					<?php 
                $zone = WC_Shipping_Zones::get_zone($zone_id);
                $zone_methods = $zone->get_shipping_methods();
                if (!empty($zone_methods)) {
                    ?>
					<thead>
						<tr style="background: #e9e9e9;">
							<th colspan="4" style="text-align: center; border: 1px solid #e1e1e1;">
								<?php 
                    echo sprintf('<a href="%1$s">%2$s</a>', esc_url(admin_url('admin.php?page=wc-settings&tab=shipping&zone_id=' . $zone->get_id())), $zone->get_zone_name());
                    ?>
								<?php 
                    esc_html_e('Methods', 'woocommerce-shipping-estimate');
                    ?>
							</th>
						</tr>
						<tr>
							<th class="name" style="padding-left: 2% !important"><?php 
                    esc_html_e('Name', 'woocommerce-shipping-estimate');
                    ?>
</th>
							<th class="type"><?php 
                    esc_html_e('Type', 'woocommerce-shipping-estimate');
                    ?>
</th>
							<th class="day-from"><?php 
                    esc_html_e('From (days)', 'woocommerce-shipping-estimate');
                    ?>
 <?php 
                    echo wc_help_tip(__('The earliest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
                    ?>
</th>
							<th class="day-to"><?php 
                    esc_html_e('To (days)', 'woocommerce-shipping-estimate');
                    ?>
 <?php 
                    echo wc_help_tip(__('The latest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
                    ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
                    foreach ($zone->get_shipping_methods() as $instance_id => $method) {
                        ?>
						<tr>
							<td style="padding-left: 2%" class="name">
								<a href="<?php 
                        echo esc_url(admin_url('admin.php?page=wc-settings&tab=shipping&instance_id=' . $instance_id));
                        ?>
"><?php 
                        echo esc_html($method->get_title());
                        ?>
</a>
							</td>
							<td class="type">
								<?php 
                        echo esc_html($method->get_method_title());
                        ?>
							</td>
							<td class="day-from">
								<input type="number" step="1" min="0" name="method_estimate_from[<?php 
                        echo esc_attr($instance_id);
                        ?>
]" value="<?php 
                        echo isset($method_estimate_from[$instance_id]) ? $method_estimate_from[$instance_id] : '';
                        ?>
" />
							</td>
							<td class="day-to">
								<input type="number" step="1" min="0" name="method_estimate_to[<?php 
                        echo esc_attr($instance_id);
                        ?>
]" value="<?php 
                        echo isset($method_estimate_to[$instance_id]) ? $method_estimate_to[$instance_id] : '';
                        ?>
" />
							</td>
						</tr>
					<?php 
                    }
                    ?>
					</tbody>
					<?php 
                }
                ?>
				<?php 
            }
            ?>
				<?php 
        }
        ?>
				
				<?php 
        $world_zone = WC_Shipping_Zones::get_zone(0);
        ?>
				<?php 
        $world_zone_methods = $world_zone->get_shipping_methods();
        ?>
				<?php 
        if (!empty($world_zone_methods)) {
            ?>
					<thead>
						<tr style="background: #e9e9e9;">
							<th colspan="4" style="text-align: center; border: 1px solid #e1e1e1;">
								<?php 
            $zone_name = __('Rest of the World', 'woocommerce-shipping-estimate');
            ?>
								<?php 
            echo sprintf('<a href="%1$s">%2$s</a>', esc_url(admin_url('admin.php?page=wc-settings&tab=shipping&zone_id=0')), $zone_name);
            ?>
								<?php 
            esc_html_e('Methods', 'woocommerce-shipping-estimate');
            ?>
							</th>
						</tr>
						<tr>
							<th class="name" style="padding-left: 2% !important"><?php 
            esc_html_e('Name', 'woocommerce-shipping-estimate');
            ?>
</th>
							<th class="type"><?php 
            esc_html_e('Type', 'woocommerce-shipping-estimate');
            ?>
</th>
							<th class="day-from"><?php 
            esc_html_e('From (days)', 'woocommerce-shipping-estimate');
            ?>
 <?php 
            echo wc_help_tip(__('The earliest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
            ?>
</th>
							<th class="day-to"><?php 
            esc_html_e('To (days)', 'woocommerce-shipping-estimate');
            ?>
 <?php 
            echo wc_help_tip(__('The latest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
            ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
            foreach ($world_zone_methods as $instance_id => $method) {
                ?>
						<tr>
							<td style="padding-left: 2%" class="name">
								<a href="<?php 
                echo esc_url(admin_url('admin.php?page=wc-settings&tab=shipping&instance_id=' . $instance_id));
                ?>
"><?php 
                echo esc_html($method->get_title());
                ?>
</a>
							</td>
							<td class="type">
								<?php 
                echo esc_html($method->get_method_title());
                ?>
							</td>
							<td class="day-from">
								<input type="number" step="1" min="0" name="method_estimate_from[<?php 
                echo esc_attr($instance_id);
                ?>
]" value="<?php 
                echo isset($method_estimate_from[$instance_id]) ? $method_estimate_from[$instance_id] : '';
                ?>
" />
							</td>
							<td class="day-to">
								<input type="number" step="1" min="0" name="method_estimate_to[<?php 
                echo esc_attr($instance_id);
                ?>
]" value="<?php 
                echo isset($method_estimate_to[$instance_id]) ? $method_estimate_to[$instance_id] : '';
                ?>
" />
							</td>
						</tr>
					<?php 
            }
            ?>
					</tbody>
					<?php 
        }
        ?>
					<?php 
        $methods = WC()->shipping->get_shipping_methods();
        unset($methods['flat_rate'], $methods['free_shipping'], $methods['local_pickup']);
        if (!empty($methods)) {
            ?>
					<thead>
						<tr style="background: #e9e9e9;">
							<th colspan="4" style="text-align: center; border: 1px solid #e1e1e1;"><?php 
            esc_html_e('Other Methods', 'woocommerce-shipping-estimate');
            ?>
</th>
						</tr>
						<tr>
							<th class="name" style="padding-left: 2% !important"><?php 
            esc_html_e('Name', 'woocommerce-shipping-estimate');
            ?>
</th>
							<th class="id"><?php 
            esc_html_e('ID', 'woocommerce-shipping-estimate');
            ?>
</th>
							<th class="day-from"><?php 
            esc_html_e('From (days)', 'woocommerce-shipping-estimate');
            ?>
 <?php 
            echo wc_help_tip(__('The earliest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
            ?>
</th>
							<th class="day-to"><?php 
            esc_html_e('To (days)', 'woocommerce-shipping-estimate');
            ?>
 <?php 
            echo wc_help_tip(__('The latest estimated arrival. Can be left blank.', 'woocommerce-shipping-estimate'));
            ?>
</th>
						</tr>
					</thead>
					<tbody>
						<?php 
            foreach ($methods as $method_id => $method) {
                ?>
							<tr>
								<td style="padding-left: 2%" class="name">
									<a href="<?php 
                echo esc_url(admin_url('admin.php?page=wc-settings&tab=shipping&section=' . $method_id));
                ?>
">
									<?php 
                echo esc_html($method->get_title());
                ?>
									</a>
								</td>
								<td class="id">
									<?php 
                echo esc_attr($method->id);
                ?>
								</td>
								<td class="day-from">
									<input type="number" step="1" min="0" name="method_estimate_from[<?php 
                echo esc_attr($method_id);
                ?>
]" value="<?php 
                echo isset($method_estimate_from[$method_id]) ? $method_estimate_from[$method_id] : '';
                ?>
" />
								</td>
								<td width="1%" class="day-to">
									<input type="number" step="1" min="0" name="method_estimate_to[<?php 
                echo esc_attr($method_id);
                ?>
]" value="<?php 
                echo isset($method_estimate_to[$method_id]) ? $method_estimate_to[$method_id] : '';
                ?>
" />
								</td>
							</tr>
						<?php 
            }
            ?>
					</tbody>
					<?php 
        }
        ?>
					<tfoot>
						<tr>
							<th colspan="4" style="padding-left: 2% !important"><span class="description"><?php 
        esc_html_e('Set the estimated range of days required for each method.', 'woocommerce-shipping-estimate');
        ?>
</span></th>
						</tr>
					</tfoot>
				</table>
			</td>
		</tr>
		<?php 
    }