WC_Tax::get_rate_code PHP Method

get_rate_code() public static method

Get a rates code. Code is made up of COUNTRY-STATE-NAME-Priority. E.g GB-VAT-1, US-AL-TAX-1.
public static get_rate_code ( mixed $key_or_rate ) : string
$key_or_rate mixed Tax rate ID, or the db row itself in object format
return string
    public static function get_rate_code($key_or_rate)
    {
        global $wpdb;
        if (is_object($key_or_rate)) {
            $key = $key_or_rate->tax_rate_id;
            $rate = $key_or_rate;
        } else {
            $key = $key_or_rate;
            $rate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $key));
        }
        $code_string = '';
        if (null !== $rate) {
            $code = array();
            $code[] = $rate->tax_rate_country;
            $code[] = $rate->tax_rate_state;
            $code[] = $rate->tax_rate_name ? $rate->tax_rate_name : 'TAX';
            $code[] = absint($rate->tax_rate_priority);
            $code_string = strtoupper(implode('-', array_filter($code)));
        }
        return apply_filters('woocommerce_rate_code', $code_string, $key);
    }

Usage Example

Example #1
0
 /**
  * Get taxes, merged by code, formatted ready for output.
  *
  * @access public
  * @return void
  */
 public function get_tax_totals()
 {
     $taxes = $this->get_taxes();
     $tax_totals = array();
     foreach ($taxes as $key => $tax) {
         $code = $this->tax->get_rate_code($key);
         if (!isset($tax_totals[$code])) {
             $tax_totals[$code] = new stdClass();
             $tax_totals[$code]->amount = 0;
         }
         $tax_totals[$code]->is_compound = $this->tax->is_compound($key);
         $tax_totals[$code]->label = $this->tax->get_rate_label($key);
         $tax_totals[$code]->amount += $tax;
         $tax_totals[$code]->formatted_amount = woocommerce_price($tax_totals[$code]->amount);
     }
     return apply_filters('woocommerce_cart_tax_totals', $tax_totals, $this);
 }
All Usage Examples Of WC_Tax::get_rate_code