Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save somewherewarm-snippets/1cfa3dd57f2348e7515d8517044dd564 to your computer and use it in GitHub Desktop.
Save somewherewarm-snippets/1cfa3dd57f2348e7515d8517044dd564 to your computer and use it in GitHub Desktop.
<?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
Copy link

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
Copy link

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?

@ciitech
Copy link

ciitech commented Aug 9, 2022

@somewherewarm-snippets

This code works but when you want to create a manual order form the backend it gives an error when adding a coupon.

Any ideas?

Thanks

unnamed

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