我想在WooCommerce的购物车页面上增加产品的数量,如果该产品已经存在于购物车上.

我试过这段代码

if ( 'same_product_added_to_cart' === $customer_gets_as_free ) {
   foreach ( $main_product_id as $main_product_single ) {
        $main_product_single = intval( $main_product_single );
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( $cart_item['product_id'] === $main_product_single ) {
        // Get the current quantity
        $current_quantity = $cart_item['quantity'];
        // Increase the quantity by one
        $new_quantity = $current_quantity + 1;

        // Update the cart with the new quantity
        WC()->cart->set_quantity( $cart_item_key, $new_quantity );

        break; // Exit the loop since we have found our product
       }
       }
   }
}

它没有起作用,相反,它触发了无限数量的循环,从而导致错误.我在这里做错了什么.add_to_cart()函数也会给出相同类型的错误.

推荐答案

通常,如果没有不同的定制购物车项目数据添加到添加到购物车上的购物车项目,WooCommerce会自己执行此操作.

您的代码中存在错误和缺失的步骤.

以下是合并重复产品(购物车项目)的方法:

add_action( 'woocommerce_before_calculate_totals', 'merge_duplicated_products_in_cart');
function merge_duplicated_products_in_cart( $cart ) {
    if ((is_admin() && !defined('DOING_AJAX')))
        return;

    if (did_action('woocommerce_before_calculate_totals') >= 2)
        return;

    $items_data = $item_update = []; // initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['data']->get_id();
        $quantity   = $cart_item['quantity'];

        // Check if the product exists
        if( in_array($product_id, array_keys($items_data)) ) {
            // Same product found
            $item_update['item_qty'] = $items_data[$product_id]['qty'] + $quantity; // Set cumulated quantities
            $item_update['item_key'] = $items_data[$product_id]['key']; // Add first product item key
            $item_update['item_remove'] = $cart_item_key; // Add current item key (product to be removed)
            break; // Stop the loop
        } 
        // Add product_id, cart item key and item quantity to the array (for each item)
        else {
            $items_data[$product_id] = array(
                'key' => $cart_item_key,
                'qty' => $quantity
            );
        }
    }
    unset($items_data); // delete the variable

    if ( ! empty($item_update) ) {
        $cart->remove_cart_item($item_update['item_remove']); // remove last item (same product)
        $cart->set_quantity($item_update['item_key'], $item_update['item_qty']); // Update quantity on first item(same product)
        unset($item_update); // delete the variable
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.


Addition,(based on the OP comment):

以下内容将针对特定定义的产品(S).如果特定产品已经在购物车中,则将数量增加1(define your product(s) ID(s) below):

add_action( 'woocommerce_before_calculate_totals', 'merge_duplicated_products_in_cart');
function merge_duplicated_products_in_cart( $cart ) {
    if ((is_admin() && !defined('DOING_AJAX')))
        return;

    if (did_action('woocommerce_before_calculate_totals') >= 2)
        return;

    $targeted_product_ids = array(18); // <== HERE set your(s) targeted product(s) ID(s)

    $items_data = $item_update = []; // initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['data']->get_id();

        // Look for specific products only
        if ( in_array($product_id, $targeted_product_ids) ) {
            $quantity   = $cart_item['quantity'];

            // Check if the product is already in cart
            if( in_array($product_id, array_keys($items_data)) ) {
                // Same product found
                $item_update['item_qty'] = $items_data[$product_id]['qty'] + 1; // ADD ONE TO THE QUANTITY
                $item_update['item_key'] = $items_data[$product_id]['key']; // Add first product item key
                $item_update['item_remove'] = $cart_item_key; // Add current item key (product to be removed)
                break; // Stop the loop
            } 
            // Add product_id, cart item key and item quantity to the array (for SPECIFIC items only)
            else {
                $items_data[$product_id] = array(
                    'key' => $cart_item_key,
                    'qty' => $quantity
                );
            }
        }
    }
    unset($items_data); // delete the variable

    if ( ! empty($item_update) ) {
        $cart->remove_cart_item($item_update['item_remove']); // remove last item (same product)
        $cart->set_quantity($item_update['item_key'], $item_update['item_qty']); // Update quantity on first item(same product)
        unset($item_update); // delete the variable
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

Laravel 10 -扩展现有的artisan命令?

如何在php中生成包含第100秒的时间序列?

WooCommerce我的帐户:从帖子作者处获取自定义帖子类型帖子ID

WooCommerce-在选定类别中每消费X个金额即可添加免费礼物

来自POST请求的(无效)json字符串中的奇怪字符(编码问题)

如何在UML类图中可视化变量引用数组

限制联系人页面仅允许在WooCommerce中登录的用户

Laravel 10.x在到达控制器之前将查询参数强制转换为正确的类型

添加不存在的订单元数据以扩展WooCommerce管理订单搜索

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

为什么从一个页面到另一个页面都找不到 JQuery,因为它们使用相同的 twig 模板?

无法在 Laravel 中实例化抽象类 ProductAbstract

如何使用不同的方法编写嵌套连接查询并将两列连接成一列在 laravel 查询生成器中

根据购买产品的自定义字段替换WooCommerce添加到购物车按钮

$_SERVER 超全局变量在 PHP 中是否保证可写?

在WooCommerce我的账户单个订单页面中添加订单商品部分和按钮

PHP:如何将多列sql查询结果转换成数组

如何从 PHP 中的 .txt 文件中列出今天生日的人的姓名?

如何获取用户之间的最新消息列表

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表