【问题标题】:Update price automatically when quantity changed on product page Magento当产品页面 Magento 上的数量发生变化时自动更新价格
【发布时间】:2013-09-16 13:38:14
【问题描述】:

我希望根据客户选择的数量自动更新产品价格。

目前,当您在 magento 中选择自定义选项时,价格会自动更新,但在选择数量时不会。

因此,假设产品价格为 10 英镑。用户输入 3 个数量并在产品页面上自动将价格更新为 30 英镑,依此类推。

有人知道更新这个的简单方法吗?

【问题讨论】:

    标签: javascript php jquery magento magento-1.7


    【解决方案1】:

    使用 jquery 你可以做到这一点

    $('#qty').keyup(function(){
        if($(this).val() != '' && isNumber($(this).val()) && $(this).val() > 0)
        {
           var price = $('#real_price').val() * 1;
           var qty = $(this).val() * 1;
           var total = price * qty;
           $('#price').html(total);
        }
        else
        {
           $('#price').html('500');    
        }
    });
    
    function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
    

    FIDDLE

    【讨论】:

      【解决方案2】:

      使用 jquery::

      HTML 代码::

      <input type='number' id='quantity'/>
      <span id='total_price'></span>
      

      jQuery 代码::

      var price=100;
      $("#quantity").on("change",function(){
          quantity=$(this).val();
          total_price=price*quantity;
          $("#total_price").html(total_price);
      
      })
      

      【讨论】:

        【解决方案3】:

        在 Magento 1.9.2.4 中,要编辑的代码文件是js/varien/product.js

        在 Magento 1.9.3 及更高版本中,要编辑的文件是js/varien/product_options.js

        添加以下代码:

        var qty;
        if($('qty').getValue().length == 0 || isNaN($('qty').getValue()) || $('qty').getValue() <= 0) { 
            qty = 1;
        } else { 
            qty = $('qty').getValue();
            price *= qty;
        }
        

        紧接着

        if (price < 0) price = 0;
        

        之前

        if (price > 0 || this.displayZeroPrice) { ...
        

        并且,在文件末尾添加:

        Event.observe(window, 'load', function() {
            $('qty').observe('blur', function(e){
                optionsPrice.reload();
            });
        });
        

        来源:https://magentojai.blogspot.com/2015/06/price-update-while-change-qty-in.html

        【讨论】:

          【解决方案4】:

          template\catalog\product\view.phtml 的末尾添加以下脚本

          <script>
          $('qty').observe('blur', function(e){
              $('qty').value = Math.max($F('qty').replace(/[^\d].*/, ''), 1);
              optionsPrice.productPrice = Math.max(optionsPrice.productOldPrice, $F('qty') * optionsPrice.productOldPrice);
              optionsPrice.reload();
          });
          </script>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-07-12
            • 2012-09-10
            • 1970-01-01
            • 1970-01-01
            • 2017-09-01
            相关资源
            最近更新 更多