Repository Forums Support WooCommerce Extended Coupon Features PRO Apply Coupon To Exisitng Subscriber

Topic Resolution: Resolved
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #15571
    Heidi Maskelyne
    Participant

    is there a way to apply a coupon to an already ongoing subscription so the subscriber on there next renewal date gets the applied auto coupon discount ?

    #15572
    Soft79
    Keymaster

    I don’t know, we don’t use the subscriptions plugin.

    #15576
    Angela Mauricette
    Participant

    Hi, For some reason the plugin cannot be installed into my website…

    this is what I got for message:
    Unpacking the package…

    Installing the plugin…

    The package could not be installed. No valid plugins were found.

    Plugin installation failed.

    #15577
    Angela Mauricette
    Participant

    Hi, For some reason the plugin cannot be installed into my website…

    this is what I got for message:
    Unpacking the package…

    Installing the plugin…

    The package could not be installed. No valid plugins were found.

    Plugin installation failed.

    #15578
    Soft79
    Keymaster

    Hi Angela,

    Strange, I can see that you downloaded the right file. You should have created a new topic instead of ‘hijacking’ someone elses unrelated thread on this forum, but I will answer the email you sent me.

    #15615
    Heidi Maskelyne
    Participant

    Hi again ,

    I know we can hook into renewals below code which is good but how can I check through all coupons to see if the subscriber has a valid coupon to use ?

    function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) {
    
        $order           = new WC_Order( $renewal_order->get_id() );
        $coupon_amount   = 25;
        $order->set_total( $order->get_total() - $coupon_amount );
        return $order;
    }
    
    add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
    #15616
    Soft79
    Keymaster

    To see how our plugin does that, you can take a peek at the get_valid_auto_coupons() function in includes/plugins/WJECF_Autocoupon.php .

    #15618
    Heidi Maskelyne
    Participant

    thank you for your help

    for anyone that needs this functionality you can adjust the code below to suit your needs

    // discount subscription renewals
    
    function discount_subscription_renewals($billing_email){
    
    	$query_args = array(
    	'posts_per_page' => -1,
    	'post_type'      => 'shop_coupon',
    	'post_status'    => 'publish',
    	'orderby'        => array( 'title' => 'ASC' ),
    	'meta_query'     => array(
    						array(
    							'key'     => 'discount_type',
    							'value'   => 'fixed_cart',
    						),
    					),
    	);
    	
    	$coupons = get_posts( $query_args );
    	$coupon_code = false;
    	$discount   = 0;
    	foreach ( $coupons as $coupon ) {
    	
    		$customer_email = array($coupon->customer_email[0]);
    
    		$amount       =  $coupon->coupon_amount;
    		$usage_count  =  $coupon->usage_count;
    		$usage_limit   =  $coupon->usage_limit;
    		
    	
    		
    		if (in_array($billing_email,$customer_email) && ($usage_count < $usage_limit)){
    		  
    		  $coupon_code = $coupon->post_title;
    		  break;
    		  //$discount   += $amount;
    		}  
    	
    	}   
        return $coupon_code;
    }
    
    function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) {
    
        $order           = new WC_Order($renewal_order->get_id() );
        $billing_email   = $order->get_billing_email();
        $coupon_code   = discount_subscription_renewals($billing_email);
        if ($coupon_code){
    	    $order->apply_coupon($coupon_code);
            //$order->set_total( $order->get_total() - $coupon_amount );
    	}    
        return $order;
    }
    
    add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.