Internal use only.
public static _update_tax_rate ( integer $tax_rate_id, array $tax_rate ) | ||
$tax_rate_id | integer | |
$tax_rate | array |
/** * 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))); }
/** * 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); }
/** * 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())); } }
/** * 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()); } }
/** * Test updating a tax rate. */ public function test__update_tax_rate() { global $wpdb; // Define a rate $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); // Update a rate $tax_rate = array('tax_rate_country' => 'US'); // Run function WC_Tax::_update_tax_rate($tax_rate_id, $tax_rate); $this->assertNotFalse($wpdb->last_result); 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])); } } }
/** * 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; }