/** * add_eu_countries_vat_rates. * * @version 2.3.10 * @since 2.3.10 */ function add_eu_countries_vat_rates() { if (!isset($_POST['add_eu_countries_vat_rates'])) { return; } if (!is_super_admin() && !is_shop_manager()) { return; } $loop = 0; foreach (wcj_get_european_union_countries_with_vat() as $country => $rate) { $tax_rate = array('tax_rate_country' => $country, 'tax_rate' => $rate, 'tax_rate_name' => isset($_POST['wcj_tax_name']) ? $_POST['wcj_tax_name'] : __('VAT', 'woocommerce'), 'tax_rate_priority' => 1, 'tax_rate_compound' => 0, 'tax_rate_shipping' => 1, 'tax_rate_order' => $loop++, 'tax_rate_class' => ''); $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); WC_Tax::_update_tax_rate_postcodes($tax_rate_id, ''); WC_Tax::_update_tax_rate_cities($tax_rate_id, ''); } }
public function install_standard_rates() { // delete previous inserted standard rates $tax_rates = $this->get_tax_rates(); foreach ($tax_rates as $tax_rate) { $tax_rate_name = sprintf("EU VAT (%s)", $tax_rate->tax_rate_country); if (0 == strpos($tax_rate->tax_rate_name, $tax_rate_name)) { WC_Tax::_delete_tax_rate($tax_rate->tax_rate_id); } } foreach ($this->tax_rates_data as $key => $value) { $tax_rate = array('tax_rate_country' => $key, 'tax_rate_state' => '*', 'tax_rate' => $value["standard_rate"], 'tax_rate_name' => sprintf("EU VAT (%s) %s%%", $key, $value["standard_rate"]), 'tax_rate_priority' => 1, 'tax_rate_compound' => 1, 'tax_rate_shipping' => 1, 'tax_rate_class' => ''); $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean('*')); WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean('*')); } }
/** * copy of import function: WC_Tax_Rate_Importer->import() * https://github.com/woothemes/woocommerce/blob/master/includes/admin/importers/class-wc-tax-rate-importer.php */ private function import_dummy_tax() { // clear tax rate tables global $wpdb; $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rates"); $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations"); $file = WC_POS_PLUGIN_PATH . 'tests/data/sample_tax_rates.csv'; $delimiter = ','; $loop = 0; if (($handle = fopen($file, "r")) !== false) { $header = fgetcsv($handle, 0, $delimiter); if (10 === sizeof($header)) { while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { list($country, $state, $postcode, $city, $rate, $name, $priority, $compound, $shipping, $class) = $row; $tax_rate = array('tax_rate_country' => $country, 'tax_rate_state' => $state, 'tax_rate' => $rate, 'tax_rate_name' => $name, 'tax_rate_priority' => $priority, 'tax_rate_compound' => $compound ? 1 : 0, 'tax_rate_shipping' => $shipping ? 1 : 0, 'tax_rate_order' => $loop++, 'tax_rate_class' => $class); $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean($postcode)); WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean($city)); } } fclose($handle); } }
/** * Handle submissions from assets/js/settings-views-html-settings-tax.js Backbone model. */ public static function tax_rates_save_changes() { if (!isset($_POST['current_class'], $_POST['wc_tax_nonce'], $_POST['changes'])) { wp_send_json_error('missing_fields'); exit; } $current_class = $_POST['current_class']; // This is sanitized seven lines later. if (!wp_verify_nonce($_POST['wc_tax_nonce'], 'wc_tax_nonce-class:' . $current_class)) { wp_send_json_error('bad_nonce'); exit; } $current_class = WC_Tax::format_tax_rate_class($current_class); // Check User Caps if (!current_user_can('manage_woocommerce')) { wp_send_json_error('missing_capabilities'); exit; } $changes = $_POST['changes']; foreach ($changes as $tax_rate_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_Tax::_delete_tax_rate($tax_rate_id); } $tax_rate = array_intersect_key($data, array('tax_rate_country' => 1, 'tax_rate_state' => 1, 'tax_rate' => 1, 'tax_rate_name' => 1, 'tax_rate_priority' => 1, 'tax_rate_compound' => 1, 'tax_rate_shipping' => 1, 'tax_rate_order' => 1)); if (isset($data['newRow'])) { // Hurrah, shiny and new! $tax_rate['tax_rate_class'] = $current_class; $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); } else { // Updating an existing rate ... if (!empty($tax_rate)) { WC_Tax::_update_tax_rate($tax_rate_id, $tax_rate); } } if (isset($data['postcode'])) { WC_Tax::_update_tax_rate_postcodes($tax_rate_id, array_map('wc_clean', $data['postcode'])); } if (isset($data['city'])) { WC_Tax::_update_tax_rate_cities($tax_rate_id, array_map('wc_clean', $data['city'])); } } wp_send_json_success(array('rates' => WC_Tax::get_rates_for_tax_class($current_class))); }
/** * Edit a tax * * @since 2.5.0 * * @param int $id The tax ID * @param array $data * * @return array */ public function edit_tax($id, $data) { try { if (!isset($data['tax'])) { throw new WC_API_Exception('woocommerce_api_missing_tax_data', sprintf(__('No %1$s data specified to edit %1$s', 'woocommerce'), 'tax'), 400); } // Check permissions if (!current_user_can('manage_woocommerce')) { throw new WC_API_Exception('woocommerce_api_user_cannot_edit_tax', __('You do not have permission to edit tax rates', 'woocommerce'), 401); } $data = $data['tax']; // Get current tax rate data $tax = $this->get_tax($id); if (is_wp_error($tax)) { $error_data = $tax->get_error_data(); throw new WC_API_Exception($tax->get_error_code(), $tax->get_error_message(), $error_data['status']); } $current_data = $tax['tax']; $data = apply_filters('woocommerce_api_edit_tax_data', $data, $this); $tax_data = array(); $default_fields = array('tax_rate_country', 'tax_rate_state', 'tax_rate', 'tax_rate_name', 'tax_rate_priority', 'tax_rate_compound', 'tax_rate_shipping', 'tax_rate_order', 'tax_rate_class'); foreach ($data as $key => $value) { $new_key = 'rate' === $key ? 'tax_rate' : 'tax_rate_' . $key; // Check if the key is valid if (!in_array($new_key, $default_fields)) { continue; } // Test new data against current data if ($value === $current_data[$key]) { continue; } // Fix compund and shipping values if (in_array($key, array('compound', 'shipping'))) { $value = $value ? 1 : 0; } $tax_data[$new_key] = $value; } // Update tax rate WC_Tax::_update_tax_rate($id, $tax_data); // Update locales if (!empty($data['postcode']) && $current_data['postcode'] != $data['postcode']) { WC_Tax::_update_tax_rate_postcodes($id, wc_clean($data['postcode'])); } if (!empty($data['city']) && $current_data['city'] != $data['city']) { WC_Tax::_update_tax_rate_cities($id, wc_clean($data['city'])); } do_action('woocommerce_api_edit_tax_rate', $id, $data); return $this->get_tax($id); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * import function. * * @param mixed $file */ public function import($file) { if (!is_file($file)) { $this->import_error(__('The file does not exist, please try again.', 'woocommerce')); } $this->import_start(); $loop = 0; if (($handle = fopen($file, "r")) !== false) { $header = fgetcsv($handle, 0, $this->delimiter); if (10 === sizeof($header)) { while (($row = fgetcsv($handle, 0, $this->delimiter)) !== false) { list($country, $state, $postcode, $city, $rate, $name, $priority, $compound, $shipping, $class) = $row; $tax_rate = array('tax_rate_country' => $country, 'tax_rate_state' => $state, 'tax_rate' => $rate, 'tax_rate_name' => $name, 'tax_rate_priority' => $priority, 'tax_rate_compound' => $compound ? 1 : 0, 'tax_rate_shipping' => $shipping ? 1 : 0, 'tax_rate_order' => $loop++, 'tax_rate_class' => $class); $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean($postcode)); WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean($city)); } } else { $this->import_error(__('The CSV is invalid.', 'woocommerce')); } fclose($handle); } // Show Result echo '<div class="updated settings-error below-h2"><p> ' . sprintf(__('Import complete - imported <strong>%s</strong> tax rates.', 'woocommerce'), $loop) . ' </p></div>'; $this->import_end(); }
/** * Take tax data from the request and return the updated or newly created rate. * * @todo Replace with CRUD in 2.7.0 * @param WP_REST_Request $request Full details about the request. * @param stdClass|null $current Existing tax object. * @return stdClass */ protected function create_or_update_tax($request, $current = null) { $id = absint(isset($request['id']) ? $request['id'] : 0); $data = array(); $fields = array('tax_rate_country', 'tax_rate_state', 'tax_rate', 'tax_rate_name', 'tax_rate_priority', 'tax_rate_compound', 'tax_rate_shipping', 'tax_rate_order', 'tax_rate_class'); foreach ($fields as $field) { // Keys via API differ from the stored names returned by _get_tax_rate. $key = 'tax_rate' === $field ? 'rate' : str_replace('tax_rate_', '', $field); // Remove data that was not posted. if (!isset($request[$key])) { continue; } // Test new data against current data. if ($current && $current->{$field} === $request[$key]) { continue; } // Add to data array. switch ($key) { case 'tax_rate_priority': case 'tax_rate_compound': case 'tax_rate_shipping': case 'tax_rate_order': $data[$field] = absint($request[$key]); break; case 'tax_rate_class': $data[$field] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : ''; break; default: $data[$field] = wc_clean($request[$key]); break; } } if ($id) { WC_Tax::_update_tax_rate($id, $data); } else { $id = WC_Tax::_insert_tax_rate($data); } // Add locales. if (!empty($request['postcode'])) { WC_Tax::_update_tax_rate_postcodes($id, wc_clean($request['postcode'])); } if (!empty($request['city'])) { WC_Tax::_update_tax_rate_cities($id, wc_clean($request['city'])); } return WC_Tax::_get_tax_rate($id, OBJECT); }
/** * Update a tax rate. * * ## OPTIONS * * <id> * : The ID of the tax rate to update. * * [--<field>=<value>] * : One or more fields to update. * * ## AVAILABLE FIELDS * * These fields are available for update command: * * * country * * state * * postcode * * city * * rate * * name * * priority * * compound * * shipping * * class * * ## EXAMPLES * * wp wc tax update 123 --rate=5 * * @since 2.5.0 */ public function update($args, $assoc_args) { try { // Get current tax rate data $tax_data = $this->format_taxes_to_items(array($args[0])); if (empty($tax_data)) { throw new WC_CLI_Exception('woocommerce_cli_invalid_tax_rate', sprintf(__('Invalid tax rate ID: %s', 'woocommerce'), $args[0])); } $current_data = $tax_data[0]; $id = $current_data['id']; $data = apply_filters('woocommerce_cli_update_tax_rate_data', $assoc_args, $id); $new_data = array(); $default_fields = array('tax_rate_country', 'tax_rate_state', 'tax_rate', 'tax_rate_name', 'tax_rate_priority', 'tax_rate_compound', 'tax_rate_shipping', 'tax_rate_order', 'tax_rate_class'); foreach ($data as $key => $value) { $new_key = 'rate' === $key ? 'tax_rate' : 'tax_rate_' . $key; // Check if the key is valid if (!in_array($new_key, $default_fields)) { continue; } // Test new data against current data if ($value === $current_data[$key]) { continue; } // Fix compund and shipping values if (in_array($key, array('compound', 'shipping'))) { $value = $value ? 1 : 0; } $new_data[$new_key] = $value; } // Update tax rate WC_Tax::_update_tax_rate($id, $new_data); // Update locales if (!empty($data['postcode']) && $current_data['postcode'] != $data['postcode']) { WC_Tax::_update_tax_rate_postcodes($id, wc_clean($data['postcode'])); } if (!empty($data['city']) && $current_data['city'] != $data['city']) { WC_Tax::_update_tax_rate_cities($id, wc_clean($data['city'])); } do_action('woocommerce_cli_update_tax_rate', $id, $data); WP_CLI::success("Updated tax rate {$id}."); } catch (WC_CLI_Exception $e) { WP_CLI::error($e->getMessage()); } }
/** * City saving. */ public function test__update_tax_rate_cities() { global $wpdb; $to_save = 'SOMEWHERE;SOMEWHERE_ELSE'; $tax_rate = array('tax_rate_country' => 'GB', 'tax_rate_state' => '', 'tax_rate' => '20.0000', 'tax_rate_name' => 'VAT', 'tax_rate_priority' => '1', 'tax_rate_compound' => '0', 'tax_rate_shipping' => '1', 'tax_rate_order' => '1', 'tax_rate_class' => ''); // Run function $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); WC_Tax::_update_tax_rate_cities($tax_rate_id, $to_save); $results = $wpdb->get_col($wpdb->prepare("SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE tax_rate_id = %d ORDER BY location_code ASC", $tax_rate_id)); $this->assertEquals(array('SOMEWHERE', 'SOMEWHERE_ELSE'), $results); WC_Tax::_delete_tax_rate($tax_rate_id); }
/** * Save tax rates */ public function save_tax_rates() { $current_class = sanitize_title($this->get_current_tax_class()); $index = 0; // Loop posted fields foreach ($_POST['tax_rate_country'] as $key => $value) { $mode = 0 === strpos($key, 'new-') ? 'insert' : 'update'; $tax_rate = $this->get_posted_tax_rate($key, $index++, $current_class); if ('insert' === $mode) { $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); } elseif (1 == $_POST['remove_tax_rate'][$key]) { WC_Tax::_delete_tax_rate($key); continue; } else { $tax_rate_id = $key; WC_Tax::_update_tax_rate($tax_rate_id, $tax_rate); } if (isset($_POST['tax_rate_postcode'][$key])) { WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean($_POST['tax_rate_postcode'][$key])); } if (isset($_POST['tax_rate_city'][$key])) { WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean($_POST['tax_rate_city'][$key])); } } }
/** * Save tax rates. */ public function save_tax_rates() { global $wpdb; $current_class = sanitize_title($this->get_current_tax_class()); // get the tax rate id of the first submited row $first_tax_rate_id = key($_POST['tax_rate_country']); // get the order position of the first tax rate id $tax_rate_order = absint($wpdb->get_var($wpdb->prepare("SELECT tax_rate_order FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $first_tax_rate_id))); $index = isset($tax_rate_order) ? $tax_rate_order : 0; // Loop posted fields foreach ($_POST['tax_rate_country'] as $key => $value) { $mode = 0 === strpos($key, 'new-') ? 'insert' : 'update'; $tax_rate = $this->get_posted_tax_rate($key, $index++, $current_class); if ('insert' === $mode) { $tax_rate_id = WC_Tax::_insert_tax_rate($tax_rate); } elseif (1 == $_POST['remove_tax_rate'][$key]) { $tax_rate_id = absint($key); WC_Tax::_delete_tax_rate($tax_rate_id); continue; } else { $tax_rate_id = absint($key); WC_Tax::_update_tax_rate($tax_rate_id, $tax_rate); } if (isset($_POST['tax_rate_postcode'][$key])) { WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean($_POST['tax_rate_postcode'][$key])); } if (isset($_POST['tax_rate_city'][$key])) { WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean($_POST['tax_rate_city'][$key])); } } }
/** * TaxJar API call * * @return void */ public function taxjar_api_call($options = array()) { global $woocommerce; $this->_log(':::: TaxJar Plugin requested ::::'); // Process $options array and turn them into varibables $options = is_array($options) ? $options : array(); extract(array_replace_recursive(array('to_country' => null, 'to_state' => null, 'to_zip' => null, 'to_city' => null, 'amount' => null, 'shipping_amount' => null), $options)); // Initalize some variables & properties $store_settings = $this->get_store_settings(); $customer = $woocommerce->customer; $this->tax_rate = 0; $this->amount_to_collect = 0; $this->item_collectable = 0; $this->shipping_collectable = 0; $this->freight_taxable = 1; $this->has_nexus = 0; $this->tax_source = 'origin'; $this->rate_id = null; // Strict conditions to be met before API call can be conducted if (empty($to_state) || empty($to_country) || empty($to_zip) || $customer->is_vat_exempt()) { return false; } // Setup Vars for API call $to_zip = explode(',', $to_zip); $to_zip = array_shift($to_zip); $from_country = $store_settings['store_country_setting']; $from_state = $store_settings['store_state_setting']; $from_zip = $store_settings['taxjar_zip_code_setting']; $from_city = $store_settings['taxjar_city_setting']; $this->_log(':::: TaxJar API called ::::'); $url = $this->uri . 'taxes'; $body_string = sprintf('plugin=woo&to_state=%s&from_state=%s&amount=%s&shipping=%s&from_city=%s&from_zip=%s&to_city=%s&to_zip=%s&from_country=%s&to_country=%s&line_items[][quantity]=1&line_items[][unit_price]=%s', $to_state, $from_state, $amount, $shipping_amount, $from_city, $from_zip, $to_city, $to_zip, $from_country, $to_country, $amount); // Build the URL and Transient key $url = str_replace(' ', '%20', $url); $cache_key = hash('md5', $url . '/' . $body_string); // To check to see if we hit the API or not $taxjar_response = false; // Make sure we don't have a cached rate if (false === ($cache_value = wp_cache_get($cache_key, 'taxjar'))) { // Log this request $this->_log("Requesting: " . $url); // Make API call with API token in header $response = wp_remote_post($url, array('headers' => array('Authorization' => 'Token token="' . $this->settings['api_token'] . '"', 'Content-Type' => 'application/x-www-form-urlencoded'), 'user-agent' => $this->ua, 'body' => $body_string)); // Fail loudly if we get an error from wp_remote_post if (is_wp_error($response)) { new WP_Error('request', __("There was an error retrieving the tax rates. Please check your server configuration.")); } else { if (200 == $response['response']['code']) { // Log the response $this->_log("Received: " . $response['body']); // Decode Response $taxjar_response = json_decode($response['body']); $taxjar_response = $taxjar_response->tax; // Update Properties based on Response $this->has_nexus = (int) $taxjar_response->has_nexus; $this->tax_source = empty($taxjar_response->tax_source) ? 'origin' : $taxjar_response->tax_source; $this->amount_to_collect = $taxjar_response->amount_to_collect; if (!empty($taxjar_response->breakdown)) { if (!empty($taxjar_response->breakdown->shipping)) { $this->shipping_collectable = $taxjar_response->breakdown->shipping->tax_collectable; } $this->item_collectable = $this->amount_to_collect - $this->shipping_collectable; } $this->tax_rate = $taxjar_response->rate; $this->freight_taxable = (int) $taxjar_response->freight_taxable; // Create cache value $cache_value = $this->amount_to_collect . '::' . $this->tax_rate . '::' . $this->freight_taxable . '::' . $this->has_nexus . '::' . $this->tax_source . '::' . $this->item_collectable . '::' . $this->shipping_collectable; // Log the new cached value $this->_log("Cache Value: " . $cache_value); // Set Cache wp_cache_set($cache_key, $cache_value, 'taxjar', $this->cache_time); } else { // Log Response Error $this->_log("Received (" . $response['response']['code'] . "): " . $response['body']); } } } else { // Read the cached value based on our delimiter $cache_value = explode('::', $cache_value); // Set properties to the cached values $this->amount_to_collect = $cache_value[0]; $this->tax_rate = $cache_value[1]; $this->freight_taxable = $cache_value[2]; $this->has_nexus = $cache_value[3]; $this->tax_source = $cache_value[4]; $this->item_collectable = $cache_value[5]; $this->shipping_collectable = $cache_value[6]; // Log Cached Response $this->_log("Cached Amount: " . $this->amount_to_collect); $this->_log("Cached Nexus: " . $this->has_nexus); $this->_log("Cached Source: " . $this->tax_source); $this->_log("Cached Rate: " . $this->tax_rate); $this->_log("Shipping Taxable? " . $this->freight_taxable); $this->_log("Item Tax to Collect: " . $this->item_collectable); $this->_log("Shipping Tax to Collect: " . $this->shipping_collectable); } // Remove taxes if they are set somehow and customer is exempt if ($customer->is_vat_exempt()) { $wc_cart_object->remove_taxes(); } elseif ($this->has_nexus) { //} else { // Use Woo core to find matching rates for taxable address $source_zip = $this->tax_source == 'destination' ? $to_zip : $from_zip; $source_city = $this->tax_source == 'destination' ? $to_city : $from_city; if (strtoupper($to_city) == strtoupper($from_city)) { $source_city = $to_city; } // Setup Tax Rates $tax_rates = array("tax_rate_country" => $to_country, "tax_rate_state" => $to_state, "tax_rate_name" => sprintf("%s Tax", $to_state), "tax_rate_priority" => 1, "tax_rate_compound" => false, "tax_rate_shipping" => $this->freight_taxable, "tax_rate" => $this->tax_rate * 100, "tax_rate_class" => ''); // Clear the cached rates $this->clear_wc_tax_cache($to_country, $to_state, $source_city, $source_zip); $this->_log($source_city); $wc_rates = WC_Tax::find_rates(array('country' => $to_country, 'state' => $to_state, 'postcode' => $source_zip, 'city' => $source_city, 'tax_class' => '')); // If we have rates, use those, but if no rates returned create one to link with, or use the first rate returned. if (!empty($wc_rates)) { $this->_log('::: TAX RATES FOUND :::'); $this->_log($wc_rates); // Get the existing ID $rate_id = key($wc_rates); // Update Tax Rates with TaxJar rates ( rates might be coming from a cached taxjar rate ) $this->_log(':: UPDATING TAX RATES TO ::'); $this->_log($tax_rates); WC_TAX::_update_tax_rate($rate_id, $tax_rates); } else { // Insert a rate if we did not find one $this->_log(':: Adding New Tax Rate ::'); $rate_id = WC_Tax::_insert_tax_rate($tax_rates); WC_Tax::_update_tax_rate_postcodes($rate_id, wc_clean($source_zip)); WC_Tax::_update_tax_rate_cities($rate_id, wc_clean($source_city)); } $this->_log('Tax Rate ID Set: ' . $rate_id); $this->rate_id = $rate_id; } }
/** * Update a single tax. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function update_item($request) { $id = (int) $request['id']; $current_tax = WC_Tax::_get_tax_rate($id, OBJECT); if (empty($id) || empty($current_tax)) { return new WP_Error('woocommerce_rest_invalid_id', __('Invalid resource id.', 'woocommerce'), array('status' => 404)); } $data = array(); $fields = array('tax_rate_country', 'tax_rate_state', 'tax_rate', 'tax_rate_name', 'tax_rate_priority', 'tax_rate_compound', 'tax_rate_shipping', 'tax_rate_order', 'tax_rate_class'); foreach ($fields as $field) { $key = 'tax_rate' === $field ? 'rate' : str_replace('tax_rate_', '', $field); if (!isset($request[$key])) { continue; } $value = $request[$key]; // Fix compund and shipping values. if (in_array($key, array('compound', 'shipping'))) { $value = (int) $request[$key]; } // Test new data against current data. if ($current_tax->{$field} === $value) { continue; } $data[$field] = $request[$key]; } // Update tax rate. WC_Tax::_update_tax_rate($id, $data); // Update locales. if (!isset($request['postcode'])) { WC_Tax::_update_tax_rate_postcodes($id, wc_clean($request['postcode'])); } if (!isset($request['city'])) { WC_Tax::_update_tax_rate_cities($id, wc_clean($request['city'])); } $tax = WC_Tax::_get_tax_rate($id, OBJECT); $this->update_additional_fields_for_object($tax, $request); /** * Fires after a tax is created or updated via the REST API. * * @param stdClass $tax Data used to create the tax. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating tax, false when updating tax. */ do_action('woocommerce_rest_insert_tax', $tax, $request, false); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($tax, $request); $response = rest_ensure_response($response); return $response; }