【问题标题】:Adding a Category Attribute in Magento 1.8.1在 Magento 1.8.1 中添加类别属性
【发布时间】:2014-05-21 11:14:17
【问题描述】:

我目前正在尝试在 Magento 1.8.1 的类别管理屏幕中添加一个新的类别属性,但我在显示任何内容时遇到了问题。

我能找到的唯一代码示例包括 mysql4,但是我认为它已经被淘汰了?请任何人都可以在这里指出我们正确的方向。

我可以在 Config > Advanced 和 core_resources 表中看到我的扩展。但不在网站的前端。

【问题讨论】:

  • 你能分享安装脚本中的文件名和代码吗?
  • 使用mysql4-install-0.0.1.php和config.xml

标签: magento magento-1.8


【解决方案1】:

在您的 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();   
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);

if (!$installer->getAttributeId($entityTypeId, 'shipping_content')) {
    $installer->addAttribute('catalog_category', 'shipping_content', array(
         'type'              => 'text',
'backend'           => '',
'frontend'          => '',
'label'             => 'Short description',
'input'             => 'textarea',
'class'             => '',
'source'            => '',
'global'            => '0',
'visible'           => true,
'required'          => false,
'user_defined'      => true,
'default'           => '',
'searchable'        => false,
'filterable'        => false,
'comparable'        => false,
'visible_on_front'  => true,
'used_in_product_listing' => false,
'unique'            => false,
'wysiwyg_enabled'   => true,
'apply_to'          => '',
'is_configurable'   => true
    ));


    $installer->updateAttribute($entityTypeId, 'shipping_content', 'is_wysiwyg_enabled', 1);
    $installer->updateAttribute($entityTypeId, 'shipping_content', 'is_html_allowed_on_front', 1);


}


$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_category', 'shipping_content');
 $installer->endSetup();

?>

【讨论】:

    【解决方案2】:

    @h3nr1ke 您可以通过以下方式获取属性:

    $category = Mage::registry('current_category');
    if ($category){
       $value = $category->getData('YOUR_CUSTOM_ATTRIBUTE_CODE');
    }
    

    【讨论】:

      【解决方案3】:

      将文件名从mysql4-install-0.0.1.php更改为install-0.0.1.php

      【讨论】:

        【解决方案4】:

        我们最近在 1.8.2.0 中进行了尝试。你真的不需要创建一个模块来添加一个类别属性,once。浏览这么多文件cruft 只安装一次似乎太浪费了。

        类别属性在安装后往往会永久保留,因此对我们来说更好的方法是使用一次性脚本。把这个保存在 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_Sales_Model_Mysql4_Setup;
        
            // change details below:
            $attribute  = array(
                'type' => 'int',
                'label'=> 'Your attribute label',
                'input' => 'text',
                'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => "",
                'group' => "General Information"
            );
        
            $installer->addAttribute('catalog_category', 'your_attribute_code', $attribute);
        
            $installer->endSetup();
        

        将其保存为 add_category_attribute.php 或其他让您难忘的东西。

        您可以使用浏览器访问此文件,或使用php-cli 运行此文件:

        php -f add_category_attribute.php
        

        祝你好运。

        【讨论】:

        • 请注意:如果您有兴趣让这个新类别属性接受文本而不仅仅是整数,请务必将 'type' ==> 'int' 更改为 'text';如果您需要文本区域,请将 'input'==>'text' 更改为 'textarea';请事先考虑这一点,否则您的类别部分会出现许多错误的类别字段
        • 那行得通...快速的问题...热门获得前面的价值?谢谢
        • 太好了,它工作正常。但这是在类别中添加属性的标准方法吗?
        • 正如我在回答开头提到的那样,通常这是通过一个包含迁移文件的模块来完成的。但通常这更难,所以如果你不能打扰 - 使用一次性脚本,例如上面的。 @dineshkashera
        猜你喜欢
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多