Mass Product Attribute Upload in Magento and Magento 2


Mass Product Attribute Upload in Magento

According to latest practice in magento site most developers use product collection to update the product attribute. But unfortunately it has bad effect to side such as,

  1. Product collection script update get slow.
  2. If we use select option to update product collection, when it save it back url key can be empty.

To overcome magento already have function call mass product attribute to update the product.

Product collection script update get slow


       /* Add a comment to this line

* Reset product collection which should include in the sales category.

* Set sale attribute to 1       */

       $productCollection->getSelect()->reset();

       $productCollection

           ->addAttributeToSelect(‘sale’)

           ->addAttributeToFilter(‘entity_id’, array(‘nin’ => $rejectedIds))

           ->addAttributeToFilter(

               array(

                   array(

                       ‘attribute’ => ‘entity_id’,

                       ‘in’        => $catalogRuleProductIds

                   ),

                   array(

                       ‘attribute’ => ‘special_from_date’,

                         ‘lteq’      => date(“Y-m-d h:i:s”)

                  )

               )

           );

         if ($productCollection->getSize() > 0) {

           foreach ($productCollection as $product) {

               $product->setSale(1)->save();

           }

 

 

According to this script it’s seems code is faster than normal way but when existing more than one 100 product the process time has been increased widely. Because once all product collection loaded and re save it from loop its reset all attribute (run update script for all attributes).

If we use select option to update product collection, when it save it back url key can be empty.

In the previous script it use select option

 

$productCollection->getSelect()->reset();

         $productCollection

             ->addAttributeToSelect('sale')

Its resetting the select option and add new attribute. In this case it’s returning only sale’s attribute details and it when it’s saving product one by one it’s removing all product URL and make product url empty. So when all saved product url will be removed and in the frontend those product will link to home page.

Solution

 $collection = Mage::getResourceModel('catalog/product_collection')

                ->addAttributeToFilter('sale', 1)

                ->getAllIds();

 

Mage::getSingleton(‘catalog/product_action’)            ->updateAttributes($collection, array(‘attribute_code’ => ‘sale’, 1));

 

Following code Updating Only The Product Attribute Next, if you are only saving a specific attribute it would be quicker (and potentially avoid your problem).
Note: When checking updateAttributes method itself reindexing the product.

Mass Product Attribute Upload in Magento 2

Solution

$this->_objectManager->get('Magento\Catalog\Model\Product\Action')
    ->updateAttributes($productIds, ['status' => $status], $storeId);

Note: once update the script its run the “catalog_product_category” indexer if only its not scheduled. 

$categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
if (!$categoryIndexer->isScheduled()) {
    $categoryIndexer->reindexList(array_unique($productIds));
}

Leave a Reply

  • (will not be published)