Joe Spurling
Joe Spurling

Reputation: 977

Can I get advice for hunting down unwanted JavaScript on a Magento page?

I have a product page on a Magento 1.7 installation that has a product with tiered pricing. It should say buy 4 and save 13%, which it does for a split second before the value of 13% is overwritten by 100% (which is incorrect). I can only assume that there is some buggy JavaScript on the page that is causing this, but I have no idea how to find it. Can somebody please give me some advice on how to find and fix this bug please?

Here is the page: http://makethemostof.co.uk/lace-print-cushions

Upvotes: 0

Views: 725

Answers (4)

Tr1stan
Tr1stan

Reputation: 2775

Although this is great post on trying to get to the bottom of what is causing the issue, I found that the solutions on the page just prevent the code from running, they don't actually solve the problem. I have posted a solution to this problem here:

https://stackoverflow.com/a/16022796/159341

Upvotes: 0

Joe Spurling
Joe Spurling

Reputation: 977

For those that are interested specifically in the bug in Magento 1.7 where tier price savings are always displayed as 100% this is the fix:

In js/varien/product.js change the following on line 748:

for (var i = 0; i < this.tierPrices.length; i++) {

with

for (var i > 1; i < this.tierPrices.length; i++) {

Upvotes: 0

Joost Buitenhuis
Joost Buitenhuis

Reputation: 21

Actually it's

for (var i > 0; i < this.tierPrices.length; i++) {

If you replace it for 1 it will just calculate the correct Savings % for the first tier price not for the others after that, they wil still be set to 100%.

While he needs to calculate it for all tierprices so if i is greater then 0 he needs to calculate the correct tier % otherwise it's the same price as the product price or equal to 0 or 100%

Hope it helps

Upvotes: 2

Florian Margaine
Florian Margaine

Reputation: 60835

Looked at the HTML element in the Chrome Inspector: class="benefit".

Searched ".benefit" in the Resources tab, 1 match, in the loupe.js file.

Went to the Scripts tab, loupe.js file, prettify the code, search ".benefit".

       $$('.benefit').each(function(el) {
            var parsePrice = function(html) {
                return parseFloat(/\d+\.?\d*/.exec(html));
            };
            var container = $(this.containers[3]) ? this.containers[3] : this.containers[0];
            var price = parsePrice($(container).innerHTML);
            var tierPrice = $$('.price.tier-' + i);
            tierPrice = tierPrice.length ? parseInt(tierPrice[0].innerHTML, 10) : 0;
            var $percent = Selector.findChildElements(el, ['.percent.tier-' + i]);
            $percent.each(function(el) {
                el.innerHTML = Math.ceil(100 - ((100 / price) * tierPrice));
            });
        }, this);

Found the script in less than a minute thanks to Chrome Inspector.

Now, here is the culprit:

el.innerHTML = Math.ceil(100 - ((100 / price) * tierPrice));

It's an easy math calculation. I don't know what tierPrice is, but I'm sure you can find out the correct calculation for your business (Math.ceil rounds up the number).

Upvotes: 7

Related Questions