Magento EAV: What is the 'source' setting used for? - magento

I'm curious what this is used for? I defined the following source model for a custom attribute I added with an entity script, yet I have no idea how to make use of the source attribute. Maybe I can use it a Form Widget? The attribute I added was exportStatus to customer eav.
<?php
class Company_Customer_Model_Customer_Attribute_Source_ExportStatus
extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = array(
array(
'value' => '0',
'label' => 'Pending Export',
),
array(
'value' => '1',
'label' => 'Exported to Mainframe',
),
array(
'value' => '2',
'label' => 'Acknowledged by Mainframe',
)
);
}
return $this->_options;
}
}
and
<?php
class Company_Customer_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'customer' => array(
'entity_model' =>'customer/customer',
'attribute_model' => 'customer/attribute',
'table' => 'customer/entity',
'additional_attribute_table' => 'customer/eav_attribute',
'entity_attribute_collection' => 'customer/attribute_collection',
'attributes' => array(
'export_status' => array(
//'group' => 'Group/Tab',
'label' => 'Customer Export Status',
'type' => 'int',
'input' => 'select',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => 'company_customer/customer_attribute_source_exportStatus',
'global' => 2, //global scope
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
)
)
)
);
}
}

It allows you to create drop down menus.
See this article:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module
Specifically Appendix A: Dropdown Options

Related

Magento Custom Order Attribute

I am trying to add a custom attribute field to the sales_flat_order table in the database for Magento 1.8+. Do I need to create a New Admin theme?
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("order", "my_custom_input_field", array("type"=>"varchar"));
$installer->addAttribute("quote", "my_custom_input_field", array("type"=>"varchar"));
$installer->endSetup();
Try this:
$installer->addAttribute(Mage_Sales_Model_Order::ENTITY, 'THIS_IS_THE_CODE', array(
'group' => 'General',
'type' => 'varchar',
'default' => '0',
'input' => 'text',
'label' => 'My pretty pretty label',
'source' => '',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'user_defined' => false
));
$installer->addAttribute(Mage_Sales_Model_Quote::ENTITY, 'THIS_IS_THE_CODE', array(
'group' => 'General',
'type' => 'varchar',
'default' => '0',
'input' => 'text',
'label' => 'My pretty pretty label',
'source' => '',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'user_defined' => false
));

Magento - Adding Attributes programatically with default and searchable

I've searched everywhere for the answer, muddled through Magento and I can't find a working solution. I'm creating magento attributes dynamically and that's fine, but when it comes to
setting the values
setting the default value
adding more options
making the attribute searchable
nothing seems to work.
Here is my code for adding an attribute
$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->addAttribute('catalog_product', $name, array(
'type' => 'varchar',
'input' => 'select',
'backend' => '',
'frontend' => '',
'label' => $key,
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'unique' => false,
'apply_to' => '',
'is_configurable' => false,
'option' => array(
'values' => array($specific)
)
));
$installer->endSetup();
$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key))->save();
It add's it fine, it even adds the option for me, but I can't seem to set that option as default (to add more later) and I can't make it searchable.
I really hope someone can help.
Thanks
Update:
Ok i've managed to get it adding the default (still not searchable etc) using this code.
$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->startSetup();
$installer->addAttribute('catalog_product', $name, array(
'type' => 'int',
'input' => 'select',
'backend' => '',
'frontend' => '',
'label' => $key,
'class' => '',
'source' => 'eav/entity_attribute_source_table',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'unique' => false,
'apply_to' => '',
'is_configurable' => false,
'option' => array(
'value' => array(
$this->getAttributeName($specific) => array($specific)
)
)
));
$installer->endSetup();
$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key));
$attr->setDefaultValue($attr->getSource()->getOptionId($this->getAttributeName($specific)));
$attr->save();
however when i go to add a new option using this code where $specific = "Pepsi";
$model = Mage::getModel('catalog/resource_eav_attribute');
$option = array();
$option['attribute_id'] = $attr;
$option['value'][$this->getAttributeName($specific)][1] = $specific;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
I get the error: "Default option value is not defined"
Did you have tried with 'is_searchable' => true instead of 'searchable' => true?
Look in to catalaog_eav_attribute. There is a column 'is_searchable' which type is smallint
Try this:
'is_searchable' => 1,
Referring to the error:
"Default option value is not defined"
When you add the option: ---------------------------------------> /
$option['value'][$this->getAttributeName($specific)][1] = $specific;
Here the [0] is the option's "default admin value" and [1] is the "default store value".
$option['value'][$this->getAttributeName($specific)][0] = $specific;
Your code is not making $specific the default option, but setting $specific the defaults store's value.

Magento Set order of added category attribute

How can I set the order of an added category attribute following this installation script
<?php
$installer = $this;
echo "installing";
$installer->startSetup();
$this->addAttribute('catalog_category', 'title_recipe', array(
'group' => 'General Information',
'input' => 'text',
'order' => '999',
'type' => 'text',
'label' => 'Title Recipe',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$this->endSetup();
$this->addAttribute('catalog_category', 'title_recipe', array(
'group' => 'General Information',
'input' => 'text',
'position' => 999,
'type' => 'text',
'label' => 'Title Recipe',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));

Add category attribute with WYSIWYG enabled

I'm following this tutorial: http://www.atwix.com/magento/add-category-attribute/
All is working well, attributes are added to categories, but without the WYSIWYG button below the field. WYSIWYG is enabled in System > Config > Content Management.
$this->startSetup();
$this->addAttribute('catalog_category', 'custom_att', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'My attribute',
'backend' => '',
'visible' => true,
'required' => false,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Whatever I try, WYSIWYG is not enabled for my attributes. Can anyone help? Or maybe there is a workaround for this?
EDIT: I searched other posts but all say that this code should add the WYSIWYG:
'wysiwyg_enabled' => true,
but it doesn't.
Tried to accomplish same task today and searching through magento code managed to complete my task with this code:
$productEntityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->addAttribute($productEntityTypeId, 'some_text', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'Some Text',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->updateAttribute($productEntityTypeId, 'some_text', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute($productEntityTypeId, 'some_text', 'is_html_allowed_on_front', 1);
This works:
$installer->updateAttribute('catalog_category', 'certifications', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute('catalog_category', 'certifications', 'is_html_allowed_on_front', 1);
This worked for me:
$installer->addAttribute('catalog_category', 'short_description', array(
'type' => 'varchar',
'label' => 'Short Description',
'input' => 'textarea',
'default' => '',
'sort_order' => 1,
'required' => false,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'group' => 'General Information',
));
Notice the following three entries:
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
Using Magento CE 1.9.2.0.
Create php file in magento root directory and paste below code and run it from browser:-
ini_set('display_errors',0);
require_once 'app/Mage.php';
Mage::app();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
function createNewAttributeSet($name) {
Mage::app('default');
$modelSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId(4) // 4 == "catalog/product"
->setAttributeSetName($name);
$modelSet->save();
$modelSet->initFromSkeleton(4)->save(); // same thing
}
// Replace your attribute name with "extra_info"
$setup->addAttribute('catalog_category', 'extra_info', array(
'group' => 'General Information',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Extra Information',
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'input' => 'textarea',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'position' => 1,
));
$setup->updateAttribute('catalog_category', 'extra_info', 'is_wysiwyg_enabled', 1);
$setup->updateAttribute('catalog_category', 'extra_info', 'is_html_allowed_on_front', 1);

Drop down attribute in category - Magento

I need a dropdown attribute in category. I tried to do this in installer:
$installer = $this;
$installer->startSetup();
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar',
'label' => 'Priority2',
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'option' => array(
'value' => array(
0 => array(''),
1 => array('normal'),
2 => array('grouping'),
3 => array('highlighted'),
4 => array('trendy'),
),
),
'default' => array(0),
));
$installer->addAttributeToGroup(
$entityTypeId, $attributeSetId, $attributeGroupId, 'priority', '11'
);
$attributeId = $installer->getAttributeId($entityTypeId, 'priority');
$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
FROM `{$installer->getTable('catalog_category_entity')}`;
");
Mage::getModel('catalog/category')
->load(1)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
Mage::getModel('catalog/category')
->load(2)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
$installer->endSetup();
but it doesn't work. Nothing shows up in Catalog->categories->Manage Categories. I can create only textfield attribute like this:
$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar',
'label' => 'Priority2',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
));
Do you have any ideas?
Thx for help.
EDIT.
I also tied SQL query like this:
INSERT INTO eav_attribute (entity_type_id, attribute_code, attribute_model, backend_model, backend_type, backend_table, frontend_model, frontend_input, frontend_label, frontend_class, source_model, is_required, is_user_defined, default_value, is_unique, note) VALUES
(9, 'priority', NULL, 'NULL', 'int', 'NULL', 'NULL', 'select', 'Priority', NULL, 'eav/entity_attribute_source_table', 1, 0, 0, 0, '');
INSERT INTO eav_attribute_option (option_id,attribute_id,sort_order) VALUES
(1500,282,0),
(1501,282,1),
(1502,282,2),
(1503,282,3),
(1504,282,4);
INSERT INTO eav_attribute_option_value (value_id,option_id,store_id,value) VALUES
(201,1500,0,''),
(202,1501,0,'normal'),
(203,1502,0,'grouping'),
(204,1503,0,'highlighted'),
(205,1504,0,'trendy');
INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES
(9, 12, 7, (SELECT atribute_id FROM eav_attribute WHERE attribute_code='priority'), 2);
It's possible using module installer:
$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'myattribute', array(
'type' => 'varchar',
'label' => 'My attribute',
'source' => 'module/category_attribute_myattribute',
'backend' => 'module/category_attribute_backend_myattribute',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'input' => 'select',
'visible' => true,
'required' => true,
'user_defined' => false,
'used_in_product_listing' => false,
'group' => 'Group name',
'default' => Module_Catalog_Model_Category_Attribute_Myattribute::DEFAULT_Groupname,
));
Hope it helps someone ;)
Please refer the tutorial for file structure and step by step explanation.
http://www.pearlbells.co.uk/how-to-add-custom-dropdown-attribute-to-magento-category-section/
<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'text',
'label' => 'Custom Dropdown',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'customfeaturedattribute/source_custom',
));
$this->endSetup();
And in the model class add the options.
public function getAllOptions()
{
$options = array(
1 => 'One',
2 => 'Two',
3 => 'Three'
);
return $options;
}

Resources