简体   繁体   English

Woocommerce - 将数量为 0 的商品添加到购物车?

[英]Woocommerce - Items with a quantity of 0 are added to the cart?

I have item quantities on the shop page next to each item.我在商店页面上每件商品旁边都有商品数量。 The user can enter their quantity and click the add to cart button, which will add the item to the cart using ajax.用户可以输入他们的数量并单击添加到购物车按钮,这将使用 ajax 将项目添加到购物车。 By default I have the item quantity set to 0;默认情况下,我将项目数量设置为 0; however if the user clicks the add to cart button when the quantity is 0, the item will be added to the cart with a quantity of 1.但是,如果用户在数量为 0 时单击添加到购物车按钮,则该商品将添加到购物车中,数量为 1。

What I want to happen is if the item quantity is 0 then a message will appear telling the user to enter their quantity and the item will not be added to the cart.我想要发生的是,如果商品数量为 0,则会出现一条消息,告诉用户输入他们的数量,并且商品不会添加到购物车中。

Is this possible?这可能吗? Any help would be appreciated.任何帮助,将不胜感激。

This should work, add This to your custom JS code.这应该有效,将 This 添加到您的自定义 JS 代码中。

jQuery( document ).ready(function() {
  jQuery(".woocommerce div.product form.cart .button").click(function(){
    if(jQuery('input[name="quantity"]').val() == 0 || 'input[name="quantity"]').val() == ""){
       alert("Quantity should be at least 1. The item will not be added to cart.");
       return false;
    }
  });
});

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

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