Question

I need to re-save all products programatically without modifying any attribute. (Only re-save)

How to do that ?

Was it helpful?

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
scroll top