1

I'm trying this code because I want to see the price of the product change dynamically when in the input field of the quantity exchange the number (this only for the products that have the tier price). For example if I write in the input of quantity 50, in the product page I want to see the first final price written, otherwise if I write 100 I want to see the updated price of the product. This is the code that I tried to use but without success:

span class="tierprice">/span> input type="text" name="qty" id="qty" value="getProductDefaultQty() * 1 ?>" title="__('Qty') ?>" class="input-text qty" />

                <script type="text/javascript">
                var $j = jQuery.noConflict();

                jQuery(function($j){
                    // probably you want a custom method in your block for getting a better and safer tierPrices array here
                    // for example with formatted prices
                    var tierPrices = <?php echo json_encode($_product->getTierPrice()) ?>;
                    var getPrice = function(qty){
                        qty = Number(qty);
                        var i = tierPrices.length;
                        while(i--)
                        {
                            if(qty >= tierPrices[i]['price_qty']){
                                return tierPrices[i]['price'];
                            }
                        }
                        return null;
                    };
                    var updatePrice = function(price){
                        $('.tierprice').html(price);
                    };
                    // you can use more events here if you want a live response while the user is typing
                    $j('#qty').change(function(){
                        var price = getPrice(this.value);
                        if(price !== null){
                            updatePrice(price);
                        }
                    });
                });
                </script>

Where is the error?

0

Idk if it will help you, but I managed to get it working by using $ outside and jQuery on the inside of the script.

$(document).on(
    'input',
    '#qty',
    function(){
    var qty = jQuery("#qty").val();

    jQuery.ajax({
        url: 'your/url/path',
        method: 'POST',
        data: {
            'qty': qty,
        },
        success: function (json) {
            var data = jQuery.parseJSON(json);
            jQuery('.container').html(data['html']);
        },
    });
}
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.