0

I'm Using this below Script, but on executing my page lands to Error 503.

Can anyone, please help me in re-saving all the product programmatically

<?php
use Magento\Framework\App\Bootstrap;

include 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollectionFactory->create()
                        ->addAttributeToSelect('*')
                        ->load();

foreach ($productcollection as $product) {
    $productId = $product->getId();
    $product = $objectManager->create('Magento\Catalog\Model\Product');
    $product->load($productId);
    $product->save();
}
?>

Thank you.

7
  • where you are executing this script server (or) local instance? use exception handling try and catch in your logic so that you can get the exact error it may help you further. Commented Oct 31, 2019 at 12:26
  • do the changes prnt.sc/pqmm7e Commented Oct 31, 2019 at 12:32
  • Ok i will try once
    – DK Thakur
    Commented Oct 31, 2019 at 12:39
  • Sir can you answer some of my other question
    – DK Thakur
    Commented Oct 31, 2019 at 12:41
  • But let me know why you would like to save all the products programmatically? Is this like trail (or) you want to perform on server level? Commented Oct 31, 2019 at 12:41

1 Answer 1

0

Here is the code in which i executed locally to save all the simple products programmatically. Make sure to take a Database backup before executing this script & Execute this script from the M2 root folder from the command line & don't forget to re-index. If it works locally while testing. Do the appropriate changes if required based on your requirements.

(or else) save one product programmatically and check the results whether it is working (or) not.

<? php
ini_set('display_errors',1);
error_reporting(E_ALL);
ini_set('memory_limit','512M');
ini_set('max_execution_time', 80000);
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');


$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productcollection = $productCollectionFactory->create()
                        ->addAttributeToSelect('*')
                        ->load();
echo "Total Products".$productcollection->getSize().'\r';                      
foreach ($productcollection as $product) {
    if($product->getTypeId() == 'simple'){
        $productId = $product->getId();
        $product = $objectManager->create('Magento\Catalog\Model\Product');
        $product->load($productId);
        $product->save();
        echo "product saved successfully -".$product->getId().'\r';
    }

   }
?>
15
  • executing Your script Error: Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 33554440 bytes) in /var/www/vhosts/Exm.com/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 291
    – DK Thakur
    Commented Oct 31, 2019 at 13:15
  • How many products are there? Commented Oct 31, 2019 at 13:16
  • 10852 records found
    – DK Thakur
    Commented Oct 31, 2019 at 13:18
  • Use ini_set('memory_limit','10G'); Commented Oct 31, 2019 at 13:19
  • Are you executing the above script on the local instance environment? (or) server? Commented Oct 31, 2019 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.