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('*')); } }
/** * 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))); }
/** * Delete a tax * * @since 2.5.0 * * @param int $id The tax ID * * @return array */ public function delete_tax($id) { global $wpdb; try { // Check permissions if (!current_user_can('manage_woocommerce')) { throw new WC_API_Exception('woocommerce_api_user_cannot_delete_tax', __('You do not have permission to delete tax rates', 'woocommerce'), 401); } $id = absint($id); WC_Tax::_delete_tax_rate($id); if (0 === $wpdb->rows_affected) { throw new WC_API_Exception('woocommerce_api_cannot_delete_tax', __('Could not delete the tax rate', 'woocommerce'), 401); } return array('message' => sprintf(__('Deleted %s', 'woocommerce'), 'tax')); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Delete a single tax. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function delete_item($request) { global $wpdb; $id = (int) $request['id']; $force = isset($request['force']) ? (bool) $request['force'] : false; // We don't support trashing for this type, error out. if (!$force) { return new WP_Error('woocommerce_rest_trash_not_supported', __('Taxes do not support trashing.', 'woocommerce'), array('status' => 501)); } $tax = WC_Tax::_get_tax_rate($id, OBJECT); if (empty($id) || empty($tax)) { return new WP_Error('woocommerce_rest_invalid_id', __('Invalid resource ID.', 'woocommerce'), array('status' => 400)); } $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($tax, $request); WC_Tax::_delete_tax_rate($id); if (0 === $wpdb->rows_affected) { return new WP_Error('woocommerce_rest_cannot_delete', __('The resource cannot be deleted.', 'woocommerce'), array('status' => 500)); } /** * Fires after a tax is deleted via the REST API. * * @param stdClass $tax The tax data. * @param WP_REST_Response $response The response returned from the API. * @param WP_REST_Request $request The request sent to the API. */ do_action('woocommerce_rest_delete_tax', $tax, $response, $request); return $response; }
/** * Delete one or more tax rates. * * ## OPTIONS * * <id>... * : The tax rate ID to delete. * * ## EXAMPLES * * wp wc tax delete 123 * * wp wc tax delete $(wp wc tax list --format=ids) * * @since 2.5.0 */ public function delete($args, $assoc_args) { $exit_code = 0; foreach ($args as $tax_id) { $tax_id = absint($tax_id); $tax = WC_Tax::_get_tax_rate($tax_id); if (is_null($tax)) { $exit_code += 1; WP_CLI::warning("Failed deleting tax rate {$tax_id}."); continue; } do_action('woocommerce_cli_delete_tax_rate', $tax_id); WC_Tax::_delete_tax_rate($tax_id); WP_CLI::success("Deleted tax rate {$tax_id}."); } exit($exit_code ? 1 : 0); }
/** * 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])); } } }