0

I want to resave all products programmatically. So I am trying to use the below code to achieve it. But the save method is not working in it. I have placed the code in the root directory of my Magento 1.9 installation. Can anyone kindly let me know how can I make it work?

<?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->getData('sku') . '<br/>';
    // save the product
    $product->save();
}
  • How did you understand that product save is not working as you have not changed anything and save? Try to change some value of products and try save. If still not working then try to load the product and then save. – Sukumar Gorai Jun 9 '18 at 13:35
  • I have tested it by echoing and checking where exactly the loop is stopping. The for loop is traversing only once, displaying only one SKU and it is stopping at the save method. No changes are being done. I checked that too. – Akshay Vasu Jun 9 '18 at 14:04
  • Can you load the product on loop once and try? – Sukumar Gorai Jun 9 '18 at 14:42
0

You need to do changes in order to save something. If you don't need anything there is no need to save().

This works pretty good:

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

foreach($products as $product){
     $product->setData('sku', $sku);
     $product->save(); 
}
  • I am actually trying to save again because I am importing stocks programmatically. But once the stocks are imported and few products go out of stock, those products still show up in the front end. Only after I save those products from backend explicitly the product disappears from the front end. Hence I am trying to save all products programmatically once again after the stocks are imported. – Akshay Vasu Jun 11 '18 at 6:06
  • Even now it is stopping at one iteration. It is working for just first product. It is saving that first product and the loop is not moving further for next iteration. I don't understand why it is happening. – Akshay Vasu Jun 11 '18 at 8:23
  • You should try to reindex the product if it runs out of stock. Normally Mage does this on his own. – Max Jun 11 '18 at 11:29
0

Try this 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) {
    $name = 'xyz';
    $productLoad = Mage::getModel('catalog/product')->load($product->getId());
    echo $productLoad->getData('sku') . '<br/>';
    $productLoad->setData('name',$name);
    // save the product
    $productLoad->save();
}
  • Does this changes the name of all the products in my store to 'xyz'? – Akshay Vasu Jun 11 '18 at 6:08
  • yes but you an try only one product if working fine then you change all product data. – user68116 Jun 11 '18 at 6:11

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.