php - WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

标签 php wordpress woocommerce cart product-quantity

如果购物车中已存在该产品,我想增加 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
    }
}

代码位于子主题的functions.php 文件中(或插件中)。经过测试并有效。


添加 (基于OP评论):

以下内容将针对特定的定义产品。如果特定产品已在购物车中,请将数量增加一个(在下面定义您的产品 ID):

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

代码位于子主题的functions.php 文件中(或插件中)。经过测试并有效。

关于php - WooCommerce:如果购物车中已存在产品,则增加购物车商品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76987300/

相关文章:

wordpress - 如何在 WooCommerce 中使用自定义订单状态获取立即付款 URL?

php - 如果运输方式是 Woocommerce 中的本地取货,则需要填写订单备注

php - Woocommerce Rest api 产品过滤器不起作用

php - 在 & 但不是 && 处拆分字符串

javascript - 数据更改时更新页面

javascript - 为什么我的 Javascript 警报无法在此自定义 WordPress 插件上运行?

wordpress - 使用 woocommerce 为 SEO 实现平面 URL 结构

php - 一个大查询还是几个小查询?

php - xampp 目录中不存在 Sendmail 文件夹?

php - 在 WooCommerce 3 中获取订单发货项目详细信息