0

I want to add new custom category attribute via upgrade script and set value to this attribute for some category.

My code:

$that = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$that->startSetup();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$groupName        = 'Head Block Content';
$entityTypeId     = $that->getEntityTypeId('catalog_product');
$attributeSetId   = $that->getAttributeSetId($entityTypeId, 'Default');
$that->addAttributeGroup($entityTypeId, $attributeSetId, $groupName);


$that->addAttribute('catalog_product', 'head_block_title', array(
'group'             => $groupName,
'label'             => 'Head Block Title',
'input'             => 'text',
'type'              => Varien_Db_Ddl_Table::TYPE_TEXT,
'required'          => false,
'is_configurable'   => false,
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'sort_order'        => 10,
));

$that->updateAttribute($entityTypeId, 'head_block_title', 'is_visible_on_front', 1);

$that->addAttribute('catalog_product', 'head_block_description', array(
'group'             => $groupName,
'label'             => 'Head Block Description',
'input'             => 'textarea',
'type'              => Varien_Db_Ddl_Table::TYPE_TEXT,
'required'          => false,
'is_configurable'   => false,
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'sort_order'        => 20,
));
$that->updateAttribute($entityTypeId, 'head_block_description', 'is_visible_on_front', 1);

$that->addAttribute('catalog_product', 'head_block_link', array(
'group'             => $groupName,
'label'             => 'Head Block Link',
'input'             => 'text',
'type'              => Varien_Db_Ddl_Table::TYPE_TEXT,
'required'          => false,
'is_configurable'   => false,
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'sort_order'        => 30,
));
$that->updateAttribute($entityTypeId, 'head_block_link', 'is_visible_on_front', 1);

$that->addAttribute('catalog_product', 'head_block_link_title', array(
'group'             => $groupName,
'label'             => 'Title of Head Block Link',
'input'             => 'text',
'type'              => Varien_Db_Ddl_Table::TYPE_TEXT,
'required'          => false,
'is_configurable'   => false,
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'sort_order'        => 40,
));
$that->updateAttribute($entityTypeId, 'head_block_link_title', 'is_visible_on_front', 1);

$that->addAttribute('catalog_product', 'head_block_image', array(
'group'             => $groupName,
'label'             => 'Head Block Image',
'input'             => 'image',
'backend'           => 'hastens_catalog/product_attribute_backend_image',
'type'              => Varien_Db_Ddl_Table::TYPE_TEXT,
'required'          => false,
'is_configurable'   => false,
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'sort_order'        => 50,
));
$that->updateAttribute($entityTypeId, 'head_block_image', 'is_visible_on_front', 1);

$that->endSetup();

Second file:

$that = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$that->startSetup();
/** @var Mage_Catalog_Model_Category $category */
$category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Beds');
$category->setHeadBlockImage('category_beds_head.jpg');
$category->setHeadBlockTitle('The best bed, allows for no shortcuts');
$category->setHeadBlockLink('/#');
$category->setHeadBlockLinkTitle('Sea featured bed');
$category->save();

$category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Accessories');
$category->setHeadBlockImage('category_accessories_head.jpg');
$category->setHeadBlockTitle('Accessories');
$category->setHeadBlockDescription('We offer just about everything you can imagine in the line of bed linen, down quilts and pillows, headboards, legs, bath robes, and much more. All with the Ha?stens insignia. You have a personality, and so should your bed.');
$category->save();
$that->endSetup();

My problem: script doesn't update value for categories. But script update custom category attributes when I run Second file again.

  • Where is first file and second file located? – Fabian Blechschmidt Dec 27 '13 at 18:51
  • @ Fabian Blechschmidt .These files were located at module data folder. Answer below help me and I move install attributes scripts to sql folder and now all works. Thanks! – Sergiy Shkirya Dec 28 '13 at 11:38
0

I'm guessing one is sql and the other is data? If so, it looks like perhaps data installation is being run before the attributes are set up.

I would create an initial version that installs the attributes and an upgrade that installs the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.