WooCommerce - Check if item's are already in c

2019-01-24 23:54发布

问题:

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

回答1:

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:

function matched_cart_items($product_ids) {

    if(!WC()->cart->is_empty()):

        // Initialise the count
        $count = 0;

        foreach(WC()->cart->get_cart() as $cart_item ):

            $items_id = $cart_item['product_id'];

            // For an array of product IDS
            if(is_array($product_ids) && in_array($items_id, $product_ids))
                $count++; // incrementing the counted items

            // for a unique product ID (integer or string value)
            if($product_ids == $items_id)
                $count++; // incrementing the counted items

        endforeach;

        // returning counted items 
        return $count;

    endif;
}

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_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>';
}

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>';
}


回答2:

Case 1 : Pass Array As an argument.

 function woo_in_cart($arr_product_id) {
        global $woocommerce;         
        $cartarray=array();

        foreach($woocommerce->cart->get_cart() as $key => $val ) {
            $_product = $val['data'];
            array_push($cartarray,$_product->id);
        }         
        $result = !empty(array_intersect($cartarray,$arr_product_id));
        return $result;

        }

How to call function

$is_incart=array(2,4,8,11);
print_r(woo_in_cart($is_incart));

Case 2 : Use Code that you run.

$is_in_product_cart=array(123,124,125,126,..);

foreach($is_in_product_cart as $is_in_cart ) 
    if(woo_in_cart($is_in_cart))
    {
        // Product is already in cart
    }
}


回答3:

There was a mistake in the woo_in_cart function. Here the correct one:

 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;
    };

}

Here an example of usage:

//Set IDs Array variable

$my_products_ids_array = array(22,23,465);
if (woo_in_cart($my_products_ids_array)) {
  echo 'ohh yeah there some of that products in!';
}else {
  echo 'no matching products :(';
}