Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: Check if items are already in cart

I found this great snippet from this website

The following is the function to check if a specific product exists in cart:

        function woo_in_cart($product_id) {
        global $woocommerce;         
        foreach($woocommerce->cart->get_cart() as $key => $val ) {
            $_product = $val['data'];

            if($product_id == $_product->id ) {
                return true;
            }
        }         
        return false;
        }

And this to use anywhere needed:

      if(woo_in_cart(123)) {
     // Product is already in cart
     }

The problem is how to use it to check multiple products like this:

      if(woo_in_cart(123,124,125,126...)) {
     // Product is already in cart
     }

Thanks.

source

like image 872
mysticalghoul Avatar asked Dec 21 '16 12:12

mysticalghoul


People also ask

How do I find my WooCommerce cart product ID?

You can use directly $product_id variable of the first item in cart. 2) Using an array of product IDs (one for each item in cart). To get the 1st item product ID: $products_ids_array[0]; To get the 2nd item product ID: $products_ids_array[1]; etc…

Is cart empty WooCommerce?

We can do our check by using the WC() global function and getting the cart contents count. While there are several ways we could check this, I prefer this method since it returns as an integer, so we simply need to check if it's equal to zero to determine if the cart is empty.

How do I get a cart quantity in WooCommerce?

You can also call more appropriate functions: foreach ( WC()->cart->get_cart() as $cart_item ) { $item_name = $cart_item['data']->get_title(); $quantity = $cart_item['quantity']; $price = $cart_item['data']->get_price(); ...

How do I find the cart page URL in WooCommerce?

Cart URLs are easily created from the WooCommerce Settings page. Simply enable Cart URLs, add a new Cart URL with the products you've previously determined. Save and share the cart link. The option to give each Cart URL a unique name and pretty permalink, (visible to the customer) is available.


2 Answers

Maybe something simpler, first we get product ids in cart :

$product_ids = array_merge(
  wp_list_pluck(WC()->cart->get_cart_contents(), 'variation_id'),
  wp_list_pluck(WC()->cart->get_cart_contents(), 'product_id')
);

Now if you want to check one product just simply use in_array function :

in_array(123, $product_ids);

and for more than one product:

array_intersect([123, 345, 567], $product_ids);
like image 197
Amin Avatar answered Oct 10 '22 14:10

Amin


global $woocommerce and $woocommerce->cart is outdated and simply replaced by WC()->cart

Here is a custom function with an argument that accepts a unique integer product ID or an array of product IDs, and that will return the number of matched Ids that are in cart.

The code handle any product type, including variable product and product variations:

function matched_cart_items( $search_products ) {
    $count = 0; // Initializing

    if ( ! WC()->cart->is_empty() ) {
        // Loop though cart items
        foreach(WC()->cart->get_cart() as $cart_item ) {
            // Handling also variable products and their products variations
            $cart_item_ids = array($cart_item['product_id'], $cart_item['variation_id']);

            // Handle a simple product Id (int or string) or an array of product Ids 
            if( ( is_array($search_products) && array_intersect($search_products, cart_item_ids) ) 
            || ( !is_array($search_products) && in_array($search_products, $cart_item_ids)
                $count++; // incrementing items count
        }
    }
    return $count; // returning matched items count 
}

This code goes in function.php file of your active child theme (active theme or in any plugin file).

Code is tested and works.


USAGE:

1) For a unique product ID (integer):

$product_id = 102;

// Usage as a condition in an if statement
if( 0 < matched_cart_items($product_id) ){
    echo '<p>There is "'. matched_cart_items($product_id) .'"matched items in cart</p><br>';
} else {
    echo '<p>NO matched items in cart</p><br>';
}

2) For an array of product IDs:

$product_ids = array(102,107,118);

// Usage as a condition in an if statement
if( 0 < matched_cart_items($product_ids) ){
    echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>';
} else {
    echo '<p>NO matched items in cart</p><br>';
}

3) For an array of product IDs for 3 or more matched cart items for example:

$product_ids = array(102, 107, 118, 124, 137);

// Usage as a condition in an if statement (for 3 matched items or more)
if( 3 <= matched_cart_items($product_ids) ){
    echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>';
} else {
    echo '<p>NO matched items in cart</p><br>';
}
like image 28
LoicTheAztec Avatar answered Oct 10 '22 14:10

LoicTheAztec