Categories
Magento

Magento Special Price Not Loaded With Massive Product Import

Every time I work in Magento, even a simple issue becomes a complex task. This time the problem was related to product special prices.

In that Magento based e-commerce, the whole catalog is regularly imported from an external set of files, with product details and prices for many countries. The Magento is actually configured in a multisite and multistore way.

The issue was relatively simple: single product page was not showing the special price even if that price was for sure set on that product (for that country).

I made things simpler, actually the Magento installation has at least 30 modules, many of them changing the default inventory, price and configurable product model. But at the least the price template (price.phtml) was the original of Magento, so the only reason to have the special price not shown was due to the computation of the final price which was returned always equal to the regular price.

No, there were no reasons for that behavior and initially I suspected the problem to be in one of the extra modules. But they could not be removed to keep the Magento working.

So after tens of Mage::log() in the code, I started to debug Magento using Netbeans and XDebug. Not a real pleasure, Magento is slow by nature, with XDebug it was slow at power of n.

Anyway, going forward step by step I reached a piece of the Magento code, the EAV model. That model when loading an entity (a product is an entity… everything is an entity in Magento), first loads the entity (few common fields) and then loads the attributes (prices, dates, values and so on).

Looking with an inspector inside the loaded attributes, I saw the special price was missing. OMG! Why? The special price is there!

Ok, the EAV model was not enough, I started to print out the queries and… bingo: running the query which loads the attributes I got a result set missing the special price.

Why? Because the generated query (a big query with left join and union all statements) is built to retrieve the special price for the current store but even the special price for the default store, the store with id 0.

The massive product importer was not adding the special price to the default store and the query failed to load the special price even for a different store.

Now we can discuss if this is correct or not, it doesn’t matter: that little thing force me to debug the e-commerce for a number of hours and confirmed me an diffuse opinion… to write directly inside the Magento database can be very, very dangerous!

Leave a Reply