/** * 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; }
/** * Get the tax for the given ID * * @since 2.5.0 * * @param int $id The tax ID * @param string $fields fields to include in response * * @return array|WP_Error */ public function get_tax($id, $fields = null) { global $wpdb; try { $id = absint($id); // Permissions check if (!current_user_can('manage_woocommerce')) { throw new WC_API_Exception('woocommerce_api_user_cannot_read_tax', __('You do not have permission to read tax rate', 'woocommerce'), 401); } // Get tax rate details $tax = WC_Tax::_get_tax_rate($id); if (is_wp_error($tax) || empty($tax)) { throw new WC_API_Exception('woocommerce_api_invalid_tax_id', __('A tax rate with the provided ID could not be found', 'woocommerce'), 404); } $tax_data = array('id' => (int) $tax['tax_rate_id'], 'country' => $tax['tax_rate_country'], 'state' => $tax['tax_rate_state'], 'postcode' => '', 'city' => '', 'rate' => $tax['tax_rate'], 'name' => $tax['tax_rate_name'], 'priority' => (int) $tax['tax_rate_priority'], 'compound' => (bool) $tax['tax_rate_compound'], 'shipping' => (bool) $tax['tax_rate_shipping'], 'order' => (int) $tax['tax_rate_order'], 'class' => $tax['tax_rate_class'] ? $tax['tax_rate_class'] : 'standard'); // Get locales from a tax rate $locales = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT location_code, location_type\n\t\t\t\tFROM {$wpdb->prefix}woocommerce_tax_rate_locations\n\t\t\t\tWHERE tax_rate_id = %d\n\t\t\t", $id)); if (!is_wp_error($tax) && !is_null($tax)) { foreach ($locales as $locale) { $tax_data[$locale->location_type] = $locale->location_code; } } return array('tax' => apply_filters('woocommerce_api_tax_response', $tax_data, $tax, $fields, $this)); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Format taxes from query result to items in which each item contain * common properties of item, for instance `tax_rate_id` will be `id`. * * @since 2.5.0 * @param array $taxes Array of tax rate. * @return array Items */ protected function format_taxes_to_items($taxes) { global $wpdb; $items = array(); foreach ($taxes as $tax_id) { $id = is_object($tax_id) ? $tax_id->tax_rate_id : $tax_id; $id = absint($id); // Get tax rate details $tax = WC_Tax::_get_tax_rate($id); if (is_wp_error($tax) || empty($tax)) { continue; } $tax_data = array('id' => $tax['tax_rate_id'], 'country' => $tax['tax_rate_country'], 'state' => $tax['tax_rate_state'], 'postcode' => '', 'city' => '', 'rate' => $tax['tax_rate'], 'name' => $tax['tax_rate_name'], 'priority' => (int) $tax['tax_rate_priority'], 'compound' => (bool) $tax['tax_rate_compound'], 'shipping' => (bool) $tax['tax_rate_shipping'], 'order' => (int) $tax['tax_rate_order'], 'class' => $tax['tax_rate_class'] ? $tax['tax_rate_class'] : 'standard'); // Get locales from a tax rate $locales = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT location_code, location_type\n\t\t\t\tFROM {$wpdb->prefix}woocommerce_tax_rate_locations\n\t\t\t\tWHERE tax_rate_id = %d\n\t\t\t", $id)); if (!is_wp_error($tax) && !is_null($tax)) { foreach ($locales as $locale) { $tax_data[$locale->location_type] = $locale->location_code; } } $items[] = $tax_data; } return $items; }