Magento商店的SQL替换功能

时间:2015-07-06 06:33:13

标签: sql magento

我想替换我的magento商店中每件商品的产品说明中的某些字母。有一种方法可以通过phpMyAdmin(SQL)运行脚本。有一个REPLACE函数:

REPLACE( string1, string_to_replace, replacement_string )

但我怎么做其余的代码?如何访问包含其中描述的单元格?

提前致谢!

编辑:更多详情:

我想创建一个循环遍历每个产品的SQL语句,并在REPLACE函数的帮助下更改产品描述的部分内容。

1 个答案:

答案 0 :(得分:1)

您应该避免在Magento实例上执行直接SQL。如果要更新产品描述,可以使用目录模型 - 甚至导出/导入例程。以下是如何以编程方式执行此操作 - 创建以下内容并将其放在Magento网站的根目录中。

include_once "app/Mage.php";
Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$yourstring = 'The string you want to find';
$newstring = 'The string you want to replace it with';

$_productCollection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->load();

foreach($_productCollection as $_product) {

if (strpos($_product->getDescription(),$yourstring) !== false) {

    $newdescription = str_replace($yourstring,$newstring,$_product->getDescription());
    $_product->setDescription($newdescription);
    $_product->save();

    echo 'Updated product: '.$_product->getName().'<br />';
}

}
相关问题