【问题标题】:Magento: Adding new attribute to grouped products in setup scriptMagento:在设置脚本中为分组产品添加新属性
【发布时间】:2012-02-19 16:43:25
【问题描述】:

我正在为分组产品开发一个小型 Magento 扩展。这个扩展需要另一个属性,所以我想我可以编写一个设置脚本,为分组产品添加一个新属性。但正如我在 Magento 中尝试做的几乎所有事情一样,结果比我预期的要复杂得多。 Magento 官方论坛并没有真正的帮助,所以我希望在这里得到一些支持:)

新属性应该只出现在分组产品的“常规”标签中;简单产品、可配置产品、捆绑产品等应保持不变。该属性应该独立于所选属性集而存在,就像它是系统属性一样。

为此,我想我可以将属性添加到分组产品的实体中,但我发现,分组产品没有特殊实体,一般产品只有实体“catalog_product”。因此我的下一个想法是,我需要将属性添加到“catalog_product”实体,然后将其分配给正确的属性组,以便它仅适用于分组产品。

问题是我还没有深入到 Magento,我完全不知道我应该如何找到相应的属性组,或者我的想法是否可行,也许我完全走错了轨道:/

只是为了让你知道我到目前为止得到了什么: 我在扩展的配置文件中注册了我的设置脚本,它正在执行,唯一的问题是设置脚本本身,它看起来像下面的 atm,因为 - 正如我所说 - 我还不知道:

$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_product", "my_attrib_name", array( /* just a placeholder */ ));
$installer->endSetup();

非常基本的...

【问题讨论】:

    标签: php magento attributes installation add


    【解决方案1】:

    我现在想出了如何自己做。我的方法是正确的,我只需要找到对应的参数。

    addAttribute() 调用现在如下所示:

    // ...
    $installer->addAttribute(
        "catalog_product", // Entity the new attribute is supposed to be added to
        "my_attrib_code", // attribute code
         array( // Array containing all settings:
            "type" => "varchar",
            "label" => "My attribute",
            "note" => "Insert additional information about the attribute here",
            "input" => "text",
            "global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
            // Dont know if this is really necessary, but it makes sure 
            // the attribute is created as a system attribute:
            "user_defined" => false,
            // This makes sure the attribute only applies to grouped products
            "apply_to" => Mage_Catalog_Model_Product_Type::TYPE_GROUPED
        )
    );
    // ...
    

    现在安装程序添加的属性是系统属性,它会自动添加到每个属性集的“常规”组中,并且不能更改/移动。如我所愿,它也仅适用于分组产品。

    【讨论】:

    • 哇,这真的对我有用!也许为了让它变得非常整洁,使用它作为addAttribute() 的第一个参数:Mage::getModel('catalog/product')->getResource()->getEntityType()->getId() 或使用Mage_Catalog_Model_Product::ENTITY
    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多