/** * Test: WC_Shipping_Zones::delete_zone */ public function test_delete_zone() { // Setup WC_Helper_Shipping_Zones::create_mock_zones(); // Test WC_Shipping_Zones::delete_zone(1); $zones = WC_Shipping_Zones::get_zones(); // Assert $this->assertTrue(3 === sizeof($zones)); // Clean WC_Helper_Shipping_Zones::remove_mock_zones(); }
/** * 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); }
public function __construct($options) { global $wpdb; $this->wpdb = $wpdb; $this->import = $options['import']; $this->count = $options['count']; $this->xml = $options['xml']; $this->logger = $options['logger']; $this->chunk = $options['chunk']; $this->xpath = $options['xpath_prefix']; $this->prices_include_tax = 'yes' === get_option('woocommerce_prices_include_tax', 'no'); $this->payment_gateways = WC_Payment_Gateways::instance()->get_available_payment_gateways(); $this->shipping_methods = WC()->shipping->get_shipping_methods(); if (class_exists('WC_Shipping_Zones')) { $zones = WC_Shipping_Zones::get_zones(); if (!empty($zones)) { foreach ($zones as $zone_id => $zone) { if (!empty($zone['shipping_methods'])) { foreach ($zone['shipping_methods'] as $method) { $this->shipping_zone_methods[] = $method; } } } } else { $zone = new WC_Shipping_Zone(0); $this->shipping_zone_methods = $zone->get_shipping_methods(); } } $tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes')))); if ($tax_classes) { // Add Standard tax class if (!in_array('', $tax_classes)) { $tax_classes[] = ''; } foreach ($tax_classes as $class) { foreach (WC_Tax::get_rates_for_tax_class(sanitize_title($class)) as $rate_key => $rate) { $this->tax_rates[$rate->tax_rate_id] = $rate; } } } add_filter('wp_all_import_is_post_to_skip', array(&$this, 'wp_all_import_is_post_to_skip'), 10, 5); add_filter('wp_all_import_combine_article_data', array(&$this, 'wp_all_import_combine_article_data'), 10, 4); }
/** * Show zones */ protected function zones_screen() { global $wpdb; $allowed_countries = WC()->countries->get_allowed_countries(); $continents = WC()->countries->get_continents(); $method_count = wc_get_shipping_method_count(); wp_localize_script('wc-shipping-zones', 'shippingZonesLocalizeScript', array('zones' => WC_Shipping_Zones::get_zones(), 'default_zone' => array('zone_id' => 0, 'zone_name' => '', 'zone_order' => null), '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_failed' => __('Your changes were not saved. Please retry.', 'woocommerce'), 'no_shipping_methods_offered' => __('No shipping methods offered to this zone.', 'woocommerce')))); wp_enqueue_script('wc-shipping-zones'); include_once dirname(__FILE__) . '/views/html-admin-page-shipping-zones.php'; }
/** * Handle submissions from assets/js/wc-shipping-zones.js Backbone model. */ public static function shipping_zones_save_changes() { if (!isset($_POST['wc_shipping_zones_nonce'], $_POST['changes'])) { 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; } $changes = $_POST['changes']; foreach ($changes as $zone_id => $data) { if (isset($data['deleted'])) { if (isset($data['newRow'])) { // So the user added and deleted a new row. // That's fine, it's not in the database anyways. NEXT! continue; } WC_Shipping_Zones::delete_zone($zone_id); continue; } $zone_data = array_intersect_key($data, array('zone_id' => 1, 'zone_name' => 1, 'zone_order' => 1, 'zone_locations' => 1, 'zone_postcodes' => 1)); if (isset($zone_data['zone_id'])) { $zone = new WC_Shipping_Zone($zone_data['zone_id']); if (isset($zone_data['zone_name'])) { $zone->set_zone_name($zone_data['zone_name']); } if (isset($zone_data['zone_order'])) { $zone->set_zone_order($zone_data['zone_order']); } if (isset($zone_data['zone_locations'])) { $zone->clear_locations(array('state', 'country', 'continent')); $locations = array_filter(array_map('wc_clean', (array) $zone_data['zone_locations'])); foreach ($locations as $location) { // Each posted location will be in the format type:code $location_parts = explode(':', $location); switch ($location_parts[0]) { case 'state': $zone->add_location($location_parts[1] . ':' . $location_parts[2], 'state'); break; case 'country': $zone->add_location($location_parts[1], 'country'); break; case 'continent': $zone->add_location($location_parts[1], 'continent'); break; } } } if (isset($zone_data['zone_postcodes'])) { $zone->clear_locations('postcode'); $postcodes = array_filter(array_map('strtoupper', array_map('wc_clean', explode("\n", $zone_data['zone_postcodes'])))); foreach ($postcodes as $postcode) { $zone->add_location($postcode, 'postcode'); } } $zone->save(); } } wp_send_json_success(array('zones' => WC_Shipping_Zones::get_zones())); }
/** * Get the shipping methods for all shipping zones. * * Note: WooCommerce 2.6 intoduces the concept of Shipping Zones * * @return array (Array of) all shipping methods instances */ public function getZonesShippingMethods() { $zones = array(); // Rest of the World zone $zone = new \WC_Shipping_Zone(); $zones[$zone->get_zone_id()] = $zone->get_data(); $zones[$zone->get_zone_id()]['formatted_zone_location'] = $zone->get_formatted_location(); $zones[$zone->get_zone_id()]['shipping_methods'] = $zone->get_shipping_methods(); // Add user configured zones $zones = array_merge($zones, \WC_Shipping_Zones::get_zones()); $shipping_methods = array(); // Format: $shipping_methods[zone_name_method_id] => shipping_method_object // where zone_name is e.g. domestic, europe, rest_of_the_world, and // methods_id is e.g. flat_rate, free_shiping, local_pickup, etc foreach ($zones as $zone) { foreach ($zone['shipping_methods'] as $instance_id => $shipping_method) { // Zone names are converted to all lower-case and spaces replaced with $shipping_methods[$shipping_method->id . '_' . $instance_id] = $shipping_method; } } return $shipping_methods; }
/** * Handle submissions from assets/js/wc-shipping-zones.js Backbone model. */ public static function shipping_zones_save_changes() { if (!isset($_POST['wc_shipping_zones_nonce'], $_POST['changes'])) { 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; } $changes = $_POST['changes']; foreach ($changes as $zone_id => $data) { if (isset($data['deleted'])) { if (isset($data['newRow'])) { // So the user added and deleted a new row. // That's fine, it's not in the database anyways. NEXT! continue; } WC_Shipping_Zones::delete_zone($zone_id); continue; } $zone_data = array_intersect_key($data, array('zone_id' => 1, 'zone_order' => 1)); if (isset($zone_data['zone_id'])) { $zone = new WC_Shipping_Zone($zone_data['zone_id']); if (isset($zone_data['zone_order'])) { $zone->set_zone_order($zone_data['zone_order']); } $zone->save(); } } wp_send_json_success(array('zones' => WC_Shipping_Zones::get_zones())); }
/** * Show zones */ private function zones_screen() { $allowed_countries = WC()->countries->get_allowed_countries(); $continents = WC()->countries->get_continents(); wp_localize_script('wc-shipping-zones', 'shippingZonesLocalizeScript', array('zones' => WC_Shipping_Zones::get_zones(), 'default_zone' => array('zone_id' => 0, 'zone_name' => '', 'zone_order' => null), '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_failed' => __('Your changes were not saved. Please retry.', 'woocommerce'), 'no_methods' => '<a href="#" class="add_shipping_method button">' . __('Add Shipping Method', 'woocommerce') . '</a>', 'add_another_method' => '<a href="#" class="add_shipping_method button">' . __('Add Shipping Method', 'woocommerce') . '</a>'))); wp_enqueue_script('wc-shipping-zones'); include_once 'views/html-admin-page-shipping-zones.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§ion=' . $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 }