
Two (more) Magento Checkout Bugs for the Price of One
10/11/11 8:31 AM • Posted in Bugs
By Dante P
0 Comments
Depending on the shipping methods configured for your store, your customers may be running into a few javascript errors. Unfortunately occurrences of various errors as part of the purchase transaction isn't anything new. The good news is the two here aren't show-stoppers, which may explain why the core code has remained unchanged over so many releases.
For a little background we can read that the two issues are in Magento's field of vision with this bug 25547 which still seems open and this bug 26296 which hints that perhaps, just maybe, a fix will be in 1.6.1. A Magento forum discussion to this can be found here and as well here among other places.
Let's just fix it. The file giving grief we'll find here:
app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml
or your theme's version of it. If you don't have one in your theme, you'll probably want to copy the base/default one now. Towards the end of the file you'll see a javascript function includingShipping:
function includingShipping(getShippingCode)
{
<?php if (!empty($shippingMe)): ?>
var newPrice = shippingMe[getShippingCode];
if (!lastPrice) {
lastPrice = newPrice;
quoteBaseGrandTotal += newPrice;
}
if (newPrice != lastPrice) {
quoteBaseGrandTotal += (newPrice-lastPrice);
lastPrice = newPrice;
}
The lines we're interested in are 81, 83, and 86. There are a few ways to rewrite this code depending on your tastes. For example In line 81 we are given:
if (!lastPrice) {
While such a conditional may pass muster with PHP, it doesn't with Javascript. This line has the chance to produce the error: lastPrice is not defined because, straightforwardly, lastPrice is not always defined. 'Not always' meaning lastPrice is conditionally defined earlier in this file at around line 50. We can work with this pretty easily either by ensuring lastPrice is always set, or re-writing the conditional which checks it to be a bit more stable like so:
if (typeof lastPrice === 'undefined') {
Which brings us to the other occurrence of an undefined variable - quoteBaseGrandTotal. Like lastPrice, it isn't necessarily defined. So doing any math with it has a chance of ending with a bit of a sting. quoteBaseGrandTotal may be called on line 83 or 86 (or possibly both). Following the lastPrice change above, we add a check for it to see if it is defined, and from that decide how to do the math. After a bit of tape and solder we arrive with:
function includingShipping(getShippingCode)
{
<?php if (!empty($shippingMe)): ?>
var newPrice = shippingMe[getShippingCode];
if (typeof lastPrice === 'undefined') {
lastPrice = newPrice;
if(typeof quoteBaseGrandTotal === 'undefined') {
quoteBaseGrandTotal = newPrice;
} else {
quoteBaseGrandTotal += newPrice;
}
}
if (newPrice != lastPrice) {
if(typeof quoteBaseGrandTotal === 'undefined') {
quoteBaseGrandTotal = (newPrice-lastPrice);
} else {
quoteBaseGrandTotal += (newPrice-lastPrice);
}
lastPrice = newPrice;
}
Which should keep the math as intended and remove two possible errors from our customers shopping experience. Keep your fingers crossed for 1.6.1!
