Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
@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