WC_Cart::get_discounted_price PHP Method

get_discounted_price() public method

Function to apply discounts to a product and get the discounted price (before tax is applied).
public get_discounted_price ( mixed $values, mixed $price, boolean $add_totals = false ) : float
$values mixed
$price mixed
$add_totals boolean (default: false)
return float price
    public function get_discounted_price($values, $price, $add_totals = false)
    {
        if (!$price) {
            return $price;
        }
        $undiscounted_price = $price;
        if (!empty($this->coupons)) {
            $product = $values['data'];
            foreach ($this->coupons as $code => $coupon) {
                if ($coupon->is_valid() && ($coupon->is_valid_for_product($product, $values) || $coupon->is_valid_for_cart())) {
                    $discount_amount = $coupon->get_discount_amount('yes' === get_option('woocommerce_calc_discounts_sequentially', 'no') ? $price : $undiscounted_price, $values, true);
                    $discount_amount = min($price, $discount_amount);
                    $price = max($price - $discount_amount, 0);
                    // Store the totals for DISPLAY in the cart
                    if ($add_totals) {
                        $total_discount = $discount_amount * $values['quantity'];
                        $total_discount_tax = 0;
                        if (wc_tax_enabled()) {
                            $tax_rates = WC_Tax::get_rates($product->get_tax_class());
                            $taxes = WC_Tax::calc_tax($discount_amount, $tax_rates, $this->prices_include_tax);
                            $total_discount_tax = WC_Tax::get_tax_total($taxes) * $values['quantity'];
                            $total_discount = $this->prices_include_tax ? $total_discount - $total_discount_tax : $total_discount;
                            $this->discount_cart_tax += $total_discount_tax;
                        }
                        $this->discount_cart += $total_discount;
                        $this->increase_coupon_discount_amount($code, $total_discount, $total_discount_tax);
                        $this->increase_coupon_applied_count($code, $values['quantity']);
                    }
                }
                // If the price is 0, we can stop going through coupons because there is nothing more to discount for this product.
                if (0 >= $price) {
                    break;
                }
            }
        }
        return apply_filters('woocommerce_get_discounted_price', $price, $values, $this);
    }
WC_Cart