Magento how to add new attribute to sales/order model? - magento

I want to add one attribute to sales/order, that is the Mage_Sales_Model_Order, to store some extra data.
I found the resource of sales/order uses normal table. And when I save an order, it only saves the fields that match the columns in the table.
What is the right way to add this attribute?

I would recommend a hybrid of a few of the answers below. You definitly want to put this code in a install script via config xml but your going to want to use the Mage_Eav_Model_Entity_Setup as your setup class if you want to leverage the addAttribute function. So your config xml will look something like this...
<config>
...
<resources>
<modulename_setup>
<setup>
<module>Your_Module</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</modulename_setup>
</resources>
...
</config>
and your install script should look something like this
$installer = $this;
/* #var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer->startSetup();
$installer->addAttribute('sales_order', 'attributename', array(
'group' => 'General',
'label' => 'Label frontend',
'note' => '',
'type' => 'string', //backend_type
'input' => 'text', //frontend_input
'frontend_class' => '',
'source' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => false,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));
$installer->endSetup();
Keep in mind this is untested code, and you might need to tweak some of the attribute options to get it to function the way you need it to.

To add new attribute on order you use this code, but just one time. Get any file and put the code there, execute one time and can remove the code.
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('sales_order', 'attributename', array(
'group' => 'General',
'label' => 'Label frontend',
'note' => '',
'type' => 'string', //backend_type
'input' => 'text', //frontend_input
'frontend_class' => '',
'source' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => false,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));

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

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

Magento : Add an additional field under order form for admin use only

I need two additional fields for adding additional notes with each order besides comments...
I have trying running this script found on the web
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' => 'text',
'backend_type' => 'text',
'frontend_input' => 'text',
'is_user_defined' => true,
'label' => 'Your attribute label',
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'default' => ''
);
$installer->addAttribute('order', 'your_attribute_code', $attribute);
$installer->endSetup();
But after running and checking the order form there were no additional field beside comments.... any one has any idea about how i could do it..

Magento: add new attribute to all products

I want to add a new attribute to all products. I have done it with a install script trough
$installer = $this;
$installer->startSetup();
$this->addAttribute('catalog_product','test2',array(
'label' => 'test2',
'type' => 'varchar',
'visible' => true,
'required' => false,
'required' => 0
));
But how can I add values to this attribute by
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttributeGroup($entityTypeId, 'Default', 'test2', 0);
$installer->endSetup();
This is one of the sample code which I had used to create my own custom Product Attribute:-
$installer = $this;
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer->startSetup();
$attrCode = 'test2';
$attrGroupName = 'Test Group';
$attrLabel = 'Test 2';
$attrNote = 'Test Note';
$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);
if ($attrIdTest === false) {
$objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
'group' => $attrGroupName,
'sort_order' => 7,
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => $attrLabel,
'note' => $attrNote,
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'visible_on_front' => false,
'unique' => false,
'is_configurable' => false,
'used_for_promo_rules' => true
));
}
$installer->endSetup();
This is used with the references of these two articles:-
Magento Wiki: Installing Custom Attributes with Your Module
Mukesh Chapagain Blog: Magento Adding attribute from MySQL setup file
Also, you will find that I have used the array key "group" to mention the Attribute Group Name, where this new custom Attribute will reside. The irony is that mentioning of this key, in the above code sample, automatically creates this Attribute in every Attribute Set found in this Magento.
So you do not need to call any method (like "addAttributeToSet()") to add this Attribute to all Attribute Sets.
Hope it helps.
Run this script in your magento root directory.(Change Configuration you need)
<?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();
$installer->addAttribute('catalog_product', 'snum', array(
'label' => 'Serial No',
'type' => 'int',
'input' => 'text',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'visible_in_advanced_search' => false,
'unique' => false
));
$installer->endSetup();
?>
For Remove Product 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_product', 'snum');
$installer->endSetup();
?>
you can add custom attribute to the magento backend as shown.If you create the product attribute as module it is easy to move from one database to another.
?php
$this->startSetup();
$this->addAttribute(catalog_product, 'featured_product', array(
'group' => 'General',
'input' => 'select',
'type' => 'text',
'label' => 'Featured Product',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'eav/entity_attribute_source_boolean',
'sort_order' => 8,
));
$this->endSetup();
please refer my tutorial for step by step explanation and file structure.
http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento/

Magento: how do I add a column to products selection in admin category page?

I would like to change the admin category page so that in the products tab I also have the product type as a column. In that way I will be able to rapidly add configurable master products without having to sift through their simple child products.
An alternative option - or additional column - would be to have the visibility column with the usual catalog/search, search, catalog options.
I have tried #clockworkgeek's intro to the topic here: Add column to Magento admin catolog > manage products
But I need more pointers at the 'add sql here' part.
you should add this file: app/code/local/[your company]/[you app]/Model/Resource/Eav/Mysql4/Setup.php
class [YourNameSpace]_[YourModule]_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup{
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(
'**Name of your column 1**' => array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Your Column Label ...',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 1,
'group' =>'General',
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'visible_in_advanced_search' => true,
'used_in_product_listing' => true,
'used_for_sort_by' => true,
),
// your other columns...
)
)
);
}
}
you need remove your module from table core_resource first.
then edit file [you module]/etc/config.xml, add this code in global area:
.......
<resources>
<[your_module_name_in_lowercase]_setup>
<setup>
<module>[YourNamespace]_[YourModuleName]</module>
<class>
[YourNamespace]_[YourModuleName]_Model_Resource_Eav_Mysql4_Setup
</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</[your_module_name_in_lowercase]_setup>
<[your_module_name_in_lowercase]_write>
<connection>
<use>core_write</use>
</connection>
</[your_module_name_in_lowercase]_write>
<[your_module_name_in_lowercase]_read>
<connection>
<use>core_read</use>
</connection>
</[your_module_name_in_lowercase]_read>
</resources>
...........
Sometimes there is a lot to be said for using a local over-ride rather than trying to write yet another module.
I solved my problem by making a local copy of the file that does the product grid: app/code/local/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php
I added this line immediately after after the product collection is prepared:
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner'
);
Then in 'prepareColumns' function I added:
$this->addColumn('visibility',
array(
'header'=> Mage::helper('catalog')->__('Visibility'),
'width' => '70px',
'index' => 'visibility',
'type' => 'options',
'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
));
This now means that I have my catalogue/search, not visible individually options in the grid.
The template for how to add columns is the normal product grid: app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

Resources