Question

when I sort by price product order in Asc or dec. In product listing shows like, First product price is 40, second product price is 50 and the third product price is 40.

If I opened and save those 50 price product in admin backend that product, it's working fine.

All product have customizable options. When I run the following script it's working fine. But this script will delete those customizable options.

<?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('frontend');


$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

$productCollection->load();

foreach($productCollection  as $product)
{
 $product->save();

}

echo "saved";

Find any possible way to fix this issue. advance thanks

Was it helpful?

Solution

Try to load the product options with your collection. The product collection does not load options by default since they are not used in product lists. Maybe that have impact on what you are doing in your code.

$productCollection->addOptionsToResult()->load();

OTHER TIPS

Try this,

<?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('frontend');                             
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); 
$productCollection->addAttributeToSelect('*');              
$productCollection->addAttributeToSort('price', 'ASC');  

foreach($productCollection  as $product){                      
    $product = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);                            
    $product->save();                                                 
}                      
  echo "saved";

Hope this helps, peace :)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top