首页 文章

如何使用安装程序/更新脚本和Magento抽象更新Magento产品的值?

提问于
浏览
4

我使用安装程序脚本向我的Magento应用程序产品实体添加了一个自定义eav属性(基本上,遵循此处描述的过程:Installing Custom Attributes with Your Module) . 现在,我想使用更新脚本根据某些条件(基于产品类别)更改(填充)每个产品的此属性的值 . 我尝试使用的脚本基本上是这样的:

$attributeValues = array(...) // Map from $productId to the desired  $value
$product = Mage::getModel('catalog/product');
foreach($attributeValues as $productId=>$value){
    $product->load($productId)->setMyAttribute($value);
    $product->save();
}

我的问题是:在更新脚本中使用这种抽象级别(Mage :: getModel('catalog / product')及其方法)是否可以?如果不是,您会如何建议使用更新脚本更改这些属性值(不需要sql)?

我使用的脚本(直到现在)没有工作并且因错误而失败:

Call to a member function getStoreIds() on a non-object

在magento核心文件中 .

我不知道这个错误是Magento错误还是我如何使用更新脚本的问题 .

我正在使用Magento 1.4.0.1

2 回答

  • 2

    尝试在sql升级脚本中添加 Mage::app()->setUpdateMode(false) . 例如

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');;
    $installer->startSetup();
    Mage::app()->setUpdateMode(false);
    Mage::app()->setCurrentStore('your_store_code_here');
    

    如果您查看 Mage::app()->getStore() ,您将看到以下代码段,该代码段返回保存产品所需的不正确的商店 .

    if (!Mage::isInstalled() || $this->getUpdateMode()) {
            return $this->_getDefaultStore();
        }
    
  • 1

    数据更新脚本是可行的方法

    只需使用数据升级脚本即可 . 这是放在 data 文件夹而不是 sql 文件夹中的脚本 . 这些脚本稍后运行,然后数据库结构更新,并允许访问更多功能 .

    示例文件名:

    app/code/local/My/Module/data/your_setup/data-install-0.1.0.php
    app/code/local/My/Module/data/your_setup/data-upgrade-0.1.0-0.2.0.php
    

    is already available in Magento 1.4 .

相关问题