/** * Function to apply Gift Certificate's credit to cart */ public function apply_smart_coupon_to_cart() { $this->global_wc()->cart->smart_coupon_credit_used = array(); $cart_contains_subscription = false; if (class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription()) { $cart_contains_subscription = true; } if ($cart_contains_subscription) { $calculation_type = WC_Subscriptions_Cart::get_calculation_type(); if ($calculation_type == 'recurring_total') { return; } } if ($this->global_wc()->cart->applied_coupons) { foreach ($this->global_wc()->cart->applied_coupons as $code) { $smart_coupon = new WC_Coupon($code); if ($smart_coupon->is_valid() && $smart_coupon->discount_type == 'smart_coupon') { $order_total = $this->global_wc()->cart->cart_contents_total + $this->global_wc()->cart->tax_total + $this->global_wc()->cart->shipping_tax_total + $this->global_wc()->cart->shipping_total; if ($this->global_wc()->cart->discount_total != 0 && $this->global_wc()->cart->discount_total + $smart_coupon->amount > $order_total) { $smart_coupon->amount = $order_total - $this->global_wc()->cart->discount_total; } elseif ($smart_coupon->amount > $order_total) { $smart_coupon->amount = $order_total; } $this->global_wc()->cart->discount_total = $this->global_wc()->cart->discount_total + $smart_coupon->amount; if ($cart_contains_subscription) { WC_Subscriptions_Cart::increase_coupon_discount_amount($code, $smart_coupon->amount); } $this->global_wc()->cart->smart_coupon_credit_used[$code] = $smart_coupon->amount; //Code for displaying the price label for the store credit coupons if (empty($this->global_wc()->cart->coupon_discount_amounts)) { $this->global_wc()->cart->coupon_discount_amounts = array(); } $this->global_wc()->cart->coupon_discount_amounts[$code] = $smart_coupon->amount; } } } }
/** * Apply sign up fee or recurring fee discount after tax is calculated * * @since 1.2 * @version 1.3.6 */ public static function apply_subscription_discount_after_tax($coupon, $cart_item, $price) { global $woocommerce; $calculation_type = WC_Subscriptions_Cart::get_calculation_type(); if (sizeof($woocommerce->cart->cart_contents) > 0) { foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { if (!WC_Subscriptions_Product::is_subscription($cart_item['product_id'])) { continue; } if (!$coupon->apply_before_tax() && $coupon->is_valid() && self::is_subscription_discountable($cart_item, $coupon)) { $apply_sign_up_coupon = $apply_sign_up_percent_coupon = $apply_recurring_coupon = $apply_recurring_percent_coupon = $apply_initial_coupon = $apply_initial_percent_coupon = false; if ('sign_up_fee_total' == $calculation_type) { $apply_sign_up_coupon = 'sign_up_fee' == $coupon->type ? true : false; $apply_sign_up_percent_coupon = 'sign_up_fee_percent' == $coupon->type ? true : false; } elseif ('recurring_total' == $calculation_type) { $apply_recurring_coupon = 'recurring_fee' == $coupon->type ? true : false; $apply_recurring_percent_coupon = 'recurring_percent' == $coupon->type ? true : false; } if (in_array($calculation_type, array('combined_total', 'none'))) { if (!WC_Subscriptions_Cart::cart_contains_free_trial()) { // Apply recurring discounts to initial total if ('recurring_fee' == $coupon->type) { $apply_initial_coupon = true; } if ('recurring_percent' == $coupon->type) { $apply_initial_percent_coupon = true; } } if (WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() > 0) { // Apply sign-up discounts to initial total if ('sign_up_fee' == $coupon->type) { $apply_initial_coupon = true; } if ('sign_up_fee_percent' == $coupon->type) { $apply_initial_percent_coupon = true; } } } // Deduct coupon amounts if ($apply_sign_up_coupon || $apply_recurring_coupon || $apply_initial_coupon) { if ($price < $coupon->amount) { $discount_amount = $price; } else { $discount_amount = $coupon->amount; } $woocommerce->cart->discount_total = $woocommerce->cart->discount_total + $discount_amount * $cart_item['quantity']; WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, $discount_amount * $cart_item['quantity']); // Deduct coupon % discounts from relevant total } elseif ($apply_sign_up_percent_coupon || $apply_recurring_percent_coupon) { $woocommerce->cart->discount_total = $woocommerce->cart->discount_total + round($price / 100 * $coupon->amount, $woocommerce->cart->dp); WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, round($price / 100 * $coupon->amount, $woocommerce->cart->dp)); // Deduct coupon % discounts from combined total (we need to calculate percent from base price) } elseif ($apply_initial_percent_coupon) { $product_id = $cart_item['data']->is_type(array('subscription_variation')) ? $cart_item['data']->variation_id : $cart_item['data']->id; // We need to calculate the right amount to discount when the price is the combined sign-up fee and recurring amount if ('combined_total' == $calculation_type && !WC_Subscriptions_Cart::cart_contains_free_trial() && isset($woocommerce->cart->base_sign_up_fees[$product_id]) && $woocommerce->cart->base_sign_up_fees[$product_id] > 0) { $base_total = $woocommerce->cart->base_sign_up_fees[$product_id] + $woocommerce->cart->base_recurring_prices[$product_id]; if ('recurring_percent' == $coupon->type) { $portion_of_total = $woocommerce->cart->base_recurring_prices[$product_id] / $base_total; } if ('sign_up_fee_percent' == $coupon->type) { $portion_of_total = $woocommerce->cart->base_sign_up_fees[$product_id] / $base_total; } $amount_to_discount = WC_Subscriptions_Manager::get_amount_from_proportion($price, $portion_of_total); } else { $amount_to_discount = $price; } $discount_amount = round($amount_to_discount / 100 * $coupon->amount, $woocommerce->cart->dp); $woocommerce->cart->discount_total = $woocommerce->cart->discount_total + $discount_amount; WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, $discount_amount); } } } } }