2

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?

1 Answer 1

5

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:

  1. 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 and entity_id. And it will have qty and price columns as non-indexed values (helps with write performance)
  2. Put all the data for import into created table.
  3. Trigger singe INSERT ... FROM SELECT ... ON DUPLICATE KEY UPDATE for catalog/product_tier_price table, that will replace/insert your pricing information for the product. Additionally, you can perform DELETE FROM SELECT before adding data for cleaning up previously set values.
  4. Invoke price re-indexation for updated products.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.