I fixed a nasty little bug in GoogleCheckout (now Wallet) today. Basically if a customer has a free or zero priced product in their cart, GoogleCheckout will return an error looking something like this:
Google Checkout: Error parsing XML; message from parser is: cvc-datatype-valid.1.2.1: '' is not a valid value for 'decimal'.
I have developed custom modules which add free or bonus items to a customer's cart if they use coupons, meet certain cart criteria or belong to particular customer groups. Buy x, get y rules also work this way. So this is a nuisance. Luckily few customers opt to use GoogleCheckout, but still, I don’t Live with Broken Windows[1].
Chasing the problem down the call stack leads to app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Checkout.php and specifically the _getItemsXml() method.
$unitPrice = $item->getBaseCalculationPrice();
if (Mage::helper('weee')->includeInSubtotal()) {
$unitPrice += $item->getBaseWeeeTaxAppliedAmount();
}
// ...
<unit-price currency="{$this->getCurrency()}">{$unitPrice}</unit-price>
Now, if the product's baseprice is 0, then for some unfathomable reason it's set to '', not 0. As the unit-price element expects a decimal value, an empty string fails validation.
The fix is pretty trivial
$unitPrice = $item->getBaseCalculationPrice();
if (Mage::helper('weee')->includeInSubtotal()) {
$unitPrice += $item->getBaseWeeeTaxAppliedAmount();
}
$unitPrice = ((float) $unitPrice > 0) ? $unitPrice : 0.00;
The store I needed to fix only used US dollars so I haven't tested how the use of other currencies or locales might affect this fix.
To apply the fix, don't modify the core codepool, but instead take advantage of the local and community codepool's higher classloader priority[2] and place the amended code in app/code/local/Mage/GoogleCheckout/Model/Api/Xml/Checkout.php.
[1]: 'Don't Live With Broken Windows' is a tip I first read about in The Pragmatic Programmer. It is used to help fight Software Entropy (software's tendency to lose structure over time). This concept has parallels with the real world as urban areas with broken windows tend to see higher levels of vandalism when compared to areas where windows are constantly maintained.
When you ignore small problems it becomes easier to let more significant problems slide too. Hence the rule of thumb, 'Dont Live With Broken Windows'.
[2]: Magento resolves classes in this order local, community then core. This means if two classes have the name Mage_Core_Model_Foo one exists in local the other in core, then the version in local is used.