简体   繁体   English

覆盖购物车中现有产品的产品数量 -Woocommerce

[英]override the product quantity of existing already product in cart -Woocommerce

I have tried a lot to override the existing product quantity in the cart but nothing.我已经尝试了很多来覆盖购物车中现有的产品数量,但没有。

Actually I have this code:其实我有这个代码:

add_action('woocommerce_add_to_cart', 'add_to_cart_qty', 10, 6 );
function add_to_cart_qty( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){

    $real_product_id = $variation_id > 0 ? $variation_id : $product_id;

    $product = wc_get_product($real_product_id);
    $product_stock = $product->get_stock_quantity();

    // Zero or negative stock (remove the product)
    if( $product_stock <= 0 && $product->get_manage_stock() ){
        WC()->cart->remove_cart_item( $cart_item_key );
        return;
    }

    if( $quantity > $product_stock && $product->get_manage_stock() ){
        WC()->cart->set_quantity( $cart_item_key, $product_stock );
    }
}

that sets the maximum available product quantity when product is added to cart.设置将产品添加到购物车时的最大可用产品数量。 But I will need also to act in cart page when customer change the item quantities…但是,当客户更改商品数量时,我还需要在购物车页面中进行操作……

I think we can handle this issue in two ways:我认为我们可以通过两种方式处理这个问题:

  • first either we remove the product on add to the cart of the same product which is already in cart首先,我们将产品添加到购物车中已存在的同一产品的购物车中
  • second way is update the quantity of existing product on add to the cart of the same product, and this should also work for variable products.第二种方法是在添加到同一产品的购物车时更新现有产品的数量,这也适用于可变产品。

I have tried a lot of different snippet codes for this but no results.我为此尝试了很多不同的片段代码,但没有结果。

Any help will be appreciated.任何帮助将不胜感激。

Using this custom function hooked in woocommerce_after_cart_item_quantity_update action hook, will avoid customer to add more than the product stock quantity when updating cart item quantity:使用钩在woocommerce_after_cart_item_quantity_update动作钩子中的这个自定义函数,将避免客户在更新购物车项目数量时添加超过产品库存数量:

add_action('woocommerce_after_cart_item_quantity_update', 'update_cart_items_quantities', 10, 4 );
function update_cart_items_quantities( $cart_item_key, $quantity, $old_quantity, $cart ){
    $cart_data = $cart->get_cart();
    $cart_item = $cart_data[$cart_item_key];
    $manage_stock = $cart_item['data']->get_manage_stock();
    $product_stock = $cart_item['data']->get_stock_quantity();

    // Zero or negative stock (remove the product)
    if( $product_stock <= 0 && $manage_stock ){
        unset( $cart->cart_contents[ $cart_item_key ] );
    }
    if( $quantity > $product_stock && $manage_stock ){
        $cart->cart_contents[ $cart_item_key ]['quantity'] = $product_stock;
    }
    return $product_stock;
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.代码位于活动子主题(或活动主题)的 function.php 文件或任何插件文件中。

This code is tested and works even for product variations.此代码经过测试,甚至适用于产品变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM