Magento 2.3: Re-save all products programatically [closed]
-
13-04-2021 - |
Question
I need to re-save all products programatically without modifying any attribute. (Only re-save)
How to do that ?
Solution
Create a custom script on the root of Magento.
magentoroot/saveallproduct.php
<?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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange