1

I have started a referral scheme which gives users a £25 discount coupon code which works great but not for pre-existing subscribers how can I hook into renewals and apply the auto coupon so subscriptions can benefit from the discount too.?

Can I achieve this with woocommerce_new_order?

EDIT:

UPDATING POST TO HELP OTHERS CODE I USED IN THE END TO UTILISE EXISTING SUBSCRIBERS UTILISING COUPONS BELOW:

// 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 );
Mike
  • 45
  • 5
  • Hi Mike, could you please help me implement this? – James Deadman Nov 05 '20 at 10:18
  • Just copy above code and add it to your functions.php file in your theme folder – Mike Nov 06 '20 at 12:45
  • Thanks Mike, appreciate the comment back. But where do they then apply the coupon code? Apologies for my lack of knowledge. – James Deadman Nov 09 '20 at 11:46
  • They don't need to apply it. If you add a coupon for someone specific, if it's their coupon it will just auto apply it when there subscription renews – Mike Nov 10 '20 at 13:00
  • FWIW this no longer works in 2021. However I think it's because get_billing_email is no longer used. Instead use: $order->billing_email; – user2115227 Mar 12 '21 at 09:08

1 Answers1

2
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 );
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Now I just need a way to loop through all coupons and if valid for that customer apply it any help with that ? – Mike May 15 '20 at 16:51