在安装脚本中创建类别属性

时间:2016-10-14 09:19:57

标签: php magento

我想在类别中添加新属性,并更新所有已创建类别的默认值。

我的代码是:

$installer = $this;
$installer->startSetup();

$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'include_in_parent_navigation', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'int',
    'source'        => 'eav/entity_attribute_source_boolean',
    'label'         => 'Include in Parent Navigation',
    'required'      => 0,
    'unique'        => 0,
    'sort_order'    => 11,
    'user_defined'  => 1,
    'default'       => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));

$_categories = Mage::getModel('catalog/category')
    ->getCollection();
foreach($_categories as $category) {
    $category->setData('include_in_parent_navigation', 1);
    $category->save();
}
$installer->endSetup();

添加新属性但不更新现有类别。

1 个答案:

答案 0 :(得分:0)

use this below code 

$_categories = Mage::getModel('catalog/category')
    ->getCollection();
foreach($_categories as $category) {
    $cat=Mage::getModel('catalog/category')->load($category->getId());
    $cat->setData('include_in_parent_navigation', 1);
    $cat->save();
}