Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Plugin Name: All Products for WooCommerce Subscriptions - Disable Coupons When Cart contains products on Subscription
* Plugin URI: https://woocommerce.com/products/all-products-for-woocommerce-subscriptions/
* Description: Use this snippet to prevent coupons from applying in the cart if there is at least one product on Subscription there.
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'woocommerce_coupon_is_valid', 'apfs_disable_for_products_on_subscription', 10, 3 );
function apfs_disable_for_products_on_subscription( $is_valid, $coupon, $object ){
$cart_contents = WC()->cart->cart_contents;
foreach ( $cart_contents as $cart_item ) {
if ( ! empty( $cart_item[ 'wcsatt_data'][ 'active_subscription_scheme' ] ) ) {
$is_valid = false;
break;
}
}
return $is_valid;
}
@Leonard02

This comment has been minimized.

Copy link

@Leonard02 Leonard02 commented Nov 5, 2020

Hi, Any idea on how to apply that coupon code is added only to subscribed products and only to the first quantity of each product?? I will really appreciate if someone can help me.

@riadnassour

This comment has been minimized.

Copy link

@riadnassour riadnassour commented Mar 23, 2021

This snippet will work but, when applying a coupon manually in a new order using the Dashboard a "Coupon is not valid." error message will appear, any way to fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment