WooCommerce:检查商品是否已在购物车中 [英] WooCommerce: Check if items are already in cart

查看:32
本文介绍了WooCommerce:检查商品是否已在购物车中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个网站

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

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
     }

谢谢.

来源

推荐答案

global $woocommerce$woocommerce->cart 过时并简单地替换WC()->cart

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

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

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 
}

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

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

代码已经过测试并且可以正常工作.

Code is tested and works.

用法:

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

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

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

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

这篇关于WooCommerce:检查商品是否已在购物车中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆