Exemplo n.º 1
0
 /**
  * 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 = $this->is_cart_contains_subscription();
     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) {
                     if ($this->is_wcs_gte('2.0.0')) {
                         WC_Subscriptions_Coupon::increase_coupon_discount_amount($this->global_wc()->cart, $code, $smart_coupon->amount);
                     } else {
                         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;
             }
         }
     }
 }