kingtommy dot com

MeetCritique FullSend memberships excluded from coupon discounts

Client project · Excerpts from the live MeetCritique code

No coupon should discount a FullSend membership. The obvious filter is not enough, because of one condition in WooCommerce core.

WC_Discounts::get_items_to_apply_coupon() skips an item only when the coupon is invalid for the product and invalid for the cart. For fixed_cart coupons, is_valid_for_cart() is always true, so a product-level rule alone is bypassed.

A second filter, on woocommerce_coupon_is_valid_for_cart, turns the cart check off, only for carts that contain a FullSend item. WooCommerce then judges each item separately, and the product filter lets every other item in that cart keep the discount. Carts without a membership behave as before.

The product filter functions.php:5295-5296, 5303-5316
add_filter( 'woocommerce_coupon_is_valid_for_product', 'mc_exclude_fullsend_from_coupons', 99, 4 );
function mc_exclude_fullsend_from_coupons( $is_valid, $product, $coupon, $cart_item ) {
// …
	if ( mc_is_fullsend_product( $product ) ) {
		return false;
	}

	// Cart-level coupon (fixed_cart) in a cart holding FullSend: the filter
	// below has switched is_valid_for_cart() off to force per-item evaluation,
	// so the remaining items must be allowed explicitly or the coupon would
	// discount nothing at all.
	if ( ! $coupon->is_type( wc_get_product_coupon_types() ) && mc_cart_contains_fullsend() ) {
		return true;
	}

	return $is_valid;
}
The cart filter functions.php:5322-5334
add_filter( 'woocommerce_coupon_is_valid_for_cart', 'mc_fullsend_blocks_cart_level_coupons', 99, 2 );
function mc_fullsend_blocks_cart_level_coupons( $is_valid, $coupon ) {
	if ( ! $is_valid || ! $coupon instanceof WC_Coupon ) {
		return $is_valid;
	}
	if ( mc_coupon_allowed_on_fullsend( $coupon ) ) {
		return $is_valid;
	}
	if ( ! mc_cart_contains_fullsend() ) {
		return $is_valid;
	}
	return false;
}

All demos