/** * Function to apply discounts to a product and get the discounted price (before tax is applied). * * @access public * @param mixed $values * @param mixed $price * @param bool $add_totals (default: false) * @return float price */ public function get_discounted_price($values, $price, $add_totals = false) { if (!$price) { return $price; } if (!empty($this->applied_coupons)) { foreach ($this->applied_coupons as $code) { $coupon = new WC_Coupon($code); if ($coupon->apply_before_tax() && $coupon->is_valid()) { if ($coupon->is_valid_for_product($values['data']) || $coupon->is_valid_for_cart()) { $discount_amount = $coupon->get_discount_amount($price, $values, $single = true); $price = max($price - $discount_amount, 0); if ($add_totals) { $this->discount_cart += $discount_amount * $values['quantity']; $this->increase_coupon_discount_amount($code, $discount_amount * $values['quantity']); $this->increase_coupon_applied_count($code, $values['quantity']); } } } } } return apply_filters('woocommerce_get_discounted_price', $price, $values, $this); }
/** * Does the coupon have a value? (autocoupon should not be applied if it has no value) * @param WC_Coupon $coupon The coupon data * @return bool True if it has a value (discount, free shipping, whatever) otherwise false) **/ function coupon_has_a_value($coupon) { $has_a_value = false; if ($coupon->enable_free_shipping()) { $has_a_value = true; } else { //Test whether discount > 0 //See WooCommerce: class-wc-cart.php function get_discounted_price global $woocommerce; foreach ($woocommerce->cart->get_cart() as $cart_item) { if ($coupon->is_valid_for_cart() || $coupon->is_valid_for_product($cart_item['data'], $cart_item)) { if ($coupon->get_discount_amount($cart_item['data']->price, $cart_item) > 0) { $has_a_value = true; break; } } } } return apply_filters('wjecf_coupon_has_a_value', $has_a_value, $coupon); }