5

I'm trying to trigger an echo statement if a certain category of product is in my cart, here's my code:

<?php
//Check to see if user has product in cart
global $woocommerce;

//flag no book in cart
$item_in_cart = false;

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) {
                $_categoryid = $term->term_id;
            }

    if ( $_categoryid == 'name_of_category' ) {
        //book is in cart!
        $item_in_cart = true;

    }
}

if ($item_in_cart === true) {echo 'YES';}
else {echo 'Nope!';}

?>

Any idea as to what i'm doing wrong? I do have 'name_of_category' products in my cart, i'd like a nice Yes echoed!

Thanks!

Guillaume
  • 217
  • 1
  • 2
  • 11
  • Yes barrell, "Nope!" is echoed. Any idea what I'm doing wrong? – Guillaume Mar 22 '14 at 01:06
  • It seems that the 'Nope!' is echoed from the //flag no book in cart $item_in_cart declaration, when i hide that line i get an undefined variable error on the line if($item_in_cart === true) ... – Guillaume Mar 22 '14 at 01:19
  • I think you should move your `if($_categoryid == 'name_of_category')` statement into the `$terms` foreach loop – barell Mar 22 '14 at 09:53

2 Answers2

13

Edited my code following Barrell's advice and echo 'Bingo'!

Works like a charm, here's the code:

    function check_product_in_cart() {
        //Check to see if user has product in cart
        global $woocommerce;

        //assigns a default negative value
        //  categories targeted 17, 18, 19

        $product_in_cart = false;

        // start of the loop that fetches the cart items

        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );

            // second level loop search, in case some items have several categories
            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
                if (( $_categoryid === 17 ) || ( $_categoryid === 18 ) || ( $_categoryid === 19 )) {
                    //category is in cart!
                    $product_in_cart = true;
                }
            }
        }

        return $product_in_cart;
   }

Hope that can help someone!

Guillaume
  • 217
  • 1
  • 2
  • 11
  • Thank you very much. As of WooCommerce 3.0 it is ought to be `$_product->get_id()` instead of direct access via `$_product->id` – Dominik Späte Apr 09 '17 at 01:11
1

@Guillaume and others who helped- thanks for posting this for it was helpful to me. Once I started testing I realized that the code didn't work for all of my products. Some products in my case have categories with sub-categories and that was preventing the code from picking up the relevant categories on all the products. I altered your code slightly to create an array and it seems to be working well:

function check_product_in_cart() {
    //Check to see if user has product in cart
    global $woocommerce;

    // start of the loop that fetches the cart items

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );

        // second level loop search, in case some items have several categories
        // this is where I started editing Guillaume's code

        $cat_ids = array();

        foreach ($terms as $term) {
            $cat_ids[] = $term->term_id;
        }

        if(in_array(434, (array)$cat_ids) || in_array(435, (array)$cat_ids)) {

          //category is in cart!
           $product_in_cart = true;
        }
    }

    return $product_in_cart;
}
podoglyph
  • 31
  • 4