Skip to content

Instantly share code, notes, and snippets.

@rashmimalpande
Last active March 2, 2020 18:44
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 rashmimalpande/ecb5fcba474c5b6edcd1ac2972452cba to your computer and use it in GitHub Desktop.
Save rashmimalpande/ecb5fcba474c5b6edcd1ac2972452cba to your computer and use it in GitHub Desktop.
<?php
add_filter( 'woocommerce_coupons_enabled','ts_hide_coupon_field_on_cart' );
function ts_hide_coupon_field_on_cart( $enabled ) {
$product_id = 55;
$cart = WC()->cart->get_cart();
foreach ( $cart as $id => $cart_item ) {
if( $cart_item[ 'data' ]->get_id() == $product_id ) {
return false;
}
}
return $enabled;
}
@kevinbrown041
Copy link

Hi
I wonder if you can help? I'm trying to get this to work.
I want to only show the coupon box when the price is more than 19.99 AND its not product id 455821
I can't get this to work. Would you be able to give me any ideas why?

Thanks
Kevin

// START Remove coupon box when price is less than 19.99
add_filter( 'woocommerce_coupons_enabled','hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
	global $product;
//	$id = $product->get_id();
	$product_id = 455821;

	$price = "19.95";
	$cart = WC()->cart->get_cart();
		foreach ( $cart as $id => $cart_item ) {
		if( ($cart_item[ 'data' ]->get_price() <= $price) || ( $cart_item[ 'data' ]->$product->get_id() != $product_id ) ) {
			return false; // dont remove it 
		}
	}	
	

	return $enabled;
}

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