【问题标题】:Magento - Programmatically create custom product attribute via set up scriptMagento - 通过设置脚本以编程方式创建自定义产品属性
【发布时间】:2015-09-23 07:36:17
【问题描述】:

我正在尝试运行一个在 magento admin 中创建附加产品属性的脚本。但是该属性不会出现在管理后端。

这是我的模特: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

$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();

我按照本教程进行操作

http://magento4newbies.blogspot.com/2015/01/how-to-adding-custom-product-attributes.html

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

在这个链接Adding custom product attributes in Magento using setup script 它说magento安装程序的标准类是Mage_Eav_Model_Entity_Setup,但是在处理产品时,你需要使用Mage_Catalog_Model_Resource_Setup

我尝试了很多解决方案,但都没有成功。

每次我进行一些更改时,我总是在刷新页面之前删除资源版本。

问题:

自定义属性不会显示或未在后台管理中创建。

问题:

magento 安装程序的正确标准类是什么?

我的安装程序正确吗?

希望你能帮我解决这个问题。

【问题讨论】:

  • app/etc/modules 中有xml文件吗?例如Rts_Cattribute.xml 0.1.0
  • 是的,我已经包含了它,但是属性不显示,但版本已经在 core_resource 中。
  • 也许你需要重新索引并清除缓存。你检查过数据库中的属性吗?
  • 我总是清除我的缓存,但没有创建属性,但是版本已在 core_resource 表上注册。
  • 如果您运行了一次设置,并且您的版本的设置表中有一行,则设置脚本不会再次运行。您需要将其从数据库中删除。

标签: php magento


【解决方案1】:

我注意到我必须做的一件事是将数据库连接信息放在 config.xml 类中。

<catalog_setup>
            <setup>
                <module>XX_YY</module>
                <class>XX_YY_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </catalog_setup>
        <catalog_setup>
            <connection>
                <use>core_write</use>
            </connection>
        </catalog_setup>
        <catalog_setup>
            <connection>
                <use>core_read</use>
            </connection>
        </catalog_setup>

在安装程序类中,我将安装程序设置为等于:

$installer = Mage::getResourceModel('catalog/setup','catalog_setup');

【讨论】:

    【解决方案2】:

    对于创建产品属性,无需创建模块,只需在 Magento 根文件夹中的脚本下方运行即可。

    <?php  
    
    require_once('app/Mage.php');
     Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
    
    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $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();
    
    ?>
    

    用于删除产品属性

    <?php  
    
    require_once('app/Mage.php');
     Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
     $installer = new Mage_Sales_Model_Mysql4_Setup;
     $installer->startSetup();
     $installer->removeAttribute('catalog_product', 'max_ftlbs');
     $installer->endSetup();
    
    ?>
    

    【讨论】:

      【解决方案3】:

      如果您使用脚本创建自定义属性,则每次将文件从一个数据库移动到另一个数据库时都需要运行该脚本(Local-Staging-Live)。

      但是我们使用创建的模块非常容易维护和管理。

      谢谢

      【讨论】:

        【解决方案4】:

        本教程逐步说明如何使用设置脚本添加自定义产品属性和文件结构。

        http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento-using-setup-script/

        <?php
        $this->startSetup();
        $this->addAttribute(catalog_product, 'featured_product', array(
        'group'         => 'General',
        'input'         => 'select',
        'type'          => 'text',
        'label'         => 'Featured Product',
        'backend'       => '',
        'visible'       => true,
        'required'      => false,
        'visible_on_front' => true,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'source' => 'eav/entity_attribute_source_boolean',
        'sort_order'        => 8,
        ));
        
        $this->endSetup();
        

        【讨论】:

          猜你喜欢
          • 2016-09-14
          • 1970-01-01
          • 1970-01-01
          • 2014-02-10
          • 2012-10-07
          • 1970-01-01
          • 2021-04-17
          • 2012-11-15
          • 2023-04-04
          相关资源
          最近更新 更多