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.

  • How many products you have? Did you check without saving the product its shows all skus? – Sukumar Gorai Jun 11 '18 at 8:51
  • @VishwasBhatnagar I have already used set_time_limit(0) at the start of the script. And still, the same thing is happening. I think it is not because of script timeout – Akshay Vasu Jun 11 '18 at 8:55
  • @SukumarGorai Yes, the list has all the sku's. Also if i remove the $product->save() and run. The loop executes properly without any problem. But it won't save the updated value of the attribute. – Akshay Vasu Jun 11 '18 at 8:56
  • Please check the answer if its working for you as its working fine for me. – Sukumar Gorai Jun 11 '18 at 8:57
  • @AkshayVasu, please check my answer and let me know if it worked for you. – Mohit Kumar Arora Jun 11 '18 at 11:35
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');
    }
}
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 8:53
  • @Prashant Patel, are you sure that by adding $i++; the issue will be resolved? – Amit Bera Jun 11 '18 at 9:01
  • yes you can try @AkshayVasu – user68116 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();  
}
  • Sorry, this answer is not working for me. Now it's not even updating the first product attribute, it is throwing http 500 error for me. I don't know why – Akshay Vasu Jun 11 '18 at 9:02
  • Can you move the $product->save() line to try-cache block so that you will get the exact error why this is not saving other products? – Sukumar Gorai Jun 11 '18 at 9:03
  • probably this line is throwing error to me $product = Mage::getModel('catalog/product')->load($product->getId()); – Akshay Vasu Jun 11 '18 at 9:03
  • I tried doing what you said. The try catch block is not showing any errors too. – Akshay Vasu Jun 11 '18 at 9:11
  • Using load() inside a loop is no good idea. – sv3n Jun 11 '18 at 12:22
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();
}
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.

  • Using load() inside a loop is no good idea. – sv3n Jun 11 '18 at 12:22

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.