Magento – Re-save all products in Magento2 – Weird error has forced me to manually resave products to get configurable options to show up on frontend

magento2script

I can't find any information on how to programmatically re-save all products.

I'm running into a weird error that suddenly all products with configurable options, the options no longer show up on the frontend unless I manually go into the backend and click save on the product.

I have over 1000 products so it would be really helpful if I could get a script working to programatically re-save all products so that their options show up on the frontend again.

Things I have already tried:

  1. reindex all by php bin/magento indexer:reindex
  2. cleared all cache etc
  3. redeployed static assets
  4. ran php bin/magento setup:upgrade etc…

If someone could convert this logic to Magento 2 that would be soooo helpful and save me probably 10 hours of work:

http://magetechno.com/wpmaster/resave-all-products-in-magento/

"Due to index problem in Magento which causes the product information
isn’t shown correctly on frontend. You will have to resave the product
to have correct data again. But there are a lot of products you can do
it manually. Here is script to help you to save all products quickly.

Step 1 : Create file named “resaveallproduct.php” in root folder of
magento. Step 2 : Paste following code."

// 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->getName() . '<br/>';
    // save the product
    $product->save();
}

echo 'Congratulations!!! Product resaved';
?>

Step 3 : Now run “resaveallproduct.php” file from broswer. For Example
: yourdomain/resaveallproduct.php

I need the above code basically to work for magento 2, or any other type of workaround.

Please help!

Best Answer

I think the best would be to use an UpgradeData script. First, you will need to create a module - which is pretty easy (or see the example files below). You only need to create the registration.php and etc/module.xml files and then run:

  • bin/magento module:enable --clear-static-content Component_Name
  • bin/magento setup:upgrade

After that, create an UpgradeData class in a setup directory. There you will need to use constructor dependency injection for the product collection and the state. After you have those in your class, setup the upgrade method to do the work.

Note: you will need to change the setup_version in your module.xml any time that you want the script to run.

Here's an example (in my test module SwiftOtter\ShareButtons):

namespace SwiftOtter\ShareButtons\Setup;

use Magento\Framework\App\State;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Setup\{UpgradeDataInterface, ModuleDataSetupInterface, ModuleContextInterface};

class UpgradeData implements UpgradeDataInterface
{
    protected $productCollection;
    protected $state;

    public function __construct(Collection $productCollection, State $state) {
        $this->productCollection = $productCollection;
        $this->state = $state;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $this->state->setAreaCode('frontend');

        $products = $this->productCollection->load();
        $i = 0;

        foreach ($products as $product) {
            if ($i > 4) {
                break;
            }

            $product->save();
            $i++;
        }

        $setup->endSetup();
    }
}

A few notes on that:

  1. That is using PHP7 for the use statements so you will need to adjust the curly braces if you are running 5.6.
  2. I left the iterator in there so you could easily test just the first couple of products in your store to ensure it works correctly.

If it helps, here's what the etc/module.xml is (after you change the name):

<?xml version="1.0" ?>
<config>
    <module name="SwiftOtter_ShareButtons" setup_version="0.1.7" />
</config>

And registration.php:

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'SwiftOtter_ShareButtons', __DIR__);
Related Topic