首页 > 解决方案 > 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);
});

标签: javascriptphpjquerywordpresswoocommerce

解决方案


推荐阅读