Pawan Kumawat

WooCommerce: Check if Product Category is in the Cart

To check if a specific product category is in the cart during the `woocommerce_before_cart` action hook, you can use a similar approach as in the previous answer. However, in this case, you need to use the `woocommerce_before_cart_contents` action hook, which fires before the cart items are displayed on the cart page. Here’s how you can achieve this:

 

add_action('woocommerce_before_cart_contents', 'check_product_category_in_cart');

function check_product_category_in_cart() {
    $category_slug = 'your-category-slug'; // Replace 'your-category-slug' with the desired category slug
    $category_found = false;

    // Get the cart contents
    $cart_items = WC()->cart->get_cart();

    // Loop through the cart items
    foreach ($cart_items as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];
        $product = wc_get_product($product_id);

        // Check if the product belongs to the desired category
        if (has_term($category_slug, 'product_cat', $product_id)) {
            $category_found = true;
            break;
        }
    }

    if ($category_found) {
        // Category is in the cart, do something
        echo 'The category is in the cart.';
    } else {
        // Category is not in the cart, do something else
        echo 'The category is not in the cart.';
    }
}

 

Replace `’your-category-slug’` with the desired category slug you want to check. This code will run when the cart page is loaded, and it will check if any product in the cart belongs to the specified category. If the category is found in the cart, it will display “The category is in the cart.” If the category is not in the cart, it will display “The category is not in the cart.”

Remember to place this code snippet in your theme’s `functions.php` file or in a custom plugin to ensure it runs properly on your WooCommerce store.

Categories

Related Blogs

Elementor shortcodes

Elementor is a popular page builder plugin for WordPress that allows you to create and customize web pages using a drag-and-drop interface. Elementor provides a wide range of built-in widgets and elements, and you can also create your own custom elements using shortcodes.

Add Admin User in WordPress By FTP

Creating an admin user in WordPress through an FTP client is not a direct method since user accounts are managed through the WordPress database and not through files on the server. To add an admin user, you’ll need to use a different approach. Here’s how you can do it:

WooCommerce: Check if Product ID is in the Cart

To check if a specific product ID is in the cart in WooCommerce, you can use the `woocommerce_before_cart` action hook, which is fired before the cart page is displayed. Here’s how you can achieve this:

How to log information, warnings, errors In wordpress

To create a custom write_log() function for WordPress, you can use the error_log() function to write messages to the error log. This function will allow you to log information, warnings, errors, or any other messages during development or debugging.