Checking if items are in cart only works if logged in

Question

I have this code in my functions file to check if certain items are in the cart. Then on the cart page I list the actual ID’s and show something if they are in the cart. However, this code is only working if I am logged in but if logged out I don’t see what I should. How can I get it to work for logged out users as well?

// check if item in cart
 function woo_in_cart($arr_product_id) {
    global $woocommerce;
    $cartarray=array();

    foreach($woocommerce->cart->get_cart() as $key => $val ) {
       $_product = $val['product_id'];
       array_push($cartarray,$_product);
    }

    if (!empty($cartarray)) {
       $result = array_intersect($cartarray,$arr_product_id);
    }

    if (!empty($result)) {
       return true;
    } else {
       return false;
    };

}
0
user8463989 3 years 2020-08-24T06:10:49-05:00 0 Answers 40 views 0

Leave an answer

Browse
Browse