How to Filter Products Based on Categories on the Cart Page?

Yes, We can do that but we need more details like your site URL and by applying the below steps need to check it.

Step 1: Identify the Current Cart Page
You can use is_page() or is_cart() to identify which cart page is being displayed, then filter the cart contents based on the category.

For the Basket page (Rental products only):
Use the cart page ID or the custom page slug for the Basket page.
For the Cart-Purchase page (Purchase products only):
Similarly, use the cart page ID or the custom page slug for the Cart-Purchase page.

Step 2: Add Custom Code in functions.php

function filter_cart_products_by_category() {
    // Check if we are on the Basket page
    if (is_page('basket')) { // Change 'basket' to your Basket page slug or use page ID if needed
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            $product = $cart_item['data'];
            $terms = get_the_terms($product->get_id(), 'product_cat');
            
            if ($terms) {
                // Check if product is not in the "Rental" category
                $show_product = false;
                foreach ($terms as $term) {
                    if ($term->slug == 'rental') { // Change 'rental' to your Rental category slug
                        $show_product = true;
                        break;
                    }
                }
                
                // If product is not "Rental", remove from cart
                if (!$show_product) {
                    WC()->cart->remove_cart_item($cart_item_key);
                }
            }
        }
    }

    // Check if we are on the Cart-Purchase page
    if (is_page('cart-purchase')) { // Change 'cart-purchase' to your Cart-Purchase page slug or use page ID if needed
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            $product = $cart_item['data'];
            $terms = get_the_terms($product->get_id(), 'product_cat');
            
            if ($terms) {
                // Check if product is not in the "Purchase" category
                $show_product = false;
                foreach ($terms as $term) {
                    if ($term->slug == 'purchase') { // Change 'purchase' to your Purchase category slug
                        $show_product = true;
                        break;
                    }
                }
                
                // If product is not "Purchase", remove from cart
                if (!$show_product) {
                    WC()->cart->remove_cart_item($cart_item_key);
                }
            }
        }
    }
}
add_action('woocommerce_before_cart', 'filter_cart_products_by_category');
add_action('woocommerce_before_checkout_form', 'filter_cart_products_by_category'); 

Step 3: Customize Your Category Slugs
Make sure to replace ‘rental’ and ‘purchase’ in the code with the correct slugs of your categories. You can find these slugs in your WordPress admin panel under Products > Categories.

Step 4: Additional Styling and User Experience

If you want to display a message on each cart page informing users about category restrictions (e.g., “Only rental products are allowed in the Basket”), you can use the following code:

function display_cart_page_message() {
    if (is_page('basket')) {
        echo '<p>Your basket contains only rental products.</p>';
    } elseif (is_page('cart-purchase')) {
        echo '<p>Your cart contains only purchase products.</p>';
    }
}
add_action('woocommerce_before_cart', 'display_cart_page_message');

Testing

  • Go to the Basket page and add purchase products to the cart. They should be removed.
  • Go to the Cart-Purchase page and add rental products to the cart. They should be removed.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)