• Resolved nickterry

    (@nickterry)


    Hi there!

    Great Plugin. I noticed an issue when there are multiple subscriptions. The before_calculate_cart_total function creates an error because it only checks for the first [0] coupon in cart. I have fixed this with the following code:

    
        /**
         * Update cart data with coupon trial length & trial period before calculating cart data
         * 
         * @param $cart
         */
       
        public function before_caculate_cart_total($cart)
        {   
                
            if($cart->get_applied_coupons())
            {
                // Get Coupon id
    
                $coupon_ids = $cart->get_applied_coupons();
    
                foreach ($coupon_ids as $coupon_id) {
                    $coupon = new WC_Coupon( $coupon_id );
                    
                    if($coupon->is_type('subscription_trial'))
                    {
    
                        $coupon_trial_length = $coupon->get_meta('_wcsc_coupon_trial_length');
                        $coupon_trial_period = $coupon->get_meta('_wcsc_coupon_trial_period');
    
                        // Check if the coupon apply
                        if($coupon_trial_length>0)
                        {                            
                            foreach( $cart->cart_contents as $cart_item_id=>$cart_item ) 
                            {
    
                                if(is_a($cart_item['data'], 'WC_Product_Subscription') || is_a($cart_item['data'], 'WC_Product_Subscription_Variation'))
                                {
                                    $cart_item['data']->update_meta_data('_subscription_trial_length', $coupon_trial_length, true);
                                    $cart_item['data']->update_meta_data('_subscription_trial_period', $coupon_trial_period, true);
                                }
                            }
                        }
                    }
    
                }
    
            }
        }
  • The topic ‘Error with Multiple Coupons’ is closed to new replies.