save() public method

Save zone data to the database.
public save ( ) : integer
return integer
 /**
  * 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;
 }
 /**
  * Create some mock shipping zones to test against
  */
 public static function create_mock_zones()
 {
     self::remove_mock_zones();
     // Local 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();
     // Europe zone
     $zone = new WC_Shipping_Zone();
     $zone->set_zone_name('Europe');
     $zone->set_zone_order(2);
     $zone->add_location('EU', 'continent');
     $zone->save();
     // US california zone
     $zone = new WC_Shipping_Zone();
     $zone->set_zone_name('California');
     $zone->set_zone_order(3);
     $zone->add_location('US:CA', 'state');
     $zone->save();
     // US zone
     $zone = new WC_Shipping_Zone();
     $zone->set_zone_name('US');
     $zone->set_zone_order(4);
     $zone->add_location('US', 'country');
     $zone->save();
 }
Example #3
0
 /**
  * Helper method to create a Shipping Zone.
  *
  * @param string $name Zone name.
  * @param int    $order Optional. Zone sort order.
  * @return WC_Shipping_Zone
  */
 protected function create_shipping_zone($name, $order = 0, $locations = array())
 {
     $zone = new WC_Shipping_Zone(null);
     $zone->set_zone_name($name);
     $zone->set_zone_order($order);
     $zone->set_locations($locations);
     $zone->save();
     $this->zones[] = $zone;
     return $zone;
 }
 /**
  * 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->save();
     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));
     }
 }
Example #5
0
 /**
  * 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()));
 }
 /**
  * Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.
  */
 public static function shipping_zone_methods_save_changes()
 {
     if (!isset($_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_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;
     }
     if (!current_user_can('manage_woocommerce')) {
         wp_send_json_error('missing_capabilities');
         exit;
     }
     global $wpdb;
     $zone_id = wc_clean($_POST['zone_id']);
     $zone = new WC_Shipping_Zone($zone_id);
     $changes = $_POST['changes'];
     if (isset($changes['zone_name'])) {
         $zone->set_zone_name(wc_clean($changes['zone_name']));
     }
     if (isset($changes['zone_locations'])) {
         $zone->clear_locations(array('state', 'country', 'continent'));
         $locations = array_filter(array_map('wc_clean', (array) $changes['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($changes['zone_postcodes'])) {
         $zone->clear_locations('postcode');
         $postcodes = array_filter(array_map('strtoupper', array_map('wc_clean', explode("\n", $changes['zone_postcodes']))));
         foreach ($postcodes as $postcode) {
             $zone->add_location($postcode, 'postcode');
         }
     }
     if (isset($changes['methods'])) {
         foreach ($changes['methods'] as $instance_id => $data) {
             $method_id = $wpdb->get_var($wpdb->prepare("SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d", $instance_id));
             if (isset($data['deleted'])) {
                 $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id);
                 $option_key = $shipping_method->get_instance_option_key();
                 if ($wpdb->delete("{$wpdb->prefix}woocommerce_shipping_zone_methods", array('instance_id' => $instance_id))) {
                     delete_option($option_key);
                     do_action('woocommerce_shipping_zone_method_deleted', $instance_id, $method_id, $zone_id);
                 }
                 continue;
             }
             $method_data = array_intersect_key($data, array('method_order' => 1, 'enabled' => 1));
             if (isset($method_data['method_order'])) {
                 $wpdb->update("{$wpdb->prefix}woocommerce_shipping_zone_methods", array('method_order' => absint($method_data['method_order'])), array('instance_id' => absint($instance_id)));
             }
             if (isset($method_data['enabled'])) {
                 $is_enabled = absint('yes' === $method_data['enabled']);
                 if ($wpdb->update("{$wpdb->prefix}woocommerce_shipping_zone_methods", array('is_enabled' => $is_enabled), array('instance_id' => absint($instance_id)))) {
                     do_action('woocommerce_shipping_zone_method_status_toggled', $instance_id, $method_id, $zone_id, $is_enabled);
                 }
             }
         }
     }
     $zone->save();
     wp_send_json_success(array('zone_id' => $zone->get_id(), 'zone_name' => $zone->get_zone_name(), 'methods' => $zone->get_shipping_methods()));
 }
Example #7
-2
 /**
  * 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());
 }