How do I add a custom sorting option in the backend of magento? I want to be able to sort customers by company name in the backend. I simply can't figure out how to add a sorting option.
I found a clever way to get the company name which can be found here.
You have to override the customer grid to do this, to declare the override on your config.xml:
<global>
<adminhtml>
<rewrite>
<customer_grid>Namespace_Module_Block_Rewrite_Customergrid</customer_grid>
</rewrite>
</adminhtml>
</global>
Then you have to create your class on app/code/local/Namespace/Module/Block/Rewrite/Customergrid.php:
<?php
class Namespace_Module_Block_Rewrite_Customergrid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')
->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
/*$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
$this->addColumn('lastname', array(
'header' => Mage::helper('customer')->__('Last Name'),
'index' => 'lastname'
));*/
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt'=> 0))
->load()
->toOptionHash();
$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode',
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'billing_country_id',
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region',
));
$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));
$this->addColumn('company', array(
'header' => Mage::helper('customer')->__('Company'),
'width' => '150px',
'index' => 'company',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('customer')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}
$this->addColumn('action',
array(
'header' => Mage::helper('customer')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('customer')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
return parent::_prepareColumns();
}
}
We are extending Mage_Adminhtml_Block_Customer_Grid and copying the functions _prepareCollection() and _prepareColumns() with two key differences. On _prepareCollection() we add:
->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left')
To load the customer's company and then on _prepareColumns() we add the column you need:
$this->addColumn('company', array(
'header' => Mage::helper('customer')->__('Company'),
'width' => '150px',
'index' => 'company',
));
Related
I am using Magento 1.9.2 and i am rewriting the products grid table.
I have done a copy from the original Grid.php and created this:
/app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php and here is what it contains:
<?php
class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('productGrid');
$this->setDefaultSort('entity_id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
$this->setVarNameFilter('product_filter');
}
protected function _getStore()
{
$storeId = (int) $this->getRequest()->getParam('store', 0);
return Mage::app()->getStore($storeId);
}
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id');
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$collection->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left');
}
if ($store->getId()) {
//$collection->setStoreId($store->getId());
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
$adminStore
);
$collection->joinAttribute(
'custom_name',
'catalog_product/name',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'price',
'catalog_product/price',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->setCollection($collection);
parent::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
return $this;
}
protected function _addColumnFilterToCollection($column)
{
if ($this->getCollection()) {
if ($column->getId() == 'websites') {
$this->getCollection()->joinField('websites',
'catalog/product_website',
'website_id',
'product_id=entity_id',
null,
'left');
}
}
return parent::_addColumnFilterToCollection($column);
}
protected function _prepareColumns()
{
$this->addColumn('entity_id',
array(
'header'=> Mage::helper('catalog')->__('ID'),
'width' => '50px',
'type' => 'number',
'index' => 'entity_id',
));
$this->addColumn('name',
array(
'header'=> Mage::helper('catalog')->__('Name'),
'index' => 'name',
));
$store = $this->_getStore();
if ($store->getId()) {
$this->addColumn('custom_name',
array(
'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
'index' => 'custom_name',
));
}
$this->addColumn('type',
array(
'header'=> Mage::helper('catalog')->__('Type'),
'width' => '60px',
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
));
$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
->load()
->toOptionHash();
$this->addColumn('set_name',
array(
'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
'width' => '100px',
'index' => 'attribute_set_id',
'type' => 'options',
'options' => $sets,
));
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
$this->addColumn('number',
array(
'header'=> Mage::helper('catalog')->__('Поръчка №'),
'width' => '50px',
'index' => 'entity_id',
'renderer' => 'Mage_Adminhtml_Block_Catalog_Product_Renderer',
));
$store = $this->_getStore();
$this->addColumn('price',
array(
'header'=> Mage::helper('catalog')->__('Price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'price',
));
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$this->addColumn('qty',
array(
'header'=> Mage::helper('catalog')->__('Qty'),
'width' => '100px',
'type' => 'number',
'index' => 'qty',
));
}
$this->addColumn('visibility',
array(
'header'=> Mage::helper('catalog')->__('Visibility'),
'width' => '70px',
'index' => 'visibility',
'type' => 'options',
'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
));
$this->addColumn('status',
array(
'header'=> Mage::helper('catalog')->__('Status'),
'width' => '70px',
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('websites',
array(
'header'=> Mage::helper('catalog')->__('Websites'),
'width' => '100px',
'sortable' => false,
'index' => 'websites',
'type' => 'options',
'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(),
));
}
$this->addColumn('action',
array(
'header' => Mage::helper('catalog')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('catalog')->__('Edit'),
'url' => array(
'base'=>'*/*/edit',
'params'=>array('store'=>$this->getRequest()->getParam('store'))
),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
));
if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) {
$this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
}
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('product');
$this->getMassactionBlock()->addItem('delete', array(
'label'=> Mage::helper('catalog')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('catalog')->__('Are you sure?')
));
$statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('catalog')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('catalog')->__('Status'),
'values' => $statuses
)
)
));
if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
$this->getMassactionBlock()->addItem('attributes', array(
'label' => Mage::helper('catalog')->__('Update Attributes'),
'url' => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
));
}
Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
return $this;
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array(
'store'=>$this->getRequest()->getParam('store'),
'id'=>$row->getId())
);
}
}
The custom code i've added in the Grid.php is this:
$this->addColumn('number',
array(
'header'=> Mage::helper('catalog')->__('Поръчка №'),
'width' => '50px',
'index' => 'entity_id',
'renderer' => 'Mage_Adminhtml_Block_Catalog_Product_Renderer',
));
I have created and rendered also, i saw this in another answer:
<?PHP
class Mage_Adminhtml_Block_Catalog_Product_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$productId = $row->getData($this->getColumn()->getIndex());
$orders = array();
$collection = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToFilter('product_id', array('eq' => $productId))
->load();
foreach($collection as $orderItem) {
$orders[$orderItem->getOrder()->getIncrementId()] = $orderItem->getOrder();
}
$first_key = key($orders);
return $first_key;
}
}
I have added the additional column so i can display in every row in which order ID the product is purchased. I have no problems. Everything is correct, but the problem comes when i try to search with this custom column.
The order ID is displayed correctly in the column but can not be searched by order ID.
Where is my mistake, why it is not working and how can i fix it?
Thanks in advance!
You are unable to search your custom_column in grid because you have just used renderer which returns order_id in run time and prepare your columns. That's it. What magento grid search do, it filters the loaded collection with "search text".
For example:- you make a search for order_id = 10000901, your search result returns null because order_id is not present in collection.
So you should join sales_flat_order & sales_flat_order_item with your product table in order to get increment_id in collection. Then sorting & searching both will work perfectly.
Please add this code
$collection->getSelect()->joinLeft(
array('order_item'=>'sales_flat_order_item'),
'e.entity_id = order_item.product_id',
array('order_item.product_id','order_item.order_id')
);
$collection->getSelect()->joinLeft(
array('order'=>'sales_flat_order'),
'order_item.order_id = `order`.entity_id',
array('order.increment_id')
);
$collection->getSelect()->group('e.entity_id');
before $this->setCollection($collection); in method _prepareCollection() of your /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
and
update column code as below
$this->addColumn('increment_id',
array(
'header'=> Mage::helper('catalog')->__('Order Id'),
'width' => '100px',
'type' => 'number',
'index' => 'increment_id',
));
I have a grid, in this grid I have filter. Grid code is:
$this->widget('booster.widgets.TbGridView', array(
'id' => 'sam',
'type' => 'striped bordered condensed',
'dataProvider' => $dataProvider,
'responsiveTable' => true,
'enableHistory' => true,
'filter' => $asset,
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'filter' => false,
'type' => 'text',
),
array(
'name' => 'user',
'header' => 'Registered By',
'type' => 'text',
'value' => '$data["assignedBy"]',
),
array(
'name' => 'createdAt',
'header' => 'Created At',
'type' => 'datetime',
),
array(
'name' => 'serial',
'header' => 'Serial',
),
array(
'name' => 'brand',
'header' => 'Brand',
),
array(
'name' => 'model',
'header' => 'Model',
),
array(
'name' => 'assetType',
'type' => 'text',
'filter' => \wsi\it\model\AssetType::getRepository()->getTypeTree(),
'value' => '$data["assetTypeName"]',
'header' => 'Type',
),
array(
'name' => 'assigned',
'value' => '(isset($data["assignedTo"]))? $data["assignedTo"]:null',
'header' => 'Assigned To',
),
array(
'name' => 'location',
'filter' => \wsi\hr\Facade::getInstance()->getLocations(),
'value' => '$data["locationName"]',
'header' => 'Location',
),
array(
'name' => 'status',
'header' => 'Status',
'filter' => \wsi\it\model\Asset::$statusOptionList,
'value' => '\wsi\it\model\Asset::$statusOptionList[$data["status"]]',
),
array(
'class' => 'booster.widgets.TbButtonColumn',
'template' => '{view} {update} {delete}',
'header' => '',
'buttons' => array(
'update' => array(
'url' => '\Yii::app()->controller->createUrl("asset/create", array("id"=>$data["id"]))',
),
'view' => array(
'url' => '\Yii::app()->controller->createUrl("asset/view", array("id"=>$data["id"]))',
),
'delete' => array(
'url' => '\Yii::app()->controller->createUrl("asset/delete", array("id"=>$data["id"]))',
),
),
),
)
)
);
When Grid is rendered in the browser you can see that the filter list box contains html which is not parsed fully!
I had this problem with Yii-Booster before and I solved it with an option which I passed to that widget ('htmlOptions' => 'encode' => false) and It prevents to be treated as string, so browsers would parse it as space. That code which You can see below does not work for grid filter!
$form->dropDownListGroup($formModel, 'segmentList', array(
'wrapperHtmlOptions' => array(
'class' => 'col-md-6'
),
'widgetOptions' => array(
'data' => $segmentTreeArray,
'htmlOptions' => array(
>>> 'encode' => false, <<<
)
),
'hint' => "Press CTRL to add another item, otherwise others will be deselected",
));
BUT I am sure that I have to pass same "encode" => false to filter list too, I just can not find under what key I should pass it (htmlOptions did not work).
I've been writing a custom module for the magento backend,i want to add filter for each column
Can anyone point me in the right direction as to where the code is that handles this function? I'm going to assume its part of the controller
Thanks for any help you can provide!
i have prepared columns like this
public function __construct()
{
parent::__construct();
$this->setId('main_grid');
$this->setDefaultSort('entity_id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
protected function _prepareCollection()
{
$model = Mage::getModel('CartAbandoned/tip');
$collection = $model->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
$this->addColumn('id', array(
'header' => Mage::helper('CartAbandoned')->__('Id'),
'align' => 'right',
'width' => '50px',
'type' => 'number',
'index' => 'entity_id',
));
$this->addColumn('E-Mail', array(
'header' => Mage::helper('CartAbandoned')->__('EMail'),
'align' => 'left',
'width' => '150px',
'index' => 'customer_email',
'type' => 'text',
'truncate' => 50,
'escape' => true,
));
$this->addColumn('firstName', array(
'header' => Mage::helper('CartAbandoned')->__('firstName'),
'align' => 'left',
'index' => 'customer_firstname',
'type' => 'text',
'escape' => true,
));
$this->addColumn('lastName', array(
'header' => Mage::helper('CartAbandoned')->__('lastName'),
'align' => 'left',
'index' => 'customer_lastname',
'type' => 'text',
'escape' => true,
));
$this->addColumn('total', array(
'header' => Mage::helper('CartAbandoned')->__('Total'),
'align' => 'left',
'index' => 'base_grand_total',
'type' => 'price',
'escape' => true,
));
$this->addColumn('quan', array(
'header' => Mage::helper('CartAbandoned')->__('Quantity'),
'align' => 'left',
'index' => 'items_qty',
'type' => 'number',
'escape' => true,
));
$this->addColumn('cartcreatedtime', array(
'header' => Mage::helper('CartAbandoned')->__('cartcreatedtime'),
'align' => 'left',
'index' => 'created_at',
'type' => 'datetime',
'escape' => true,
));
$this->addColumn('cartabandonedtime', array(
'header' => Mage::helper('CartAbandoned')->__('cartabandonedtime'),
'align' => 'left',
'index' => 'updated_at',
'type' => 'datetime',
'escape' => true,
));
$this->addColumn('action',array(
'header' => Mage::helper('CartAbandoned')->__('Action'),
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('CartAbandoned')->__('View Products'),
'url' => array('base'=>'*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false
));
First of all search project by this term "extends Mage_Adminhtml_Block_Widget_Grid", you should find for example this class Mage_Adminhtml_Block_Catalog_Category_Tab_Product.
Basically what you need to focus on are two methods:
_prepareCollection()
_prepareColumns()
_prepareCollection prepares collection which is used by your grid and on which Magento applies filters which are represented by index key in each column that you add in _prepareColumns() method.
Example
Below example comes from class that i've pasted above ;)
$this->addColumn('E-Mail', array(
'header' => Mage::helper('CartAbandoned')->__('EMail'),
'align' => 'left',
'width' => '150px',
'index' => 'customer_email',
'type' => 'text',
'truncate' => 50,
'escape' => true,
));
In your collection there should be field/column which is called customer_email and as you have index set to same name Magento should handle rest.
EDIT
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('catalog')->__('ID'),
'sortable' => true,
'width' => '60',
'index' => 'entity_id'
));
$this->addColumn('name', array(
'header' => Mage::helper('catalog')->__('Name'),
'index' => 'name'
));
$this->addColumn('sku', array(
'header' => Mage::helper('catalog')->__('SKU'),
'width' => '80',
'index' => 'sku'
));
return parent::_prepareColumns();
}
This example show how to prepare filterable collection for 3 columns: sku, name and entity_id.
There is one solution, which I found on atwix.com
$this->addColumn('address', array(
'header'=> Mage::helper('sales')->__('Address'),
'type' => 'text',
'renderer' => 'Atwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address',
'filter_condition_callback' => array($this, '_addressFilter'),
));
as you can see we have added one more value filter_condition_callback
and the only thing we need is to add this protected method, which allow us add filtration:
protected function _addressFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->where(
"sales_flat_order_address.city like ?
OR sales_flat_order_address.street like ?
OR sales_flat_order_address.postcode like ?"
, "%$value%");
return $this;
}
Full article you can find here: http://www.atwix.com/magento/grid-filter-for-columns/
On my Admin side customer grid, i have a situacion, some times and some customers on column name i need First Name + Last Name, it's already done by magento core, but some customer i need just to print First name. Take a look on my Grid.php, i need take off last name from 'name' catenation, but when i remove from collection my column sort stop work.
<?php
class Mage_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('customerGrid');
$this->setUseAjax(true);
$this->setDefaultSort('entity_id');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
/*foreach($collection as $customer){
preg_match("/[A-z| ]*//*", $customer->getName(), $name);
$customer->setName($name[0]);
unset($name);
}*/
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
/*
$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
$this->addColumn('lastname', array(
'header' => Mage::helper('customer')->__('Last Name'),
'index' => 'lastname'
));
*/
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'sortable' => true,
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt'=> 0))
->load()
->toOptionHash();
$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode',
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'billing_country_id',
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region',
));
$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('customer')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}
$this->addColumn('action',
array(
'header' => Mage::helper('customer')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('customer')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('customer');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('customer')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('customer')->__('Are you sure?')
));
$this->getMassactionBlock()->addItem('newsletter_subscribe', array(
'label' => Mage::helper('customer')->__('Subscribe to Newsletter'),
'url' => $this->getUrl('*/*/massSubscribe')
));
$this->getMassactionBlock()->addItem('newsletter_unsubscribe', array(
'label' => Mage::helper('customer')->__('Unsubscribe from Newsletter'),
'url' => $this->getUrl('*/*/massUnsubscribe')
));
$groups = $this->helper('customer')->getGroups()->toOptionArray();
array_unshift($groups, array('label'=> '', 'value'=> ''));
$this->getMassactionBlock()->addItem('assign_group', array(
'label' => Mage::helper('customer')->__('Assign a Customer Group'),
'url' => $this->getUrl('*/*/massAssignGroup'),
'additional' => array(
'visibility' => array(
'name' => 'group',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $groups
)
)
));
return $this;
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=> true));
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
}
}
Its wrong way i know, but have to get just first name and don't have other way.
But now this make my sort on columns from grid stop work. I need sort working.
If i remove this 3 lines from my _prepareCollection(), it's work fine but i need it too and with it don't work my sort. I'm crazy with this someone can help???
Ty in advise.
You're best bet would be to load the firstname attribute on the collection and add that as an independent column. Adding column to customer grid in Magento - Data not populating will give you a good start. You should just need to add in ->addAttributeToSelect('firstname') and then add the column in the prepareColumns function...
$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
In my custom module I created a grid which loads all the products. I have following columns added already:
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('price', array(
'header' => Mage::helper('catalog')->__('Price'),
'type' => 'currency',
'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
'index' => 'price'
));
$this->addColumn('sku', array(
'header' => Mage::helper('catalog')->__('SKU'),
'width' => '90',
'index' => 'sku',
));
$this->addColumn('status', array(
'header' => Mage::helper('catalog')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
1 => 'Enabled',
2 => 'Disabled',
),
));
$this->addColumn('type', array(
'header'=> Mage::helper('catalog')->__('Type'),
'width' => '60px',
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
));
Now I want to add the product type column as a dropdown so that I can filter the products by produt type i.e. simple, configurable etc.
EDITTED
Just added this to the function and it worked:
$this->addColumn('type', array(
'header'=> Mage::helper('catalog')->__('Type'),
'width' => '60px',
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
));
Just added this to the function and it worked:
$this->addColumn('type', array(
'header'=> Mage::helper('catalog')->__('Type'),
'width' => '60px',
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
));