Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This isn't too hard at all actually!</p> <p>When I did this I was adding the last_login to the customer grid.</p> <p>The first step, which you have already done but I'm including here for completeness, is to add the column to the initial select statement:</p> <pre><code>/** * set collection object * * @param Mage_Customer_Model_Resource_Customer_Collection $collection */ public function setCollection($collection) { //Group by email since multiple customer_ids can exist in the log/customer. $collection-&gt;groupByEmail(); //join the last_login field here. $collection-&gt;getSelect()-&gt;joinLeft(array('c_log' =&gt; $collection-&gt;getTable('log/customer')), 'c_log.customer_id=e.entity_id', array('last_login' =&gt; 'login_at')); parent::setCollection($collection); } </code></pre> <p>Next, we add the column to the grid. Notice we are defining two callbacks. 'filter_condition_callback' is stock Magento. Oddly, there is no callback index (so far as I am aware) to sort on this. We need to add a sort callback otherwise we can't sort our new column.</p> <pre><code> $this-&gt;addColumnAfter('last_login', array( 'header' =&gt; Mage::helper('customer')-&gt;__('Last Login'), 'type' =&gt; 'datetime', 'align' =&gt; 'center', 'index' =&gt; 'last_login', 'filter_index' =&gt; '`c_log`.`login_at`', 'gmtoffset' =&gt; true, //Stock Magento Callback - Notice the callback key has been assigned. 'filter_condition_callback' =&gt; 'filter_last_login', //Custom Callback Index 'order_callback' =&gt; 'sort_last_login' ), 'customer_since'); </code></pre> <p>Next, we add the callback functions that will handle the filtering &amp; sorting:</p> <pre><code>if (!function_exists('filter_last_login')) { /** * @param Mage_Customer_Model_Resource_Customer_Collection $collection * @param Mage_Adminhtml_Block_Widget_Grid_Column $column */ function filter_last_login($collection, $column) { if (!$column-&gt;getFilter()-&gt;getCondition()) { return; } $condition = $collection-&gt;getConnection() -&gt;prepareSqlCondition('c_log.login_at', $column-&gt;getFilter()-&gt;getCondition()); $collection-&gt;getSelect()-&gt;where($condition); } } if (!function_exists('sort_last_login')) { /** * @param Mage_Customer_Model_Resource_Customer_Collection $collection * @param Mage_Adminhtml_Block_Widget_Grid_Column $column */ function sort_last_login($collection, $column) { $collection-&gt;getSelect()-&gt;order($column-&gt;getIndex() . ' ' . strtoupper($column-&gt;getDir())); } } </code></pre> <p>Finally, since the order_callback is not a real index we need to call this callback if defined instead of the default sorting mechanism. This is how i accomplished it:</p> <pre><code>/** * Sets sorting order by some column * * @param Mage_Adminhtml_Block_Widget_Grid_Column $column * * @return Mage_Adminhtml_Block_Widget_Grid */ protected function _setCollectionOrder($column) { if ($column-&gt;getOrderCallback()) { call_user_func($column-&gt;getOrderCallback(), $this-&gt;getCollection(), $column); return $this; } return parent::_setCollectionOrder($column); } </code></pre> <p>Hope this helps someone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload