Jump to content

Edit History

Hissvard

Hissvard


Additional gotchas

Came here from Google and figured I'd share my quickly hacked together solution.

  • It was not tested too deeply so I take no responsibility for its proper functioning - please run your own checks.
  • It assumes the initial input of the "quantity" field is "1", so it's gonna conflict if the initial value in that field is different due to another plugin or theme you use.
  • It assumes the currency is euro
  • It assumes you're using comma as decimal separator and dot as thousands separator, as is more common in Europe.
prestashop.on('updatedProduct', () => {
    updatePricesByQuantity();
});

function updatePricesByQuantity() {
    // the CSS classes that contain the prices to change. You might need to change these depending on theme.
    const classNames = ['.price', '.regular-price'];
    // change this if not using euros. You can specify multiple like this: /€£/
    const currencyRegex = /€/;
    
    classNames.forEach((className) => {
        $(className).each(function() {
            const $target = $(this);

            // save the original price corresponding to quantity=1
            if (!$target.data('original-price')) {
                $target.data('original-price', $target.text());
            }

            // find the quantity
            const quantity = $target.closest('.cart-item, form').find('[name=qty], .js-cart-line-product-quantity').val();
          
            // replace shown price with original price multiplied by quantity
            $target.text(twoDecimals(stringToNumber($target.data('original-price')) * quantity) + ' €');
        });
    });

    // 10,00 € -> 10.00
    function stringToNumber(str) {
        return parseFloat(str.replace(/ /, '').replace(currencyRegex, '').replace(/,/, '.'));
    }

    // 5.2 -> 5,20
    function twoDecimals(n) {
        return parseFloat(Math.round(n * 100) / 100).toFixed(2).replace('.', ',');
    }
}

There is a small delay before prestashop fires the "updatedProduct" event, if that's an issue you can run this every 50 milliseconds instead:

setInterval(updatePricesByQuantity, 50);

Do keep in mind that's more computationally expensive.

Hissvard

Hissvard


Additional gotchas

Came here from Google and figured I'd share my quickly hacked together solution.

  • It was not tested too deeply so I take no responsibility for its proper functioning - please run your own checks.
  • It assumes the initial input of the "quantity" field is "1", so it's gonna conflict if the initial value in that field is different due to another plugin or theme you use.
  • It assumes you're using comma as decimal separator and dot as thousands separator, as is more common in Europe.
prestashop.on('updatedProduct', () => {
    updatePricesByQuantity();
});

function updatePricesByQuantity() {
    const classNames = ['.price', '.regular-price'];
    
    classNames.forEach((className) => {
        $(className).each(function() {
            const $target = $(this);

            if (!$target.data('original-price')) {
                $target.data('original-price', $target.text());
            }

            const quantity = $target.closest('.cart-item, form').find('[name=qty], .js-cart-line-product-quantity').val();
            $target.text(twoDecimals(stringToNumber($target.data('original-price')) * quantity) + ' €');
        });
    });

    function stringToNumber(str) {
        return parseFloat(str.replace(/ /, '').replace(/€/, '').replace(/,/, '.'));
    }

    function twoDecimals(n) {
        return parseFloat(Math.round(n * 100) / 100).toFixed(2).replace('.', ',');
    }
}

There is a small delay before prestashop fires the "updatedProduct" event, if that's an issue you can run this every 50 milliseconds instead:

setInterval(updatePricesByQuantity, 50);

Do keep in mind that's more computationally expensive.

Hissvard

Hissvard

Came here from Google and figured I'd share my quickly hacked together solution. It was not tested too deeply so I take no responsibility for its proper functioning - please run your own checks.

prestashop.on('updatedProduct', () => {
    updatePricesByQuantity();
});

function updatePricesByQuantity() {
    const classNames = ['.price', '.regular-price'];
    
    classNames.forEach((className) => {
        $(className).each(function() {
            const $target = $(this);

            if (!$target.data('original-price')) {
                $target.data('original-price', $target.text());
            }

            const quantity = $target.closest('.cart-item, form').find('[name=qty], .js-cart-line-product-quantity').val();
            $target.text(twoDecimals(stringToNumber($target.data('original-price')) * quantity) + ' €');
        });
    });

    function stringToNumber(str) {
        return parseFloat(str.replace(/ /, '').replace(/€/, '').replace(/,/, '.'));
    }

    function twoDecimals(n) {
        return parseFloat(Math.round(n * 100) / 100).toFixed(2).replace('.', ',');
    }
}

There is a small delay before prestashop fires the "updatedProduct" event, if that's an issue you can run this every 50 milliseconds instead:

setInterval(updatePricesByQuantity, 50);

Do keep in mind that's more computationally expensive.

×
×
  • Create New...