Best practice/scalable way to mass update tier prices?
-
13-12-2019 - |
Question
In a Magento CE 1.9 shop, I need to update the prices & tier prices for a few thousand products daily.
The product prices I can update easily using the following helper function
Mage::getSingleton('catalog/product_action')
->updateAttributes(array($productId), array('price' => $price), 0);
To update the tier prices I unfortunately have to rely on the following which probably isn't very scalable as the whole product is saved every time:
$tierPrices = array();
$tierPrices[] = array(
'website_id' => 0,
'cust_group' => 32000, // "All Groups"
'price_qty' => $qty,
'price' => $price
);
$product->setTierPrice($tierPrices)->save();
Is there a better way to update the tier prices?
Solution
The best way to update tier prices in bulk (as any batch of data in Magento) is to use direct database communication approach. In your case I would do the following:
- Create a new temporary table into which I would put all the prepared data for update, that will have only primary key on
website_id
,customer_group_id
andentity_id
. And it will have qty and price columns as non-indexed values (helps with write performance) - Put all the data for import into created table.
- Trigger singe
INSERT ... FROM SELECT ... ON DUPLICATE KEY UPDATE
forcatalog/product_tier_price
table, that will replace/insert your pricing information for the product. Additionally, you can performDELETE FROM SELECT
before adding data for cleaning up previously set values. - Invoke price re-indexation for updated products.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange