Support WooCommerce Subscriptions

READ FIRST: This code needs to be added at the end of the functions.php file. If you are familiar with editing functions.php, continue reading. If you haven't done this before, adding this code in the wrong place might bring the site down and the only way to access it is by editing the functions.php file again via FTP. Feel free to contact us if you need help!

If you want to automatically apply all available coupons for customers during the automatic subscription renewal add this code to your functions.php file per:


  1. Log into your website
  2. Navigate to Appearance > Theme File Editor
  3. Select Theme Functions
  4. Add the code below at the end functions.php file

add_filter('wcs_new_order_created','genie_renewal_order_created', 10, 2 );

function genie_renewal_order_created($order, $subscription)

{

$genie_all_coupons = "yes";

$genie_exclude_shipping = "yes";

$user_id = $order->get_user_id();

$user_info = get_userdata($user_id);

$user_email = $user_info->user_email;

// Prevent empty emails.

if(empty($user_email)) {

return $order;

}

$order_total = $order->get_total() - $order->get_total_tax();

if(isset($genie_exclude_shipping) && $genie_exclude_shipping === "yes") {

$order_total = $order_total - $order->get_total_shipping() - $order->get_shipping_tax();

}

$args = array(

'posts_per_page' => 999,

'post_type' => 'shop_coupon',

'post_status' => 'publish',

'meta_query' => array(

'relation' => 'AND',

array(

'key' => 'customer_email',

'value' => $user_email,

'compare' => 'LIKE'

)

// Provjeriti da ne povuce kupone koji su istekli

)

);

$coupons = get_posts( $args );

if(empty($coupons)) {

return $order;

}

$availableCoupons = [];

// Get only coupons that havent been used yet:

foreach($coupons as $coupon) {

$couponUsage = get_post_meta($coupon->ID,'usage_count', true);

$usageLimit = get_post_meta($coupon->ID,'usage_limit', true);

if($couponUsage < $usageLimit || $usageLimit === '') {

array_push($availableCoupons, $coupon);

}

}

if(empty($availableCoupons)) {

return $order;

}

// If you want to apply all coupons, up to renewal price.

if(isset($genie_all_coupons) && $genie_all_coupons === "yes") {

$total_value = 0;

$amount = 0;

foreach ($availableCoupons as $coupon) {

$coupons_obj = new WC_Coupon($coupon->ID);

$total_value = $total_value + $coupons_obj->get_amount();

$amount += genie_renewal_order_get_discount($order, $coupons_obj);

if($total_value >= $order_total) {

break;

}

}

// Check to make sure discount is not bigger than order

if ( $amount > $order_total ) {

$amount = $order_total;

}

if($amount > 0) {

genie_renewal_order_apply_discount($order, $amount);

}

} else {

$coupons_obj = new WC_Coupon($availableCoupons[0]->ID);

genie_renewal_order_apply_coupon($order, $coupons_obj);

}

$order_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->id : $order->get_id();

do_action('new_loyal_genie_event', 'subscription_renewal', array('user' => $user_id, 'order' => $order_id) );

return $order;

}

function genie_renewal_order_apply_discount($order,$amount){

$item = new WC_Order_Item_Fee();

$item->set_props( array(

'name' => __("Referral applied","loyal-genie"),

'tax_class' => NULL,

'total' => -$amount,

'total_tax' => 0,

'tax_status'=> 'none',

'order_id' => $order->get_id(),

) );

$item->save();

$order->add_item( $item );

$order->update_taxes();

$order->calculate_totals( true );

}

function genie_renewal_order_get_discount($order, $coupons_obj)

{

$order_total = $order->get_total() - $order->get_total_tax();

if(isset($genie_exclude_shipping) && $genie_exclude_shipping === "yes") {

$order_total = $order_total - $order->get_total_shipping() - $order->get_shipping_tax();

}

$amount = $coupons_obj->get_amount();

$type = $coupons_obj->get_discount_type(); // fixed_cart or percent

$coupons_obj->set_usage_count($coupons_obj->get_usage_count() + 1);

$coupons_obj->save();

if( $type == "percent") {

$discount = $order_total * ( $amount / 100);

} else {

$discount = $amount;

}

return $discount;

}

function genie_renewal_order_apply_coupon($order, $coupons_obj)

{

$order_total = $order->get_total() - $order->get_total_tax();

if(isset($genie_exclude_shipping) && $genie_exclude_shipping === "yes") {

$order_total = $order_total - $order->get_total_shipping() - $order->get_shipping_tax();

}

$amount = $coupons_obj->get_amount();

$type = $coupons_obj->get_discount_type(); // fixed_cart or percent

$coupons_obj->set_usage_count($coupons_obj->get_usage_count() + 1);

$coupons_obj->save();

if( $type == "percent") {

$discount = $order_total * ( $amount / 100);

} else {

$discount = $amount;

}

// Check to make sure discount is not bigger than order

if ( $discount > $order_total ) {

$discount = $order_total;

}

$item = new WC_Order_Item_Fee();

$item->set_props( array(

'name' => __("Referral applied","loyal-genie"),

'tax_class' => NULL,

'total' => -$discount,

'total_tax' => 0,

'tax_status'=> 'none',

'order_id' => $order->get_id(),

) );

$item->save();

$order->add_item( $item );

$order->update_taxes();

$order->calculate_totals( true );

}


This code will then automatically add referral coupons that the user has not used yet once automatic subscription has been run. In case you want to only apply one coupon, change $genie_all_coupons to "no".

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us