WooCommerce:检查物品是否已经装入购物车

时间:2016-12-21 12:03:41

标签: php wordpress woocommerce cart product

我从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

4 个答案:

答案 0 :(得分:10)

   global $woocommerce $woocommerce->cart 过时,只需替换 WC()->cart

这是一个自定义函数,其参数接受唯一的整数产品ID或产品ID数组,并返回购物车中匹配的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文件中。

代码经过测试并有效。

<强> USAGE:

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

答案 1 :(得分:2)

案例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
    }
}

答案 2 :(得分:1)

也许更简单一些,首先我们在购物车中获取产品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);

答案 3 :(得分:0)

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