Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Created February 7, 2024 05:48
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 webtoffee-git/b53a0f29f750ad4b809c693aa8dc9bdb to your computer and use it in GitHub Desktop.
Save webtoffee-git/b53a0f29f750ad4b809c693aa8dc9bdb to your computer and use it in GitHub Desktop.
Remove the coupon field on adding products from specific categories - By WebToffee
<?php //Do not copy this line of code
add_filter( 'woocommerce_cart_item_class', function($excisting_class, $cart_item, $cart_item_key ){
$product = $cart_item['data'];
$categories = $product->get_category_ids();
$prefix = "wt_sc_product_category_";
$categories = array_map(function($element) use ($prefix) {
return $prefix . $element;
}, $categories);
$categories = implode(" ", $categories);
return $excisting_class . ' ' . $categories;
}, 10, 3 );
add_action( 'wp_footer', function(){
?>
<script>
var restricted_array = [45, 55];
restricted_array = restricted_array.map(item => 'wt_sc_product_category_' + item);
function check_category_exist(){
var found = false;
jQuery('table.shop_table tr').each(function() {
for (var i = 0; i < restricted_array.length; i++) {
if (jQuery(this).hasClass(restricted_array[i])) {
jQuery('table.cart td.actions .coupon').hide();
jQuery('.woocommerce-form-coupon-toggle').hide();
found = true;
break;
}
}
});
if(!found){
jQuery('.shop_table .actions div.coupon').show();
jQuery('.woocommerce-form-coupon-toggle').show();
}
}
jQuery(document).ready(function(){
check_category_exist();
});
jQuery( document.body ).on( 'removed_from_cart updated_wc_div', function() {
check_category_exist();
});
</script>
<?php
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment