提问者:小点点

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


我从这个网站上找到了这个很棒的片段

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

        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
     }

谢谢。

来源


共3个答案

匿名用户

global$woocommerce$woocommerce-

下面是一个带有参数的自定义函数,该参数接受唯一整数产品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 
}

这段代码可以正常工作。活动子主题的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>';
}

匿名用户

也许更简单,首先我们在购物车中获得产品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_数组函数:

in_array(123, $product_ids);

对于不止一种产品:

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

匿名用户

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