14

I'm trying to update the price to a lot of products (more than 10.000). The way I am doing that right now is extremely time-consuming. Which is the best way to loop through all the products and update most of them?

Thanks

1

4 Answers 4

22

Hi Magento provide an attribute by below code

$product->setAttributeCode($newValue)
$ProductObject->getResource()->saveAttribute($product, 'attribute_Code');

Example:

$product=Mage::getModel('catalog/product')->load($id);
$product->setSpecialFromDate('2010-10-28');
// below code use for time format 
$product->setSpecialFromDateIsFormated(true);
$product->getResource()->saveAttribute($product, 'special_from_date');

Using Product Collection:

$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));

foreach($products as $product)
{
    $product->setSpecialFromDate('2010-10-28');
    // below code use for time format 
    $product->setSpecialFromDateIsFormated(true);
    $product->getResource()->saveAttribute($product, 'special_from_date');


}
6
  • This is super fast! I've tried Mage::getSingleton('catalog/product_api')->update(); and Mage::getSingleton('catalog/product_action')->updateAttributes(). product_api took 1.7 seconds on avg and product_action took 1.0 seconds on avg. however, $product->getResource()->saveAttribute() took on avg 0.04 Seconds on avg. Thanks! Commented Oct 9, 2015 at 20:53
  • Is there anything similar to this method that can update attributes at a certain store level?
    – scrowler
    Commented Sep 14, 2016 at 4:08
  • Thx @Amit Bera -- 1 question remaining ;) is there also a way to save multiple attributes in 1 go? Of would one have to loop around the $product->getResource()->saveAttribute action. THX!!
    – snh_nl
    Commented Jun 19, 2017 at 12:07
  • @amit bera, pls look into this magento.stackexchange.com/questions/201757/… what should i do ?? Commented Nov 16, 2017 at 10:21
  • @amit: I need how can i bulk change website (update attribute in multi-store)
    – zus
    Commented Jan 4, 2018 at 6:47
18

Write an SQL Query.

Second best way (and the one I recommend): \Mage_Catalog_Model_Resource_Product_Action::updateAttributes

Code example by u_maxx:

$allProductIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
$attributeCode = 'some_eav_attribute';
$attributeValue = NULL;
$attrData = array( $attributeCode => $attributeValue );
$storeId = 0;
// no reindex:<
Mage::getSingleton('catalog/resource_product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);
// reindex:
Mage::getModel('catalog/product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);

How would this work with multiselect values? Does it 'add' the value or overwrite other pre-selected values

Multiselects are normally saved as comma seperated integers, like 27,42,4711. Therefore if you change the value to say 1, the other values are lost. But what you can do is something like CONCAT(value, ',1') means adding the new value to the end, seperated by a comma. I think even if you add a value two times, this is no problem, because Magento filters it the next time the item is saved and ignores the second value.

11
  • This will only update 1k rows at a time, not as quick: github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/…
    – B00MER
    Commented Nov 13, 2014 at 15:55
  • When I understand the code correctly it writes every 1000 rows, but it updates all in a row. Commented Nov 13, 2014 at 16:44
  • 1
    Thank you @FabianBlechschmidt! Here is my code example (gist): <?php $allProductIds = Mage::getModel('catalog/product')->getCollection()->getAllIds(); $attributeCode = 'some_eav_attribute'; $attributeValue = NULL; $attrData = array( $attributeCode => $attributeValue ); $storeId = 0; Mage::getSingleton('catalog/resource_product_action')->updateAttributes($allProductIds, $attrData, $storeId);
    – Max Uroda
    Commented Apr 14, 2016 at 12:15
  • 1
    The problem with this approach is that it doesn't reindex the products, so you may not see changes in the frontend and just see them in the admin panel. To also reindex after update you can change Mage::getSingleton('catalog/resource_product_action') to Mage::getModel('catalog/product_action') with the exact same parameters mentioned above.
    – shampoo
    Commented Oct 3, 2017 at 18:10
  • 1
    @snh_nl updated the answer. Hope this answers your question Commented Jul 2, 2018 at 18:33
5

10k products, use SQL. EAV just muddies the waters. Per question is Fastest way.

Find attribute_id

SELECT * FROM eav_attribute where entity_type_id = 4 and attribute_code = 'YOUR_ATTRIBUTE_CODE_HERE'

Update with found attribute_id from above query.

UPDATE catalog_product_entity_int SET value = 1 WHERE attribute_id = FOUND_ATTRIBUTE_ID

I suppose you could do a subselect with update but for simplicities sake two SQL queries is easiest to grasp.

NOTE: entity_type_id = 4 is most always the product EAV entries. In case you are needing to mass update category or customer attributes this will differ as well as the table that is updated based on what type of attribute it is your updating. Also if you have multi-store setup be sure and note and condition store_id

3

Further to Fabian's answer above, you can update multiple fields at a time. The example below has just 2 (in stock, stock status), but you can use as many as you want.

    $product_ids = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('status', array('eq' => 2)) //only disabled 
        ->getAllIds();

    $attrData = [
        ['attribute_one' => 1],
        ['attribute_two' => 1]
    ];

    $storeId = 0;
    Mage::getSingleton('catalog/resource_product_action')
       ->updateAttributes($product_ids, $attrData, $storeId);
2
  • So this is if you want to update all products tot he same attribute value - how could you adapt this to update say 3 products, all the same attribute, but with different values for the attribute? thx
    – snh_nl
    Commented Jun 17, 2017 at 15:26
  • See Amit Bera's answer above: magento.stackexchange.com/a/43924/3433 Commented Jun 19, 2017 at 1:28

Your Answer

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

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