How to display Custom order attribute in Magento back end - magento

I'm using Magento 1.7 - I am fairly new to Magento module development.
I have created a new Order custom EAV attribute called Deliverydate which the customer will enter on the Shopping Cart page. I created a custom module and installer script, and can see it in the eav_attribute table.
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('order', 'deliverydate', array(
'position' => 1,
'input' => 'text',
'backend_type' => 'varchar',
'type' => 'varchar',
'label' => 'Choose delivery date',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => 1,
'visible_on_front' => 1,
));
$installer->endSetup();
But I've read many sources and can't figure out how to
1) Save the value entered by the customer.
2) Retrieve the value for display in the Admin.
I have read on these tutorials about Observers but can't seem to get one to work:
Here's my config.xml file
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Deliverydate>
<version>0.1.0</version>
</Mycompany_Deliverydate>
</modules>
<global>
<resources>
<deliverydate_setup>
<setup>
<module>Mycompany_Deliverydate</module>
<class>Mycompany_Deliverydate_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</deliverydate_setup>
</resources>
<events>
<checkout_type_onepage_save_order>
<observers>
<Deliverydate_observer>
<type>singleton</type>
<class>deliverydate/observer</class>
<method>Deliverydate</method>
</Deliverydate_observer>
</observers>
</checkout_type_onepage_save_order>
</events>
</global>
</config>
And my Observer.php file, most of which I borrowed from another tutorial / question on this site.
class Mycompany_Deliverydate_Model_Observer {
public function Deliverydate($observer) {
$event = $observer->getEvent();
$order = $event->getOrder();
$order->setDeliverydate(Mage::app()->getRequest()->getPost('delivery_date'));
}
}
I don't think this is being entered at all - I put in a throw new Exception and went through an entire order without ever seeing the exception. In any case I am not sure I'm checking the right event - checkout_type_onepage_save_order - when is this fired? And I don't know where the setDeliverydate() function would be defined but as I said this sort of comes from another source and it wasn't defined there either.
And where does the value entered get stored?

Please check this following post, It would help you. If it doesn't uplift you from this issue, please comment here
http://chillydraji.wordpress.com/2014/03/03/how-to-create-new-attribute-to-order-in-magento/

Related

Magento Module DB Setup Script not running

Running Magento 1.9.0.2, I'm trying to add 2 new checkbox fields into the Address form via Module.
Problem: The database script is NOT executed (note the die() function on the first line)
Symptoms:
My module does not exist in core_resources table
database does not get updated
module is listed & enabled in System > Configuration > Advanced
log enabled, folder {base_dir}/var/log/ is 777, no log produced
I'm installing the module; is uploading the files to the folder & refresh the cache & frontend once triggers the install?
Question: What did I miss?
Here is the {base_dir}/app/code/local/Tdg/Check/etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<version>1.0.1</version>
</Tdg_Check>
</modules>
<admin>
<fieldsets>
<customer_dataflow>
<chk_commercial><billing>1</billing><shipping>1</shipping></chk_commercial>
<chk_residential><billing>1</billing><shipping>1</shipping></chk_residential>
</customer_dataflow>
</fieldsets>
</admin>
<global>
<models>
<check>
<class>Tdg_Check_Model</class>
</check>
</models>
<resources>
<check_setup>
<setup>
<module>Tdg_Check</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</check_setup>
<check_write>
<connection>
<use>core_write</use>
</connection>
</check_write>
<check_read>
<connection>
<use>core_read</use>
</connection>
</check_read>
</resources>
<fieldsets>
<sales_copy_order_billing_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<chk_commercial><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_commercial>
<chk_residential><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_residential>
</sales_convert_quote_address>
<sales_convert_order_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</sales_convert_order_address>
<customer_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</customer_address>
<checkout_onepage_billing>
<chk_commercial><to_customer>*</to_customer></chk_commercial>
<chk_residential><to_customer>*</to_customer></chk_residential>
</checkout_onepage_billing>
</fieldsets>
</global>
</config>
And the SQL Update file {base_dir}/app/code/Tdg/Check/sql/check_setup/mysql4-install-1.0.1.php:
<?php
die('Installing Module');
/* #var $installer Mage_Customer_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
/* #var $addressHelper Mage_Customer_Helper_Address */
$addressHelper = Mage::helper('customer/address');
$store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
/* #var $eavConfig Mage_Eav_Model_Config */
$eavConfig = Mage::getSingleton('eav/config');
// update customer address user defined attributes data
$attributes = array(
'chk_commercial' => array(
'label' => 'Chk Commercial',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 140,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
'chk_residential' => array(
'label' => 'Chk Residential',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 141,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
);
foreach ($attributes as $attributeCode => $data) {
$attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
$attribute->setWebsite($store->getWebsite());
$attribute->addData($data);
$usedInForms = array(
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address'
);
$attribute->setData('used_in_forms', $usedInForms);
$attribute->save();
}
$installer->run("
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
");
$installer->endSetup();
?>
Last, it's the Module definition file Tdg_Check.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<active>true</active>
<codePool>local</codePool>
</Tdg_Check>
</modules>
</config>
In Magento module entry stored in code field like below:
yourmodulename_setup
Check for record like check_setup in code field of core_resource table.
So for run install script again you have to find module record in module and remove that record.
It will automatically run the script again.

Magento 1.7 : Admin - Custom Customer Grid attribute - not filterable

I'm not sure why, but I can't filter my attribute (it works fine with defaults ones).
Here is my code :
Config.xml
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>SaponeWebConcept_PrivateSells_Adminhtml_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
Grid.php
public function setCollection($collection)
{
$collection->addAttributeToSelect('wants_to_be_vip');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$attributeCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('wants_to_be_vip')
->getFirstItem();
$itemsOptions = $attributeCollection->getSource()->getAllOptions(false);
$optionsArr = array();
foreach ($itemsOptions as $option) {
$optionsArr[$option['value']] = $option['label'];
}
$this->addColumn('V.I.P', array(
'header'=> Mage::helper('customer')->__('Veut devenir V.I.P'),
'index' => 'wants_to_be_vip',
'type' => 'options',
'options' => $optionsArr,
));
$this->addColumnsOrder('customer_id','wants_to_be_vip');
}
Thanks for you help !
Edit : It may need some more infos :
- Values display ok.
- My select box is ok too.
- When I press the filter button, it loads, and nothing changes.

Magento V1.7 Grid View - Add manufacturer attribute to view

On this page I want to add the Manufacturer name directly below the name of the item, but can't seem to get this to work. Have tried a number of suggestions, but none seem to work.
Please be specific on the file and lines to edit.
Try this, assuming that you are using attribute set to create a 'manufacturer' field in Admin -> Catalog -> Manage Attributes.
Write a custom module that extend Catalog Product Block Grid
/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Create Block File: app/code/local/MageIgniter/ManufacturerGrid/Block/AdminhtmlCatalog/Product/Grid.php
class MageIgniter_ManufacturerGrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
Copy method _prepareCollection() to you custom block and update (line 58)
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('manufacturer') // added this line
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id');
Copy method _prepareColumns() to you custom block and add
$this->addColumn('manufacturer',
array(
'header'=> Mage::helper('catalog')->__('Manufacturer'),
'width' => '60px',
'index' => 'manufacturer',
'type' => 'options';
'options' => Mage::helper('manufacturergrid')->getManufacturerOption(),
));
Create Helper File: app/code/local/MageIgniter/ManufacturerGrid/Helper/Data.php
class MageIgniter_ManufacturerGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getManufacturerOption(){
$_opt = array();
foreach (Mage::getModel('eav/config')->getAttribute('catalog_product','manufacturer')->getSource()->getAllOptions(false,true) as $option){
$_opt[$option['value']] = $option['label'];
}
return $_opt;
}
}
Create: app/code/local/MageIgniter/ManufacturerGrid/etc/config.xml
<config>
<modules>
<MageIgniter_ManufacturerGrid>
<version>1.0.0</version>
</MageIgniter_ManufacturerGrid>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>MageIgniter_ManufacturerGrid_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<localship>
<class>MageIgniter_ManufacturerGrid_Helper</class>
</localship>
</helpers>
</global>
</config>
Create: app/etc/modules/MageIgniter_ManufacturerGrid.xml
<?xml version="1.0"?>
<config>
<modules>
<MageIgniter_ManufacturerGrid>
<active>true</active>
<codePool>local</codePool>
</MageIgniter_ManufacturerGrid>
</modules>
</config>
$manufacturer_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()
->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');
foreach ($manufacturer_items as $manufacturer_item) :
if ($manufacturer_item->getAttributeCode() == 'manufacturer')
$manufacturer_options[$manufacturer_item->getOptionId()] = $manufacturer_item->getValue();
endforeach;
$this->addColumn('manufacturer',
array(
'header'=> Mage::helper('catalog')->__('Manufacturer'),
'width' => '100px',
'type' => 'options',
'index' => 'manufacturer',
'options' => $manufacturer_options,
));
Put this code in Gird.php file and this code in apoximate line number 58 to 63 in same file
->addAttributeToSelect('manufacturer')

Magento Save Selected Country

I want to add another dropdown list of countries in customer account register page.I tried
<?php echo $this->getCountryHtmlSelect() ?>
But this doesn't display the dropdown.Another one I tried is
<select name="partner_country" id="partner_country">
<option value=''>– Please Select –</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country->getId() ?>"><?php echo $_country->getName() ?></option>
<?php endforeach; ?>
</select>
This one displays country list, but the selected country doesn't show up in backend customer information page.
I know getCountryHtmlSelect() renders dropdown of countries.Do I have create similar method in my module to save the selected country?
Update
I already created a source model while adding this attribute via setup script,
$installer->addAttribute('customer_address','partner_country_id',array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'wholesale/attribute_source_partnercountry',
'global' => 1,
'visible' => 1,
'required' => 0,
'visible_on_front' => 1,
'sort_order'=>220
));
Source model
class Company_Wholesale_Model_Attribute_Source_Partnercountry extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = Mage::getResourceModel('directory/country_collection')
->loadByStore($this->getAttribute()->getStoreId())->toOptionArray();
}
return $this->_options;
}
}
config.xml
<config>
<global>
<resources>
<wholesale_setup>
<setup>
<module>Company_Wholesale</module>
<class>Company_Wholesale_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</wholesale_setup>
</resources>
</global>
</config>
The issue you have is related to type of attribute you've created. For making possible selection of your custom attribute in the admin, you need to create/update it with type 'select' and 'source_model' for it. For this purpose using of customer module setup model is required, so in your module configuration of setup resources you need to specify it:
<config>
<global>
<resources>
<your_module_setup>
<setup>
<class>Mage_Customer_Model_Resource_Setup</class>
<module>Your_Module</module>
</setup>
</your_module_setup>
</resources>
</global>
</config>
And in your setup file, you need to create/modify your attribute. (If attribute exists, current code snippet will modify it, instead of creation).
<?php
$this->startSetup();
$this->addAttribute('customer', 'partner_country' , array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'customer/entity_address_attribute_source_country'
));
$this->endSetup();
UPDATED:
Now I got your question, you haven't added your attribute to customer create account form, so your attribute is filtered out from data that is set to customer model during account creation process.
So you simply need to specify your customer attribute for the form where it should be possible to save your attribute.
Currently there are such forms available:
customer_account_create - Register Form
customer_account_edit - Change Account Details Form
checkout_register - Registering new account during checkout
For adding your custom attribute to form, just create one more setup script, that add a record with form code and your attribute id to table called customer/form_attribute:
<?php
$this->startSetup();
$attributeId = $this->getAttributeId('customer', 'partner_country_id');
$data = array(
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_create'),
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_edit'),
array('attribute_id' => $attributeId, 'form_code' => 'checkout_register')
);
$this->getConnection()->insertMultiple($this->getTable('customer/form_attribute'), $data);
$this->endSetup();
Just opt-out the forms you don't need.
If you would like to usegetCountryHtmlSelect(), then you should give it some parameters so that it can apply to your attribute, and not countryby default. For the register form, it could give something like this :
echo $this->getCountryHtmlSelect($this->getFormData()->getPartnerCountryId(), 'partner_country_id', 'partner_country', $this->__('Partner Country'))
And in the second example, you use partner_country in select name, whereas you created a partner_country_id attribute.

Magento: can't set the default value of custom order attribute using installation script

I've added custom attribute to orders using mysql4-install-1.0.0.php in my module:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
It works - when I look at sales_flat_order table, I see new varchar field *custom_status* in the table. But the default value is NULL instead of "my_default_value" as expected there. Any ideas, why?
PS. Installation script is really executed, I reset all to initial state each time.
UPD. config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
Magento Sales module has pseudo emulation of old EAV functionality. It takes only type property from your array to create a column in the database. Also it uses "grid" property for determining is it required to make the same column in grid representation table.
BTW, previous sales module that was based on EAV, was not using default, label, required and other properties as well. If you want to set a default value for this attribute you need create an observer for order before save event and set this data in it if field is empty.
Make sure you have it like this in your config.xml:
<resources>
<customattr_setup>
<setup>
<module>Yourcompany_ModuleName</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customattr_setup>
</resources>
The <class>Mage_Sales_Model_Mysql4_Setup</class> is extremely important!
Then in the install script mysql4-install-0.1.0.php:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(
'order',
'customattr',
array(
'type' => 'float', // or whatever you want here...
'grid' => false
)
);
$installer->endSetup();
?>

Resources