Magento - Add zip code / postcode to the order grid in Magento 1.6.2 - magento

I am trying to get the order grid to display the postcode/ zip code of the of the customer.
I am trying to join the sales_flat_order_address alias with the collection but to no success.
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('postcode'));
//var_dump($collection);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Can any one please help me figure out a solution for this.

Before return parent::_prepareCollection(); You should create a join:
$collection->getSelect()->joinLeft(array('billing'=>'sales_flat_order_address'),
'main_table.entity_id = billing.parent_id AND billing.address_type="billing"',array('billing.postcode AS bp'));
If you want the shipping postcode instead, use:
$collection->getSelect()->joinLeft(array('shipping'=>'sales_flat_order_address'),
'main_table.entity_id = shipping.parent_id AND shipping.address_type="shipping"',array('shipping.postcode AS sp'));
And in the method _prepareColumns paste:
$this->addColumn('bp', array(
'header' => Mage::helper('sales')->__('Billing Postcode'),
'index' => 'bp',
'width' => '60px',
'filter_index' => 'billing.postcode'
));
That worked for me in a recent project.

Related

Magento invoice grid filter by joined attribute value

I'm using the following code to include a custom attribute, titled "sales rep" within the Magento invoice grid:
protected function _prepareCollection() {
$sales_rep = Mage::getResourceSingleton('customer/customer')->getAttribute('sales_rep');
$collection = Mage::getResourceModel('sales/order_invoice_grid_collection');
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id',array('order_id as order_id'));
$collection->join('order', 'invoice.order_id = order.entity_id',array('customer_id as customer_id'));
$collection->getSelect()->joinLeft(
array('customer_sales_rep_table' => Mage::getSingleton('core/resource')->getTableName($sales_rep->getBackend()->getTable())),
'customer_sales_rep_table.entity_id = order.customer_id
AND customer_sales_rep_table.attribute_id = '.(int) $sales_rep->getAttributeId() . '
',
array('sales_rep'=>'value')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
...
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter' => false
));
This is working perfectly, just as long as the "addColumn" property "filter" is set to "false".
How would I go about allowing users to filter by this joined attribute?
You should add your column like this:
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter_index' => 'customer_sales_rep_table.value'
));

How to display customer email in orders.csv

can someone help me? how can I add a new field in orders.csv file in Mangento? for example i want to have in csv file email and phone.
please click the link below to understand which export is talking about
http://i.stack.imgur.com/fBAHG.png
Thank you
You have to customize the Grid block : /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
So You can do like this. If you already enabled the local module functionality, Please copy and paste it to this path /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
And Open that new file ( pasted ), check this method _prepareColumns().
In this method, you have to add those attributes which you are gonna to export in order.csv.
Check the following:
....
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Email'),
'index' => 'customer_email',
));
....
After this, refresh the magento cache.
Please check the following code:
In Grid.php
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id', array('customer_email' => 'email')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Then in protected function _prepareColumns() function add following code:
$this->addColumn('customer_email', array(
'header' => Mage::helper('sales')->__('Email'),
'index' => 'customer_email',
));
After this, you must refresh the cache or login again.

magento - add customer name to order grid in magento 1.7.0.2

I'm trying to add a new column for Customer Name in the Sales Order Grid located here:
App/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
I want add Customer Name like Name in Manage Customers.
I have added the following code:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
/*junpeng add start*/
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id', array('email' => 'email'));
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
/*junpeng add end*/
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'name',
));
$this->addColumn('email', array(
'header' => Mage::helper('Sales')->__('Customer Email'),
'index' => 'email',
'type' => 'text',
));
}
Customer Email is OK,but add Customer Name does not work it!
Can someone please help me solve this problem?
You can't get customer name in just one line code join. Firstname and Lastname are different attributes and you will need to join them with your original collection and then concatenate them to display as Fullname.
So basically, Replace
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
with this code
$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$collection->getSelect()
->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
->where('ce1.attribute_id='.$fn->getAttributeId())
->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
->where('ce2.attribute_id='.$ln->getAttributeId())
->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS customer_name"));
And replace your addColumn('name', code in _prepareColumns method where you are getting the customer name, with this:
$this->addColumn('customer_name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'customer_name',
'filter_name' => 'customer_name'
));
There is a free magento extension for this:
http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html
It's currently alpha, but I have tested it and it works perfectly on 1.6 and 1.7!
I inspire my answer from Kalpesh
so you should replace this code
$collection->getSelect()->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
);
with these lines
$customerTable = Mage::getResourceSingleton('customer/customer')->getEntityTable();
$firstnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('firstname');
$firstnameAttributeTable = $firstnameAttribute->getBackend()->getTable();
$lastnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('lastname');
$lastnameAttributeTable = $lastnameAttribute->getBackend()->getTable();
$collection->getSelect()
->join( array('customer_varchar1'=>$firstnameAttributeTable),'main_table.customer_id =customer_varchar1.entity_id and customer_varchar1.attribute_id='.$firstnameAttribute->getId(),
array('customer_varchar1.value'))
->join( array('customer_varchar2'=>$lastnameAttributeTable),'main_table.customer_id =customer_varchar2.entity_id and customer_varchar2.attribute_id='.$lastnameAttribute->getId(),
array('customer name'=>'CONCAT(customer_varchar2.value ," " ,customer_varchar1.value)'));

How to get invoice increment_id on order grid? (Magento 1.7)

I'm trying to get the increment_id from the sales_flat_invoice table to appear on my orders grid.
I've managed to do that, but then it will only show orders which has been invoiced.
The sum it up, what I'm trying to do, is to create a column which contain the increment_id of the invoice (if the order has been invoiced - if not, it should be blank).
The code is used was the following:
In _prepareCollection() :
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()
->join(
array('address' => $collection->getTable("sales/order_address")),
'main_table.entity_id = address.parent_id AND address.address_type = "shipping"',
array('postcode')
);
//$collection->join('invoice', 'main_table.entity_id = invoice.order_id', 'increment_id as invoice_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
In _prepareColumns() :
$this->addColumn('invoice_id', array(
'header' => 'Faktureret',
'index' => 'invoice_id',
'width' => '70px',
'type' => 'text',
));
Thanks and have a beautiful day!
If you want to add the invoice id in the sales order grid then you can use the following code in your prepareCollection() function as
$collection->getSelect()->joinLeft('sales_flat_invoice', 'main_table.entity_id = sales_flat_invoice.order_id', 'increment_id as invoice_id');
By using the following code you will able to get the invoice id from the current order id in sales order grid.
after this add column field as
$this->addColumn('invoice_id',
array(
'header'=> $this->__('Invoice Id'),
'align' =>'right',
'type=' => 'text',
'index' => 'invoice_id',
)
);
For more follow me on
http://www.webtechnologycodes.com
You need to do a LEFT JOIN. Use ->joinLeft. ->join references to ->joinInner
To get invoice details from order detail. you can use
$_orders = $this->getOrders();
$_invoices = $_order->getInvoiceCollection();

How to add website field in magento admin grid?

Im creating a custom module for banner slider. In admin grid I want to display website field. For that in namespace/module/Block/Adminhtml/banner/Grid.php file I added
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('bannerslider')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}
in _prepareColumns() function. Now I can able to see website column. But Im not able to see the website names in each rows. How can I show the website names in each rows. Please see the image.
What im missing?
Here is my collection.
protected function _prepareCollection()
{
$collection = Mage::getModel('bannerslider/bannerslider')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
I have a field called "website_id" in my table. Each row may have multiple values which are comma separated. In this situation can you tell me how to use collection?
In the _prepareCollection() function, you insert :
parent::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
If the website_id is in other tables..., you'll need to join the table and add the website_id in select, in the _prepareCollection() function.
protected function _prepareCollection() {
//Your custom code in here
}

Resources