Magento 1.7 - add attribute to order_item and setting value - magento

I want to add a new attribute to order_item
install-0.1.0.php
$installer->addAttribute('order_item', 'xxx', array('type'=>'text', 'visible' => true, 'required' => false, 'is_user_defined' => true, 'note' => 'Field comment'));
$installer->addAttribute('quote_item', 'xxx', array('type'=>'text', 'visible' => true, 'required' => false, 'is_user_defined' => true, 'note' => 'Field comment'));
I see 2 new rows in eav_attribute
Running this two times prints nothing:
$item = Mage::getModel('sales/order_item');
$item->load(91);
$item->setXxx("test");
$item->setData("xxx", "test");
print $item->getXxx();
$item->save();
How do I set and get a value in an order item for this attribute?

Unfortunately sales/order and sales/order_item does not inherit or use the eav_attribute structure in Magento. This blog post explains it pretty well: http://www.krilion.net/blog/2012/08/adding-a-custom-attribute-to-a-magento-order/
The jist of it is that you'll need to create your installer script to create a new column in the flat table (sales_flat_order_item) in order to save your new value. You should be able to save your xxx value as soon as there's an xxx column in the flat table. This means your installer script needs to extend Mage_Sales_Model_Mysql4_Setup and not Mage_Eav_Model_Entity_Setup.

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.

Save custom attribute to order object at Checkout in Magento - How can I confirm this?

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

How to create custom source model for custom attribute type select?

I tried to search this but couldn't find any. When creating a custom product attribute with select type programmatically, Magento always assigns eav/entity_attribute_source_table as the source model.
There are 2 issues with this default source model:
I can't auto populate the field with data taken programmatically from somewhere else other than have to type the data list manually one by one.
Although I have specified the "default" or "default_value" (I can see in the database that the value is there), the field is still showing empty as the first line.
How I can change the default source_model to my own source model for select type?
Thank you
The key you are looking for is to pass a source value in your SQL setup. Make sure your $installer is an EAV setup object.
You would do the following in your setup script:
$installer = $this;
$installer->starSetup();
// Setup customer multiselect attribute
$attr = array(
'backend' => 'eav/entity_attribute_backend_array',
'input' => 'multiselect',
'label' => 'Permissions',
'note' => 'Used for group-based frontend permissions.',
'required' => false,
'sort_order' => '1000',
'source' => 'eav/entity_attribute_source_table', // Change it here
'user_defined' => true
);
$installer->addAttribute('customer', 'permissions', $attr);
// Add options for permissions
$options = array(
'attribute_id' => $installer->getAttributeId('customer', 'permissions'),
'value' => array(
'place_order' => array('Can Place Orders'),
'view_catalog' => array('Can View the Catalog'),
)
);
$installer->addAttributeOption($options);
$installer->endSetup();
Utimately, I believe the source model can be anything that provides a toOptionArray() function.
There is a great example of this in Mage_Customer, installer: mysql4-upgrade-1.5.9.9-1.6.0.0.php
In it, a country source model is being assigned to customer address attribute country_id.
$installer->updateAttribute(
'customer_address',
'country_id',
'source_model',
'customer/entity_address_attribute_source_country'
);
Change this to catalog_product, your attribute, and source model.
You set the source type as shown below.
'source' => 'categoryattr/attribute_source_type',
and create the file Attribute\Source\Type.php and the create the options and set the value 0 for default option.
$this->_options[] = array (
'label' => 'Select Category',
'value' => '0'
);
please refer below for file structure and step by step explanation.
http://www.pearlbells.co.uk/how-to-create-custom-attribute-source-type-in-magento/

Magento Admin formfield multiselect selected

I´m currently developing a custom module for magento thats going to list employees.
I have figured out almost everything. The only thing I got left is how the selected values is going to be highlighted.
The problem I´m having is for the backend.
I got 2 tabs per employee, one for employee data and one tab for magento categories.
1 employee can have 1 or more categories.
The database table that the categories are stored in are a non-eav table.
So my question is
What in a multiselect determines which values are selected? As it is now, only one value is selected.
I think you can do this by simply passing in an array of the id's to be selected into the 'value' attribute of the field being added for the multiselect in the _prepareForm() method. Something like the following.
$fieldset->addField('category_id', 'multiselect', array(
'name' => 'categories[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('mymodule/mymodel')->getMymodelValuesForForm(),
'value' => array(1,7,10),
));
The id of the form element (e.g. category_id) must not be an attribute in your model, otherwise when the form values get set with $form->setValues() later on, the attribute value will be overwritten.
I normally store multiple selections as a text column separated by commas much like most magento modules handles stores which requires a slightly different approach as shown below.
In the form block for the tab with the multiselect, you firstly define the element to be displayed like so in the _prepareForm() method. You then get the values from the model and set put them into the form data.
protected function _prepareForm()
{
...
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
...
if ( Mage::getSingleton('adminhtml/session')->getMymodelData() )
{
$data = Mage::getSingleton('adminhtml/session')->getMymodelData();
} elseif ( Mage::registry('mymodel_data') ) {
$data = Mage::registry('mymodel_data')->getData();
}
$data['store_id'] = isset($data['stores']) ? explode(',', $data['stores']) : array();
$form->setValues($data);
}
I normally store the selected stores (categories as in your case) in the main model as a text column and comma separated values of ids, hence the explode.
In the controller for for the edit action, I put the model being edited into the mage registry so we can load it and it's values in the step above.
Mage::register('mymodel_data', $model);
Thanks for answering.
This is how my field looks like:
$fieldset->addField('npn_CatID', 'multiselect', array(
'label' => Mage::helper('employeelist')->__('Kategori'),
'class' => 'required-entry',
'required' => true,
'name' => 'npn_CatID',
'values' => $data,
'value' => array(3,5)
));
npn_CatID is the value in my db where the category id is saved.
I have tried to change the name and field ID but cant get it working.
When its the field id is like above ONE value is selected and its the last one inserted for the chosen employee
My data array looks likes
array(array('value' => '1', 'label' => 'USB'), array('value' => '2', 'label' => 'Memories'))

How to add admin user attributes in Magento

I'd like to add some new attributes to the admin users in Magento. These users are different than customers (just to make it clear) and it's only possible to set their user/name/lastname/mail/pass, but I'd like to add a few new attributes.
To do so, I guess I can use the addattribute function, but I need to find out which is the id of these admin users. For example, if I want to add a new attribute to a customer, I can use a function like this:
$setup->addAttribute('customer','attribute_id', $attr );
So, in this case, 'customer' is the id for customers. How can I find out which id is used for admin users? (this question can be extended to "How can I find the different id for the different types of attributes in Magento?").
==There is a chance that there is no way to change this. I've taken a look at the admin_user table and it's quite simple, all fields are there. So maybe there are no attributes in this case.==
Thanks
You can find all such ids (entity ids) in the eav_entity_type table.
And yes, there is no record for admin user. Because all data about admin users are stored in flat tables but not in eav. So to add a new attribute to admin user, you need to add a new column in the admin_user table
You will need to add a column to the admin_user table.
$installer->getConnection()->addColumn($installer->getTable('admin/user'), 'location', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 256,
'nullable' => true,
'default' => null
));
Then, if you want to add/edit this field from the backend you need to rewrite the method Mage_Adminhtml_Block_Permissions_User_Edit_Tab_Main::_prepareForm and add a new element in there:
$fieldset->addField('location', 'select', array(
'name' => 'is_active',
'label' => Mage::helper('adminhtml')->__('location'),
'id' => 'is_active',
'title' => Mage::helper('adminhtml')->__('location'),
'class' => 'input-select',
'style' => 'width: 80px',
'options' => array('1' => Mage::helper('adminhtml')->__('Yes'), '0' => Mage::helper('adminhtml')->__('No')),
));
Clear the cache and it should work.
No option till 1.7
thats what i use in the template to show the sku to an specific user bit dirty but works fine:
<?php
//EGS SKU added for Power User
$_powerUser = 777;
if (Mage::getSingleton('customer/session')->getCustomer()->getId() == $_powerUser)
{
echo '<div class="price-from">' . $_product->getSku() . '</div>';
}
?>

Resources