I am currently trying to add a new category attribute to the category admin screen in Magento 1.8.1, I am having issues with getting anything to show though.

The only code examples I can find include mysql4, however I thought this had been retired? Please can anyone point us in the right direction here.

I can see my extension under Config > Advanced and in the core_resources table. But not in the front end of the site.

有帮助吗?

解决方案

We've tried this recently with 1.8.2.0. You don't really need to create a module just to add a category attribute, once. It seems such a waste to go through so much file cruft to get something installed just once.

Category attributes tend to stay permanently once installed so what worked better for us is to just use a one-off script. Save this one right at magento root.

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

Save it as add_category_attribute.php or something else memorable for you.

You can either use the browser to get to this file, or use php-cli to run this:

php -f add_category_attribute.php

Good luck.

其他提示

Change the file name from mysql4-install-0.0.1.php to install-0.0.1.php

@h3nr1ke You can get attribute with:

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

Run this script in your magento root folder to create Attribute

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

?>

For Remove Category Attribute

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

?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top