javascript - WooCommerce 加/减数量。产品不更新
问题描述
我试图让加/减工作,但由于某种原因它不是。当我单击减号或加号时,购物车页面正在重新加载,但产品数量保持不变。为什么这行不通?
如果我在输入字段中手动更新金额它可以工作(Ajax,没有重新加载页面)但加号和减号不会更新它。
我的自定义循环/cart.php:
if($_product->is_sold_individually()){
$product_quantity = sprintf('1 <input type="hidden" name="update_cart[%s][qty]" value="1" />', $cart_item_key);
echo apply_filters('woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item);
}else{
$terms = get_the_terms($product_id, 'product_cat');
$maxOrderAmounts = [];
foreach($terms as $term){
$categoryMaxOrderAmount = get_field('category_max_order_amount', $term);
if(!empty($categoryMaxOrderAmount)){
$maxOrderAmounts[] = $categoryMaxOrderAmount;
}
}
$min = get_min_order_quantity($_product);
$productStock = $_product->get_manage_stock() ? $_product->get_stock_quantity(): 999;
if(!empty($maxOrderAmounts) && $maxOrderAmounts < $productStock){
$max = intval(max($maxOrderAmounts));
}else{
$max = $productStock;
}
if($min >= $max){
$min = $max;
}
$product_quantity = woocommerce_quantity_input(array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $max,
'min_value' => $min,
), $_product, false);
if($min != $max){
echo "<div class=\"item-minus\"><div class=\"item-minus-in\">-</div></div>";
echo $product_quantity;
echo "<div class=\"item-plus\"><div class=\"item-plus-in\">+</div></div>";
}else{
echo $product_quantity;
}
}
剧本:
jQuery('body').on('click', '.item-amount .item-minus, .item-amount .item-plus', function () {
var theButton = jQuery(this);
var theInput = theButton.parents('.item-amount').find("input");
var oldValue = theInput.val();
var min = theInput.attr('min');
var max = theInput.attr('max');
if (typeof min === 'undefined') {
min = 0;
}
if (typeof max === "undefined") {
max = 999;
}
if (theButton.hasClass('item-plus')) {
var newVal = parseFloat(oldValue) + 1;
} else {
if (oldValue > 0) {
var newVal = parseFloat(oldValue) - 1;
} else {
newVal = 0;
}
}
// if(newVal <= max && newVal >= min){
theInput.val(newVal).trigger('change');
// }else{
// }
});
var autoSubmitTimeout = null;
jQuery('body').on('change', '.item-amount input', function () {
var object = jQuery(this);
if (autoSubmitTimeout != null) {
clearTimeout(autoSubmitTimeout);
}
autoSubmitTimeout = setTimeout(function () {
object.parents('form').submit();
}, 300);
});
解决方案
推荐阅读
- swift - 支持 macOS 应用程序中的 URL 方案
- python - 如何使用python离线访问谷歌表格
- python - Convert `pandas` frequency string to `DateOffset`
- java - 获取spring kafka中未处理的消息计数
- python - 在 python 中编写迭代的问题
- reactjs - 头盔导致 Heroku 上托管的 MERN 应用程序导致错误:拒绝执行内联脚本,因为它违反了以下内容
- node.js - 尝试访问我的 firebase 数据库时出现分段错误(核心转储)
- pip - 错误:由于 OSError 无法安装软件包:[Errno 22]
- r - predict.cv.glmnet() 如何计算二项式模型的链接值?
- api - Flutter 模板不显示调试