Magento - Add global product attribute - magento

I can not really find a satisfying answer to the following question:
What would be the best practice for a module to add a new product attribute to all attribute_sets (and possibly at a certain location).
All approaches I can find seem to refer to adding a new attribute to just one attribute set.

There is a magento way for it
Example:
$installer->addAttribute('catalog_product', 'test_me', array(
'label' => 'test Me',
'input' => 'textarea',
'type' => 'text',
'class' => '',
'global' => true,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'apply_to' => 'simple,configurable',
'visible_on_front' => true,
'is_configurable' => false,
'wysiwyg_enabled' => true,
'used_in_product_listing' => true,
'is_html_allowed_on_front' => true,
'group' => 'Config',
'sort_order' => 25
));
To add attribute to every attribute_set you need to set 'user_defined' = true and set 'group' = 'your_group_name'. If needed magento will add your group to every attribute set, and attach attribute to it.

Once you add an attribute you may assign it to one or all attribute sets, it isn't unique to the first set you assign it to. Does this answer your question?

Related

How to create a new tab in products in Magento admin panel

I just started to work with Magento. Cool thing. But I think I jumped in the middle of it so fast. However, I've been asked to create a new tab called Promotions when adding a Product. It will then have an option field (named "Is Featured") and it has values of "Yes" or "No" in form of a dropdown.
I am familiar with the structure of Magento, but I can't find where I can do the changes.
I'm using Magento 1.9.2.3 Full release.
I made an image for you guys to understand what I want better.
What should I do?!
You will have to first create the attribute:
open Catalog > Attributes > Manage Attributes
click on Add New Attribute
create a new boolean attribute (chose Yes/No in "Catalog Input Type for Store Owner") and fill the other fields
Then add the attribute to the Promotions group:
open Catalog > Attributes > Manage Attribute Sets
chose the attribute set you are going to use for this product
add a new "Group" to the column on the left with the Add New button (in this case, name it "Promotions")
drag and drop the newly created group to the position you'd like it to be
drag and drop the newly created attribute from the right column to the left one, under the Promotions group
In the installer of any of your suitable extension, add an attribute to the product with a new group, and they will be displayed there.
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'is_featured',
array(
'group' => 'Promotions',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Is Featured',
'input' => 'select',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'sort_order' => 60
));
$installer->endSetup();
It will look like that:
https://gyazo.com/9a0e423d0c3e78e7c440b5cd79a3e547
Tweak attribute settings according to your requirements.

Magento How to add a varchar attribute whit addAttribute specifiying a max length

I'm working with magento and I'm trying to add a new category attribute with type varchar(50). I've added the new attribute with:
$installer->addAttribute('catalog_category', 'shortdesc', array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'DescripciĆ³n Corta',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'position' => 1,
));
But the max lenth is 255. How can I change attribute length to 50?
Indeed, Magento hardcodes the 255 value (as of Community Edition 1.8.1.0) and doesn't accept parameters to replace it.
This should not be a problem for you, as VARCHAR(255) is the max number of characters that can be stored, but it wont take more space than it needs if you store any less than that. If you really need a hard limit, you can always add code to observe the before save event for categories and strip your string there.
Or, in the extreme case you really want this hard limit on the database, you could alter the table and modify the column.

Magento - Add custom attribute to order

I'm trying to add a custom field to my orders. At this moment, I found the post bellow that helped me to create such attribute in my database:
http://fabrizioballiano.net/2011/11/15/create-a-custom-order-attribute-in-magento/
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;
$attribute = array(
'type' => 'int',
'backend_type' => 'text',
'frontend_input' => 'text',
'is_user_defined' => true,
'label' => 'My Label',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'default' => 0
);
$installer->addAttribute('order', 'special_attribute', $attribute);
$installer->endSetup();
After executing the code above and creating several orders, I'm able to loop through all orders and see the default value to the every order.
The question is, how can I store the data I want in this field? How can I retrieve such data?
Thanks!
Add this to the gobal scope in config.xml. Then simply set the attribute in the quote - it gets automagically transferred to the order in the quote to order conversion process.
<global>
...
<fieldsets>
<sales_convert_quote>
<your_special_attribute>
<to_order>*</to_order>
</your_special_attribute>
</sales_convert_quote>
</fieldsets>
...
</global>
You can retrieve/set the attribute at any time via the magic getter/setter e.g.
$quote->getYourSpecialAttribute()
$order->getYourSpecialAttribute()
$quote->setYourSpecialAttribute()

Magento - Install EAV Attribute via Installer

Hey guys im using the installer in my module to add a new EAV product attribute to the Default attribute set. So far its working great, but there are 2 little things that bother me.
public function getDefaultEntities(){
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'disable_sale' => array(
'group' => 'General',
'label' => 'Disable Sale',
'type' => 'int',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'is_visible_on_front' => true,
'used_in_product_listing' => true,
)
)
)
);
}
I want my attribute set to be visible on product details page and catalog listing per default.
'is_visible_on_front' => true,
'used_in_product_listing' => true,
The problem is that both values are not set to be visible.
What am i doing wrong?
I haven't done this with attributes per-se, but try both 'integers' 1 AND 'string' "1" in your code and see if it works.
is_visible_on_front change to visible_on_front and then check.
Found the answer here at stackoverflow:
Magento module setup/installer script
Setup class should extend from
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
Now the installer is aware of the additional attributes and its working like a charme.

Set front end label for customer attribute - magento

I have a customer attribute which I setup using this script (I've only pasted part of it, the part related to the attribute)
$setup->addAttribute('customer', 'age', array(
'label' => 'Age',
'type' => 'int',
'input' => 'select',
'user_defined' => true,
'source' => 'eav/entity_attribute_source_table',
'visible' => true,
'required' => false,
'visible_on_front' => true
));
I would like to set the front end label of the attribute to "How old are you?" but keep the Admin label to "Age". How could I do this?
Thanks in advance,
Ok I managed after looking into magento
$labels = array();
$labels[0] = 'Age';//default store label
$labels[1] = 'Label for store with id 1';
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'age');
$oAttribute->setData('store_labels', $labels);
$oAttribute->save();
This did the trick.
Hope it helps someone else.

Resources