Reputation: 127
I have a product category in Woocommerce called 'required' and any product in this category has to be ordered with each order. Mainly they are legal fees.
Is there a way to prevent those products from being removed from the cart? The problem is some of them have a qty, they can order 1 or more, but at least 1... the qty box in the cart allows 0 and there's a remove from cart X.
I have a loop over the cart contents and can see each cart's categories... but how do I prevent it from being removed or qty set to 0?
add_action( 'woocommerce_check_cart_items', 'gs_set_min_qty_per_product' );
function gs_set_min_qty_per_product() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
if ($count > 0) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
if (strtolower($term->name) == 'required') {
}
}
}
}
}
I don;t want them to be able to remove it and then tell them they cannot proceed without adding it back in, because the way our shoping cart is built in steps, they would have to start over.
Is there a way to intercept the remove_from-cart() hook and not allow anything from that category to be removed?
Upvotes: 1
Views: 2028
Reputation: 26319
Well you can filter woocommerce_cart_item_remove_link
to remove the link from the cart. Something like this may help. But if someone knew the link they could still manually enter it.
add_filter( 'woocommerce_cart_item_remove_link', 'so_38622032_remove_link', 10, 2 );
function so_38622032_remove_link( $link, $cart_item_key ){
if( WC()->cart->find_product_in_cart( $cart_item_key ) ){
$cart_item = WC()->cart->cart_contents[ $cart_item_key ];
$product_id = $cart_item['product_id'];
if( has_term( 'required', 'product_cat', $product_id ) ){
$link = '';
}
}
return $link;
}
Upvotes: 1