简体   繁体   中英

Adding Status Column to Customer Information -> Orders in Magento

I've been attempting to add a "Status" column in the Orders grid that's shown when you view the Customer Information -> Orders tab.

I've been able to add the column, and the filter populates with the correct status values, but it breaks the default sorting and you can't filter by any item or sort.

I know there's probably a much better way of adding the column, but all the examples I found related to the main Orders table and I couldn't figure out how to modify it for this use case.

Here's my extension code - in /Pnp/Customer/Block/Customer/Edit/Tab/Orders.php

<?php

class Pnp_Customer_Block_Customer_Edit_Tab_Orders extends Mage_Adminhtml_Block_Customer_Edit_Tab_Orders
{
  public function __construct()
  {
    parent::__construct();
    $this->setId('customer_orders_grid');
    $this->setUseAjax(true);
    $this->setDefaultSort('created_at');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
  }

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('sales/order_grid_collection')
        ->addFieldToSelect('entity_id')
        ->addFieldToSelect('increment_id')
        ->addFieldToSelect('customer_id')
        ->addFieldToSelect('created_at')
        ->addFieldToSelect('grand_total')
        ->addFieldToSelect('order_currency_code')
        ->addFieldToSelect('store_id')
        ->addFieldToSelect('billing_name')
        ->addFieldToSelect('shipping_name')
        ->addFieldToSelect('status')
        ->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
        ->setIsCustomerMode(true);

    $this->setCollection($collection);
    return $this;
}

protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $this->addColumn('status', array(
            'header' => Mage::helper('sales')->__('Status'),
            'index' => 'status',
            'type'  => 'options',
            'width' => '70px',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses()
        ));

        return parent::_prepareColumns();
    }

}

Thanks in advance for any help you can offer!


Many thanks to the answers so far.

I've nearly got there with a combination of Harit and Emipro Technologies answers, here's what I have so far:

etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Pnp_Customer>
            <version>0.4.0</version>
        </Pnp_Customer>
    </modules>
    <global>
        <models>
            <pnp_statusgrid>
                <class>Pnp_Customer_Model</class>
            </Pnp_statusgrid>
        </models>
    </global>
    <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    <statusgrid_column_append>
                        <type>model</type>
                        <class>Pnp_Customer_Model_Observer</class>
                        <method>appendCustomColumn</method>
                    </statusgrid_column_append>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>
</config>

Model/Observer.php

<?php

class Pnp_Customer_Model_Observer extends Varien_Event_Observer
{
/**
 * Adds column to admin customers grid
 *
 * @param Varien_Event_Observer $observer
 * @return Pnp_Customer_Model_Observer
 */
public function appendCustomColumn(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();
    if (!isset($block)) {
        return $this;
    }

    if ($block->getType() == 'adminhtml/customer_edit_tab_orders') {
        /* @var $block Mage_Adminhtml_Block_Customer_Grid */
        $block->addColumn('status', array(
            'header'    => Mage::helper('customer')->__('Status'),
            'type'      => 'options',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
            'width'     => '70px',
            'index'     => 'status',
            'renderer'  => 'Pnp_Customer_Block_Customer_Edit_Tab_Renderer_Status',
    }
}
}

Block/Customer/Edit/Tab/Orders.php

<?php

class Pnp_Customer_Block_Customer_Edit_Tab_Orders extends Mage_Adminhtml_Block_Customer_Edit_Tab_Orders
{

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('sales/order_grid_collection')
        ->addFieldToSelect('entity_id')
        ->addFieldToSelect('increment_id')
        ->addFieldToSelect('customer_id')
        ->addFieldToSelect('created_at')
        ->addFieldToSelect('grand_total')
        ->addFieldToSelect('order_currency_code')
        ->addFieldToSelect('store_id')
        ->addFieldToSelect('billing_name')
        ->addFieldToSelect('shipping_name')
        ->addFieldToSelect('status')
        ->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
        ->setIsCustomerMode(true);

    $this->setCollection($collection);
    return $this;
}


}

Block/Customer/Edit/Tab/Renderer/status.php

<?php

class Pnp_Customer_Block_Customer_Edit_Tab_Renderer_Status extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {


public function render(Varien_Object $row)
{
if (!$value = $this->getColumn()->getIndex()){
    return $this;
} 

$value =  $row->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($value);
return '<span >'.$order['status']'</span>';
}

}

?>

Everything is working except the column shows the status name as underscored, ie payment_pending. I looked at the object being returned and can't see the label version. As a final thing to get the above finished, would anyone be able to point me in the right direction to print the human readable version of the attribute?


Final Edit:

I managed to figure it out - I changed:

return '<span >'.$order['status']'</span>';

to

return '<span >'.$order->getStatusLabel().'</span>';

This is definately work add renderer

change your column by

$this->addColumn('status', array(
    'header'    => Mage::helper('customer')->__('Status'),
    'index'     => 'status',
    'type' => 'options',
    'options' => $this->_getorderstatus(),
    'renderer'  => 'Pnp_Customer_Block_Customer_Edit_Renderer_Status',
));

Add new function in same file

protected function _getorderstatus(){
    $ordstatus = (array) Mage::getModel('sales/order_status')->getResourceCollection()->getData();
    $orderstatus=array();

    foreach($ordstatus as $item){
        $orderstatus[$item['status']]=$item['label'];
    }
return $orderstatus;
} 

status.php put function like

public function render(Varien_Object $row)
{
if (!$value = $this->getColumn()->getIndex()){
    return $this;
} 
$value =  $row->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($value);
return '<span >'.$order['status'].'</span>';
}

An easier and reliable way to achieve this is through observer.

  1. Create a Module

  2. Inside app/code/local/My/Module/etc/config.xml

 <global> <models> <my_customgrid> <class>My_Module_Model</class> </my_customgrid> </models> </global> <adminhtml> <events> <core_block_abstract_prepare_layout_before> <observers> <customgrid_column_append> <type>model</type> <class>My_Module_Model_Observer</class> <method>appendCustomColumn</method> </customgrid_column_append> </observers> </core_block_abstract_prepare_layout_before> </events> </adminhtml> 

What it does is, it allows you to observe the block before its layout is prepared.

  1. Create a file called Observer.php for your module and write down following:

    class My_Module_Model_Observer extends Varien_Event_Observer {

     /** * * @param Varien_Event_Observer $observer * @return \\My_Module_Model_Observer */ public function appendCustomColumn(Varien_Event_Observer $observer) { $block = $observer->getBlock(); if (!isset($block)) { return $this; } if ($block->getType() == 'adminhtml/customer_grid') { /* @var $block Mage_Adminhtml_Block_Customer_Grid */ $block->addColumnAfter('field', array( 'header' => 'Your Field', 'type' => 'text', 'index' => 'field', ), 'email'); } } 

    }

Follow this link for more information: http://www.atwix.com/magento/add-column-to-customers-grid-alternative-way/

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM