WC_API_Taxes::edit_tax( int $id, array $data )
Edit a tax
Description Description
Parameters Parameters
- $id
-
(Required) The tax ID
- $data
-
(Required)
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v3/class-wc-api-taxes.php
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 compound 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() ) ); } }
Changelog Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |