简体   繁体   中英

Replace Product Names with a Loop

I have a Magento webshop with several products in it. All of them begin with the word PRODUCT ,

for example

PRODUCT 001. 

I want to replace the word "PRODUCT" with "Article" .

I tried to write a small script, but it's not working at all. Here is what I've got so far:

<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
foreach($_productCollection as $_product)
{
     $name = $_product->getName();
     $new = str_replace("PRODUCT","Article", $name);
     $_product->setName($new);
     $_product->save();
};
?>

I did not really know how to run the script, so I added this into a CMS page and opened it:

{{block type="core/template" template="script.phtml"}}

What's wrong with it?

Create a file Replacename.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/Replacename.php

<?php
require_once('app/Mage.php'); 
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
               ->getCollection()->addAttributeToSelect('*');
foreach($_productCollection as $_product)
{   
     try
     {
        $name = $_product->getName();
        $new = str_replace("PRODUCT","Article", $name);
        $_product->setName($new);
        $_product->save();
     }
     catch(Exception $e){
          echo $e->getMessage();
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM