简体   繁体   English

Woocommerce将购物车中特定产品的数量相乘

[英]Woocommerce multiply the quantity of a specific product in cart

Is there any way to multiply the quantity of specific product in cart? 有什么办法可以增加购物车中特定产品的数量?

Lets say I have multiple products in the cart and one of the products has quantity "3", I want to multiply the quantity with "5" so [3*5] and update the quantity field to "15". 假设我的购物车中有多个产品,其中一个产品的数量为“ 3”,我想将数量乘以“ 5”,所以[3 * 5]并将数量字段更新为“ 15”。

I want to do it for only one specific product in cart and not for all. 我只想为购物车中的一种特定产品而不是全部都这样做。

It depends where and when you want to change it. 这取决于您要在何时何地进行更改。

I can think of two different approaches: 我可以想到两种不同的方法:

PHP: PHP:

$woocommerce->cart->set_quantity( product_id , product_quantity );

JS + AJAX: JS + AJAX:

You can find a working solution here, just try to adapt it to your needs: 您可以在这里找到可行的解决方案,只需尝试使其适应您的需求即可:

WooCommerce - change QTY triggers AJAX call in cart WooCommerce-更改数量触发购物车中的AJAX呼叫

EDIT: 编辑:

I've coded a simple jQuery example. 我已经编写了一个简单的jQuery示例。 You can do this on real time while user inputs the quantity in the cart amount input. 当用户在购物车数量输入中输入数量时,您可以实时执行此操作。 The user inputs any quantity and when he/she clicks outside or leaves the input focus the amount is automatically changed (x5). 用户输入任何数量,当他/她单击外部或离开输入焦点时,数量将自动更改(x5)。

In order to use this you have to edit the cart template in Wordpress. 为了使用此功能,您必须在Wordpress中编辑购物车模板。 I would strongly recommend using a child theme and following the Woocommerce template best practices . 我强烈建议使用儿童主题并遵循Woocommerce模板最佳实践

 $("#input-value").focusout(function() { var value = parseFloat($(this).val()); $(this).val(value*5); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> Quantity: <input type="text" id="input-value" /> 

In order to aim for only a specific product you an use some PHP in your cart template to add a 'multiply' class to the quantity container: 为了仅针对特定产品,您可以在购物车模板中使用一些PHP在数量容器中添加“乘”类:

<?php if($_product->id == your_id) {
?>
    <td class="product-quantity multiply" data-title="<?php _e( 'Quantity', 'woocommerce' ); ?>">
<?php
}else{
?>
    <td class="product-quantity" data-title="<?php _e( 'Quantity', 'woocommerce' ); ?>">
<?php
}
?>

    <?php
        if ( $_product->is_sold_individually() ) {
            $product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
        } else {
            $product_quantity = woocommerce_quantity_input( array(
                'input_name'  => "cart[{$cart_item_key}][qty]",
                'input_value' => $cart_item['quantity'],
                'max_value'   => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
                'min_value'   => '0'
            ), $_product, false );
        }

        echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
    ?>
</td>

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

相关问题 将定制产品价格乘以 WooCommerce 购物车中的数量 - Multiply custom product price by the quantity in WooCommerce cart 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category WooCommerce 购物车上的目标特定产品 ID 显示数量 - Target specific product IDs on WooCommerce cart displayed quantity WooCommerce 中特定产品标签 ID 的购物车批量折扣 - Cart bulk quantity discount for specific product tag IDs in WooCommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 根据 WooCommerce 购物车小计自动添加可变数量的特定产品 - Auto add a variable quantity of specific product based on WooCommerce cart subtotal 根据 Woocommerce 中的产品数量替换特定的购物车项目 - Replace specific cart item based on product quantity in Woocommerce WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce 如何从购物车页面中删除特定产品属性WooCommerce的数量字段 - How to remove the quantity field from cart page for specific product attribute WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM