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:
- reindex all by
php bin/magento indexer:reindex
- cleared all cache etc
- redeployed static assets
- 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
andetc/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 yourmodule.xml
any time that you want the script to run.Here's an example (in my test module
SwiftOtter\ShareButtons
):A few notes on that:
use
statements so you will need to adjust the curly braces if you are running 5.6.If it helps, here's what the
etc/module.xml
is (after you change the name):And
registration.php
: