I have subscriptions on my WooCommerce site. I created a functionality that, if there is an active subscription, rewrites it to a new one upon purchase:
add_action( 'woocommerce_thankyou', 'cancel_previous_active_subscription' );
function cancel_previous_active_subscription() {
$no_of_loops = 0;
$user_id = get_current_user_id();
$args = array(
'subscription_status' => 'active',
'subscriptions_per_page' => -1,
'customer_id' => $user_id,
'orderby' => 'ID',
'order' => 'DESC'
);
$subscriptions = wcs_get_subscriptions($args);
foreach ( $subscriptions as $subscription ) {
$no_of_loops = $no_of_loops + 1;
if ($no_of_loops > 1){
$subscription->update_status( 'cancelled' );
}
}
}
Now we need to make sure that in the checkout, if there is a subscription, a notice is displayed. But only when the subscription is in checkout, not for other products. Right now, my code is not working. How to fix it?
add_action( 'woocommerce_before_cart', 'display_subscription_notice' );
function display_subscription_notice() {
$user_id = get_current_user_id();
$args = array(
'subscription_status' => 'active',
'subscriptions_per_page' => 1,
'customer_id' => $user_id,
);
$subscriptions = wcs_get_subscriptions($args);
$has_subscription = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( wcs_order_contains_subscription( $cart_item['data']->get_id() ) ) {
$has_subscription = true;
break;
}
}
if ( !empty($subscriptions) && $has_subscription ) {
wc_add_notice( 'By purchasing a new subscription you will cancel your current one', 'notice' );
}
}
I have a version of the working code, but it works for absolutely all products, and I only need it when purchasing a new subscription:
add_action('woocommerce_before_cart', 'display_subscription_notice');
function display_subscription_notice()
{
$user_id = get_current_user_id();
$args = array(
'subscription_status' => 'active',
'subscriptions_per_page' => 1,
'customer_id' => $user_id,
);
$subscriptions = wcs_get_subscriptions($args);
if (!empty($subscriptions)) {
wc_add_notice('By purchasing a new subscription you will cancel your current one', 'notice');
}
}
You can use the following to display a notice in checkout page when a subscription product is in cart and if the customer has already an active subscription:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
In checkout page (if the user has an active subscription and a subscription product in cart):
Addition
For your first function, you could try to use the following instead, that will cancel all previous user "active" subscriptions, when a new subscription get "active" status:
Code goes in functions.php file of your child theme (or in a plugin). It should work.
Related: