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
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
Hi Magento provide an attribute by below code
$product->setAttributeCode($newValue)
$ProductObject->getResource()->saveAttribute($product, 'attribute_Code');
$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');
$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');
}
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
$product->getResource()->saveAttribute
action. THX!!
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($allProductIds, $attrData, $storeId);
// reindex:
Mage::getModel('catalog/product_action')->updateAttributes($allProductIds, $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.
<?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);
Commented
Apr 14, 2016 at 12:15
Mage::getSingleton('catalog/resource_product_action')
to Mage::getModel('catalog/product_action')
with the exact same parameters mentioned above.
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
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);