1

Is there an easy way to swap the long and short descriptions in the mysql database for a magento 2.2.0 installation for all products?

1 Answer 1

1
  1. Make a New File in Magento Root name it 'swap-product-desc.php'
  2. Add Below Code in that file and run it by url : 'your-site-url/swap-product-desc.php'.

    <?php 
    
    use \Magento\Framework\App\Bootstrap;
    include('app/bootstrap.php');
    
    // add bootstrap
    $bootstraps = Bootstrap::create(BP, $_SERVER);
    $object_Manager = $bootstraps->getObjectManager();
    
    $app_state = $object_Manager->get('\Magento\Framework\App\State');
    $app_state->setAreaCode('frontend'); 
    
    $productCollection = $object_Manager->create('\Magento\Catalog\Model\Product')->getCollection();
    
    foreach($productCollection as $product){
    
       $productObject     =  $object_Manager->create('\Magento\Catalog\Model\Product')->load($product->getId());
    
       try{
         $productDesc       = $productObject->getDescription();
         $productShortDesc  = $productObject->getShortDescription();
    
         $productObject->setDescription($productShortDesc);
         $productObject->setShortDescription($productDesc);
    
         $productObject->save();
    
         echo "Product ID :".$productObject->getId()." DESCRIPTIONS Swapped Successfully.<br>";
       }
       catch(Exception $e)
      {
        // errro in exception/code
        print_r('Error in Product Desc Swapping For Product ID : '.$productObject->getId().' : '.$e->getMessage().'<br>');
      }
    }
    
6
  • Thanks for your awesome reply.. But for some reason it didn't quite work? The script ran for several hours (about 10K products in shop), and outputted each products as it should. But all the descriptions are still all under the short description field.
    – Michael
    Commented Nov 15, 2017 at 8:45
  • Ok I will Update the Code Tommorow. Commented Nov 15, 2017 at 13:13
  • Any ide what could be wrong with this? Looks ok to me.
    – Michael
    Commented Nov 21, 2017 at 18:35
  • did you have an idea why the script isn't swapping the descriptions? It ran for several hours and posted ID after each item was processed, but no descriptions was changed though.
    – Michael
    Commented Nov 27, 2017 at 15:26
  • Yes it is actually Swapping product description but it is doing it for Default store view if you swap store view in backend you can see that Descriptions are swapped. Commented Nov 28, 2017 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.