0

I'm trying to set notifications when I have products from these two different categories inside the card in WooCommerce.

This is the code which I using:

add_action( 'woocommerce_checkout_before_customer_details', 'webroom_check_if_product_category_is_in_cart' );
function webroom_check_if_product_category_is_in_cart() {

    $cat_in_cart = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) &&
             has_term( 'cat2', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            break;
        }
    }

    if ( $cat_in_cart ) {
        $notice = 'Notification';
        wc_print_notice($notice, 'notice');
    }
}

This code works perfectly if I only set one category, but when I set two categories, for some reason I don't have results nor errors.

0

Quickly, I thought about that :

(but it's not my best! Maybe I'll come rewrite a more thoughtful code sometimes but for now, this code'll work)

add_action( 'woocommerce_before_cart', 'webroom_check_if_product_category_is_in_cart' );
function webroom_check_if_product_category_is_in_cart() {

    $cat1_in_cart = false;
    $cat2_in_cart = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
            $cat1_in_cart = true;
        } elseif(has_term( 'cat2', 'product_cat', $cart_item['product_id'] )){
            $cat2_in_cart = true;
        }
    }

    if ($cat1_in_cart === true && $cat2_in_cart === true) {
        $notice = 'Notification';
        wc_print_notice($notice, 'notice');
    }
}
  • Great! I've just a re-edited a bit your code and it works. – batman Nov 22 at 22:52
0

Here is the solution:

add_action( 'woocommerce_before_cart', 'webroom_check_if_product_category_is_in_cart' );
function webroom_check_if_product_category_is_in_cart() {

    $cat1_in_cart = false;
    $cat2_in_cart = false;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
            $cat1_in_cart = true;
        } elseif(has_term( 'cat2', 'product_cat', $cart_item['product_id'] )){
            $cat2_in_cart = true;
        }
    }

    if ($cat1_in_cart === true && $cat2_in_cart === true) {
        $notice = 'Notification';
        wc_print_notice($notice, 'notice');
    }
}
  • Please, could you indicate that my solution helped you by marking it with the green check and not republishing my solution? – Sam Nov 23 at 0:08
  • @Sam First re-edit your code so he could work. Switch woocommerce_checkout_before_customer_details with woocommerce_before_cart and correct the typos inside the code, and I will for sure. – batman Nov 23 at 9:36
  • Yes! Sorry bro! I am dyslexic haha! I looked and re-watch and I never realized this change! Sorry! – Sam Nov 23 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.