WC_Tax::calc_tax PHP Method

calc_tax() public static method

Calculate tax for a line.
public static calc_tax ( float $price, array $rates, boolean $price_includes_tax = false, boolean $suppress_rounding = false ) : array
$price float Price to calc tax on
$rates array Rates to apply
$price_includes_tax boolean Whether the passed price has taxes included
$suppress_rounding boolean Whether to suppress any rounding from taking place
return array Array of rates + prices after tax
    public static function calc_tax($price, $rates, $price_includes_tax = false, $suppress_rounding = false)
    {
        // Work in pence to X precision
        $price = self::precision($price);
        if ($price_includes_tax) {
            $taxes = self::calc_inclusive_tax($price, $rates);
        } else {
            $taxes = self::calc_exclusive_tax($price, $rates);
        }
        // Round to precision
        if (!self::$round_at_subtotal && !$suppress_rounding) {
            $taxes = array_map('round', $taxes);
            // Round to precision
        }
        // Remove precision
        $price = self::remove_precision($price);
        $taxes = array_map(array(__CLASS__, 'remove_precision'), $taxes);
        return apply_filters('woocommerce_calc_tax', $taxes, $price, $rates, $price_includes_tax, $suppress_rounding);
    }

Usage Example

Esempio n. 1
0
 /**
  * Calculate fees
  */
 public function calculate_fees()
 {
     // Reset fees before calculation
     $this->fee_total = 0;
     $this->fees = array();
     // Fire an action where developers can add their fees
     do_action('woocommerce_cart_calculate_fees', $this);
     // If fees were added, total them and calculate tax
     if (!empty($this->fees)) {
         foreach ($this->fees as $fee_key => $fee) {
             $this->fee_total += $fee->amount;
             if ($fee->taxable) {
                 // Get tax rates
                 $tax_rates = $this->tax->get_rates($fee->tax_class);
                 $fee_taxes = $this->tax->calc_tax($fee->amount, $tax_rates, false);
                 if (!empty($fee_taxes)) {
                     // Set the tax total for this fee
                     $this->fees[$fee_key]->tax = array_sum($fee_taxes);
                     // Set tax data - Since 2.2
                     $this->fees[$fee_key]->tax_data = $fee_taxes;
                     // Tax rows - merge the totals we just got
                     foreach (array_keys($this->taxes + $fee_taxes) as $key) {
                         $this->taxes[$key] = (isset($fee_taxes[$key]) ? $fee_taxes[$key] : 0) + (isset($this->taxes[$key]) ? $this->taxes[$key] : 0);
                     }
                 }
             }
         }
     }
 }
All Usage Examples Of WC_Tax::calc_tax