Update price automatically when quantity changed on product page Magento

杀马特。学长 韩版系。学妹 提交于 2019-12-19 10:46:36

问题


I am looking to have the product price automatically updated based on the quantity the customer has chosen.

Currently when you choose a custom option in magento the price automatically updates but when choosing quantity this does not.

So the scenario would be the product price is £10. User enters 3 quantity and automatically on the product page it updates the price to £30 and so on.

Does anybody know of a simple way of updating this?


回答1:


Using jquery you can do this

$('#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:


Use jquery::

HTML CODE::

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

Jquery Code::

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

})



回答3:


In Magento 1.9.2.4, the code file to edit is js/varien/product.js

In Magento 1.9.3 and above, the file to edit is js/varien/product_options.js

Add the following code:

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

right after

if (price < 0) price = 0;

and before

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

And, at the end of the file add:

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

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



来源:https://stackoverflow.com/questions/18759664/update-price-automatically-when-quantity-changed-on-product-page-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!