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()
Related
I want to add a custom field to the actions tab in the Catalog Price Rules section.
This is what I did:
I added these lines to the file app\code\core\Mage\Adminhtml\Block\Promo\Catalog\Edit\Tab\Actions.php
$fieldset->addField('custom_field', 'select', array(
'label' => 'Custom Field',
'title' => 'Custom Field',
'name' => 'custom_field',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));
I changed the version to 1.6.0.4 in this file: app\code\core\Mage\CatalogRule\etc\config.xml
<Mage_CatalogRule>
<version>1.6.0.4</version>
</Mage_CatalogRule>
I created new file with the name app\code\core\Mage\CatalogRule\sql\catalogrule_setup\upgrade-1.6.0.3-1.6.0.4.php
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'UNSIGNED' => true,
'NULLABLE' => false,
'DEFAULT' => 0,
'COMMENT' => 'Custom Field',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_field', $columnOptions);
$installer->endSetup();
Then, I logged in to the admin panel and tried to edit one of the catalog price rules.
I saw my new field in the actions tab - great!
But when I click "Save", the changes that I made in that field, was not saved.
I logged in to the phpmyadmin and went to catalogrule table.
In this table I can see the new field custom_field, with the value 0 (default) - so It really didn't save the changed.
My question is what I did wrong?
Why the changed in the custom_field didn't saved in the DB?
Thanks
I'm not sure why it isn't working for you my guess is your installer isn't being called. I managed the modification through a module and local edits to future proof the change.
Directory structure:
/app/code/local/custom/promo
/app/code/local/custom/promo/etc/
/app/code/local/custom/promo/sql
/app/code/local/custom/promo/sql/promo_setup
/app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab
add file /app/code/local/custom/promo/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Custom_Promo>
<version>1.0</version>
</Custom_Promo>
</modules>
<global>
<resources>
<promo_setup>
<setup>
<module>Custom_Promo</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</promo_setup>
</resources>
</global>
</config>
add file /app/code/local/custom/promo/sql/promo_setup/install-1-0.php
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
'NULLABLE' => false,
'COMMENT' => 'Custom Promo',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_promo', $columnOptions);
$installer->endSetup();
Clear caches and verify the column is added by looking at the catalog_rule table
SELECT * FROM catalogrule;
Finally take a copy of /app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php and create the same file /app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php
Edit the file and add the field before $form->setValues($model->getData());
//Custom Promo
$fieldset->addField('custom_promo', 'select', array(
'label' => 'Custom Promo',
'title' => 'Custom Promo',
'name' => 'custom_promo',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));
I've been having trouble with this for a few days, essentially I need to add a custom organisation_id attribute (that I have created via an installer script) to be added to both the customer object and order object at the end of the onepage checkout.
The installer script works fine (I can see the values within the eav_attribute and core_resources table in the database.
During the checkout phase on the final 'Order Review' section, once the Place Order button is clicked this executes the following observer:
sales_order_place_after
Once this has been executed it runs the following function within an Observer:
public function afterOrderPlaced($observer)
{
// this id below comes from a select dropdown within the checkout onepage & already saved to session
$organisation_id = Mage::getSingleton('customer/session')->getCustomerOrganisationId();
$this->_order = $observer->getEvent()->getOrder();
$this->_order->setOrganisationId($organisation_id)->save(); // e.g 25621
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
$this->_customer->setOrganisationId($organisation_id)->save(); // e.g 25621
}
In this instance, I simply want to set the organisationId value on both the customer and order objects to '25621'.
Once I have completed the checkout process and hit the 'order confirmation' page I'd like to be able to confirm this organisation_id has indeed been correctly added to the customer and order object, can anyone confirm the easiest/best way to do this? I presume this will need to include some modification to the admin html for Sales to include this information as well.
Please note - My Magento skillset is rather limited
My Installer Script below:
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer->addAttribute('customer', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('quote', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('order', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();
You can check your custom value in your database sales_flat_order_address table and sales_flat_quote_address table .
Let me know if you have any query
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.
I am using module setup script to add new attributes group, attribute set and attributes. I am able to create attribute set, attribute group and add products to group/set. But I am having hard time setting is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters.
$installer->addAttribute('catalog_product', 'offer_type', array(
'backend' => '',
'frontend' => '',
'class' => '',
'default' => '',
'label' => 'Offer type',
'input' => 'text',
'type' => 'int',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 1,
'searchable' => 0,
'filterable' => 1,
'unique' => 0,
'comparable' => 0,
'visible_on_front' => 1,
'is_html_allowed_on_front' => 1,
'user_defined' => 1,
));
$installer->addAttributeToSet('catalog_product', $sSetId, $groupName, 'offer_type');
I see offer_type getting added to Magento and to attribute set($sSetID) and to group ($groupname). Though when I look at attribute from magento admin UI (Catalog->attributes->Manage Attributes), I see is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters set to No. I have tried various combinations but no luck. I'm using Magento CE 1.7.0.2. I am not sure what is missing in my setup script. I have reffered http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ for this. Am I missing anything?
Thanks in advance.
Do you have properly configured your installer in your config.xml ? The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead.
Why ? look at their method _prepareValues() and you'll understand what are the authorised attributes (products have more options than the standard eav_objects, you can see that when comparing the tables eav_attribute and catalog_eav_attribute)
To point to the good installer class, take a look at the standard Mage_Catalog config.xml and adapt it for your module :
<resources>
<catalog_setup>
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
</setup>
</catalog_setup>
</resources>
ps: note that the _prepareValues() method is called only when adding an attribute... if you want to update an attribute you'll need to use the full option name ("is_visible" and not just "visible")...
Another hack would be to add these attributes afterward, but it's not very beautiful:
// adding atribute :
// [...]
//getting the new attribute with full informations
$eavConfig = Mage::getSingleton('eav/config');
$installer->cleanCache();
$attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
$attribute->addData(array(
'is_visible' => 1
));
$attribute->save()
Use 'visible_on_front' => 1, in addAttribute call.
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?