Magento-通过设置脚本以编程方式创建自定义产品属性 [英] Magento - Programmatically create custom product attribute via set up script

查看:119
本文介绍了Magento-通过设置脚本以编程方式创建自定义产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行创造了Magento管理附加产品属性的脚本.但是,该属性不会出现在管理后端.

I am trying to run a script that creates additional product attribute in magento admin. However the attribute doesn't come out to the admin backend.

这是我的模型:

This is my model: Setup.php

class Rts_Cattribute_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
protected function _prepareValues($attr)
{
    $data = parent::_prepareValues($attr);
    $data = array_merge($data, array(

        'apply_to'                      => $this->_getValue($attr, 'apply_to'),
        'frontend_input_renderer'       => $this->_getValue($attr, 'input_renderer'),
        'is_comparable'                 => $this->_getValue($attr, 'comparable', 0),
        'is_configurable'               => $this->_getValue($attr, 'is_configurable', 1),
        'is_filterable'                 => $this->_getValue($attr, 'filterable', 0),
        'is_filterable_in_search'       => $this->_getValue($attr, 'filterable_in_search', 0),
        'is_global'                     => $this->_getValue(
            $attr,
            'global',
            Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE
        ),
        'is_html_allowed_on_front'      => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
        'is_searchable'                 => $this->_getValue($attr, 'searchable', 0),
        'is_used_for_promo_rules'       => $this->_getValue($attr, 'used_for_promo_rules', 0),
        'is_visible'                    => $this->_getValue($attr, 'visible', 1),
        'is_visible_on_front'           => $this->_getValue($attr, 'visible_on_front', 1),
        'is_wysiwyg_enabled'            => $this->_getValue($attr, 'wysiwyg_enabled', 0),
        'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
        'position'                      => $this->_getValue($attr, 'position', 0),
        'used_for_sort_by'              => $this->_getValue($attr, 'used_for_sort_by', 0),
        'used_in_product_listing'       => $this->_getValue($attr, 'used_in_product_listing', 0)
    ));
    return $data;
}
}

这是我的配置:config.xml

<config>
<modules>
    <Rts_Cattribute>
        <version>0.1.0</version>
    </Rts_Cattribute>
</modules>
<global>
    <resources>
        <cattribute_setup>
            <setup>
                <module>Rts_Cattribute</module>
                <class>Rts_Cattribute_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
        </cattribute_setup>
    </resources>
</global>
</config>

最后这是我的安装脚本:mysql4-install-0.1.0.php

Lastly this is my install script: mysql4-install-0.1.0.php

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'max_ftlbs', array(
    'type'              => 'int',
    'backend'           => '',
    'frontend'          => '',
    'label'             => 'Max Ft.Lbs',
    'input'             => 'text',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => false,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false
));

$installer->endSetup();

我遵循这个教程

I followed this tutorials

http://magento4newbies.blogspot的.com/2015/01/如何对添加定制产品-attributes.html

http://codegento.com/2011/02/install-scripts-and-upgrade-scripts/

在此链接在Magento使用设置添加定制产品属性脚本 它说,对于Magento的安装程序的标准类是但随着产品打交道时,你需要使用<4>代替.

In this link Adding custom product attributes in Magento using setup script it says that The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead.

我试过很多解决方案但没有奏效.

I've tried many solution however it didn't work.

每次我做了一些改变,我总是删除该资源的版本我刷新页面之前.

Everytime I do some changes I always delete the resource version before I refresh the page.

在自定义属性不会显示出来,或者它没有在管理后端创建.

The custom attribute won't show up or it is not created in the admin backend.

什么为Magento的安装程序的右标准类是?

What is the right standard class for magento installer?

我的安装程序正确吗?

我希望你能帮助我.

推荐答案

如果您使用脚本来创建你需要,当你从一个数据库中的文件移动到另一个每次运行脚本的自定义属性(本地分期-Live).

If you use the script to create the custom attribute you need to run the script each time when you are moving the files from one database to another (Local-Staging-Live ).

但是,我们创建一个使用模块是非常容易维护和管理.

But we create using the module it is very easy to maintain and manage.

谢谢

这篇关于Magento-通过设置脚本以编程方式创建自定义产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆