0

In the below code, where I am trying to update a custom attribute called branding. The for loop is running only for one iteration and stopping. It is updating only the first product in the list and not looping any further.

Can anyone kindly let me know why this is happening?

<?php
set_time_limit(0);

// require magento core
require_once 'app/Mage.php';

// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')->getCollection();

foreach($products as $product){
     echo $product->getSku();
     $product->setData('branding', 'kib');
     $product->save();  
}

Thanks.

Sanne
  • 1,316
  • 14
  • 34
Akshay Vasu
  • 337
  • 5
  • 17

5 Answers5

0

Try this code:

$i = 0;
foreach($products as $product){
    echo $product->getSku();
    $product->setData('branding', 'kib');
    $product->save();  
    $i++;
}
  • Thank you for answering but this would just add 1 to i every time the loop executes successfully. The 'i' would work only in for loop and not in foreach loop. My issue is the loop is not going forward at all after first index – Akshay Vasu Jun 11 '18 at 08:53
  • @Prashant Patel, are you sure that by adding $i++; the issue will be resolved? – Amit Bera Jun 11 '18 at 09:01
  • yes you can try @AkshayVasu –  Jun 11 '18 at 10:01
0

Try the below code it works for me:

<?php
// require magento core
require_once('app/Mage.php');
umask(0);
Mage::app('default');

// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')->getCollection();

foreach($products as $product){
     $product = Mage::getModel('catalog/product')->load($product->getId());
     echo $product->getSku();
     $product->setData('branding', 'kib');
     $product->save();  
}
Sukumar Gorai
  • 10,823
  • 4
  • 19
  • 46
0

If you want to set only one attribute of a product ie branding. the don't load all fields of product model. instead you can try below code.

$products = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('sku','branding'));

foreach($products as $product){
    $product->setData('branding', 'kib');
    $product->save();
}
Pradeep Sanku
  • 9,236
  • 2
  • 41
  • 73
0

Please try the following code:

<?php
set_time_limit(0);

// require magento core
require_once 'app/Mage.php';

// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')->getCollection();

foreach($products as $product){
     $prod = Mage::getModel('catalog/product')->load($product->getId());
     $prod->setData('branding', 'kib');
     $prod->save();  
}

This code should work for you.

Mohit Kumar Arora
  • 9,951
  • 7
  • 27
  • 55
0

You can try this ... should perform better then call save() for every product. Just put this into your root directory:

<?php
require_once 'app/Mage.php';
Mage::app();

$run = new My_Class();
$run->updateBrandings();

class My_Class
{
    public function updateBrandings()
    {
        $attributes = array('branding');
        $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect($attributes, 'inner');

        Mage::getSingleton('core/resource_iterator')->walk(
            $products->getSelect(),
            array(array($this, 'updateBrandingsCallback'))
        );
    }

    public function updateBrandingsCallback($args)
    {
        $product = Mage::getModel('catalog/product');
        $product->setData($args['row']);
        $product->setBranding('kib');
        $product->getResource()->saveAttribute($product, 'branding');
    }
}
sv3n
  • 11,657
  • 7
  • 40
  • 73