WooCommerce:检查商品是否已在购物车中

WooCommerce: Check if items are already in cart

我从 website

中找到了这个很棒的片段

以下是检查购物车中是否存在特定产品的函数:

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

这可以在任何需要的地方使用:

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

问题是如何使用它来检查多个产品,如下所示:

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

谢谢。

source

案例 1:将数组作为参数传递。

 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;

        }

如何调用函数

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

情况 2:使用您 运行.

的代码
$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
    }
}

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

这是一个自定义函数,其参数接受唯一的整数产品 ID 或产品 ID 数组,这将 return 购物车中匹配 ID 的数量。

代码处理任何产品类型,包括可变产品和产品变体:

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 
}

此代码位于您的活动子主题(活动主题或任何插件文件)的 function.php 文件中。

代码已经过测试并且可以工作。


用法:

1) 对于唯一的产品 ID(整数):

$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) 对于产品 ID 数组:

$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) 对于 3 个或更多匹配的购物车项目的产品 ID 数组,例如:

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

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

}

这里是一个用法示例:

//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 :(';
}

也许更简单,首先我们在购物车中获取产品 ID:

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

现在,如果您想要检查一种产品,只需使用 in_array 函数即可:

in_array(123, $product_ids);

对于不止一种产品:

array_intersect([123, 345, 567], $product_ids);