php script to fix text in magento

82 Views Asked by At

I have several products in magento with an incorrect product description.

Example: "This text has some mistakes."

As you can see, the text uses   as a placeholder. I want to replace each   with a real placeholder.

I want to write a php script that will load the description from the database, fix the text and then save it.

Do you know how I could do that?

Thank you!

2

There are 2 best solutions below

0
On

You can use str_replace .

Create a file Updatedesc.php inside your magento root folder and write the below code in that file and then execute it with below URL http://www.yourdomain.com/Updatedesc.php

<?php
require_once('app/Mage.php'); 
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
               ->getCollection()->addAttributeToSelect('*');
foreach($_productCollection as $_product)
{   
     try
     {
          $description = $_product->getDescription();
          $newDescription = str_replace(" ","|", $description);
          $_productCollection->setDescription($newDescription);
          $_productCollection->save();
     }
     catch(Exception $e){
          echo $e->getMessage();
     }
}
3
On

Thank you for the help! I don't understand what this does: $_productCollection->setStoreId(2); I want to change the description of every product, so I need to add a foreach. I came up with this:

    $_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_product){
    $description = $_product->getDescription();
    $newDescription = str_replace(" ","|", $description);
    $_productCollection->setDescription($newDescription);
    $_productCollection->save();
}