WC_Tax::_insert_tax_rate PHP Method

_insert_tax_rate() public static method

Internal use only.
Since: 2.3.0
public static _insert_tax_rate ( array $tax_rate ) : integer
$tax_rate array
return integer tax rate id
    public static function _insert_tax_rate($tax_rate)
    {
        global $wpdb;
        $wpdb->insert($wpdb->prefix . 'woocommerce_tax_rates', self::prepare_tax_rate($tax_rate));
        WC_Cache_Helper::incr_cache_prefix('taxes');
        do_action('woocommerce_tax_rate_added', $wpdb->insert_id, $tax_rate);
        return $wpdb->insert_id;
    }

Usage Example

コード例 #1
1
 /**
  * Create a tax rate.
  *
  * ## OPTIONS
  *
  * [--<field>=<value>]
  * : Associative args for the new tax rate.
  *
  * [--porcelain]
  * : Outputs just the new tax rate id.
  *
  * ## AVAILABLE FIELDS
  *
  * These fields are available for create command:
  *
  * * country
  * * state
  * * postcode
  * * city
  * * rate
  * * name
  * * priority
  * * compound
  * * shipping
  * * class
  * * order
  *
  * ## EXAMPLES
  *
  *     wp wc tax create --country=US --rate=5 --class=standard --type=percent
  *
  * @since 2.5.0
  */
 public function create($__, $assoc_args)
 {
     $porcelain = isset($assoc_args['porcelain']);
     unset($assoc_args['porcelain']);
     $assoc_args = apply_filters('woocommerce_cli_create_tax_rate_data', $assoc_args);
     $tax_data = array('tax_rate_country' => '', 'tax_rate_state' => '', 'tax_rate' => '', 'tax_rate_name' => '', 'tax_rate_priority' => 1, 'tax_rate_compound' => 0, 'tax_rate_shipping' => 1, 'tax_rate_order' => 0, 'tax_rate_class' => '');
     foreach ($tax_data as $key => $value) {
         $new_key = str_replace('tax_rate_', '', $key);
         $new_key = 'tax_rate' === $new_key ? 'rate' : $new_key;
         if (isset($assoc_args[$new_key])) {
             if (in_array($new_key, array('compound', 'shipping'))) {
                 $tax_data[$key] = $assoc_args[$new_key] ? 1 : 0;
             } else {
                 $tax_data[$key] = $assoc_args[$new_key];
             }
         }
     }
     // Create tax rate.
     $id = WC_Tax::_insert_tax_rate($tax_data);
     // Add locales.
     if (!empty($assoc_args['postcode'])) {
         WC_Tax::_update_tax_rate_postcodes($id, wc_clean($assoc_args['postcode']));
     }
     if (!empty($assoc_args['city'])) {
         WC_Tax::_update_tax_rate_cities($id, wc_clean($assoc_args['city']));
     }
     do_action('woocommerce_cli_create_tax_rate', $id, $tax_data);
     if ($porcelain) {
         WP_CLI::line($id);
     } else {
         WP_CLI::success("Created tax rate {$id}.");
     }
 }
All Usage Examples Of WC_Tax::_insert_tax_rate