1

i am updating different products with different prices of a specific store. Everything is working fine. But problem is, it takes too much time as i have 1000 of products.

$collection = Mage::getResourceModel('catalog/product_collection')->addStoreFilter($_primarystoreid)->addAttributeToSelect('*');
    $prod1 = Mage::getModel('catalog/product')
            ->setStoreId($_primarystoreid);
    $prod2 = Mage::getModel('catalog/product')
            ->setStoreId($_secondarystoreid);

    foreach ($collection as $product) {
        $prod1->load($product->getId());
        $actualPrice = $prod1->getPrice();
        $newPrice = ((((float) $price) / 100) * $actualPrice) + $actualPrice;
        $prod2->load($product->getId());
        $prod2->setPrice($newPrice)->getResource()->saveAttribute($prod2, 'price');
    }

So is there any way to do the same thing without any loop?Thank You

  • just to understand...what is the value of $price variable. It's not defined anywhere. – Marius Nov 24 '14 at 12:02
  • $price is just a variable. In my case, its value is 20 suppose. – umair ayub Nov 24 '14 at 12:09
  • is it the same for all the products or varies from product to product? – Marius Nov 24 '14 at 12:14
  • What I understand from your code is that you want to increase the prices for a specific store with 20%. Am I right? – Marius Nov 24 '14 at 12:14
  • yes..exactly. But it takes a lot of time. – umair ayub Nov 24 '14 at 12:16
3

First of all you have 2 load calls in your loop. I think that's the expensive part not the loop itself.
Second...I don't understand where does $price come from.
But anyway...Here is an approach that uses a loop but does not call load.

$collection = Mage::getResourceModel('catalog/product_collection')->addStoreFilter($_primarystoreid)->addAttributeToSelect('price');

foreach ($collection as $product) {
    Mage::getSingleton('catalog/product_action')->updateAttributes(
        array($product->getId()), //what products to update
        array('price' => your price formula here), //what attributes to update
        $_secondarystoreid //on what store
    )

}

Even if the loop still exists, this is definitely faster.

[EDIT]
If you want fast, you can take the risky sql way.
Just write a query that will update everything. It does not get any faster than that.
Here is the idea.
First you have to get the id of the price attribute:

SELECT * FROM eav_attribute WHERE attribute_code = 'price';

All the prices are kept in the catalog_product_entity_decimal table.
So you need to do this... Get all the prices for products where store_id is 'primary store id'.
There is a chance that some of the products have for price "use default", and there is no record with store_id = {primary store id}. In this case you need the values for the product where store_id = 0.
Now multiply this value with 1.2 (increase with 20%) and insert the values for the same products but with store_id = {secondary store Id}.
But there is a chance that there is such a record for a product. In this case you cannot insert a new line because of the unique constraints. You just have to update the value.
Simple huh? :)

Here is what I came up with. But please backup your db before trying anything. I won't be held responsible if something goes south.

Replace the palceholders with your values.

INSERT INTO catalog_product_entity_decimal (entity_type_id, attribute_id, store_id, entity_id, value)
(
SELECT 
    e.entity_type_id, 
    e.attribute_id, 
    {secondary store id} as store_id, 
    e.entity_id, 
    IFNULL(e1.value, e.value) * 1.2 as value 
FROM 
    catalog_product_entity_decimal e
LEFT JOIN 
    catalog_product_entity_decimal e1 
ON 
    e.attribute_id = e1.attribute_id AND
    e1.store_id = {primary store id} AND
    e.entity_id = e1.entity_id
WHERE 
    e.attribute_id = {price attribute id} AND 
    e.store_id = 0
)
ON DUPLICATE KEY UPDATE 
    value = IFNULL(e1.value, e.value) * 1.2

When you are done, reindex everything.

  • It is showing me an error. Could you please double check your answer? thankyou – umair ayub Nov 24 '14 at 13:27
  • it worked for me. Can you tell me what error you get? – Marius Nov 24 '14 at 13:28
  • There has been an error processing your request. – umair ayub Nov 24 '14 at 13:34
  • $write = Mage::getSingleton("core/resource")->getConnection("core_write"); $query="query goes here"; $write->query($query); am i doing it wrong? – umair ayub Nov 24 '14 at 13:35
  • Oh. I didn't try it in the code. I just executed the query on the db. Check var/log for the error message. – Marius Nov 24 '14 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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