Using column aliases in Sales Order Grid field

假装没事ソ 提交于 2019-12-04 19:46:21

let try this first code

public function setCollection($collection){
    parent::setCollection($collection);
    $collection->getSelect()->join(
        array('address_shipping' => $collection->getTable("sales/order_address")),
        'main_table.entity_id = address_shipping.parent_id AND address_shipping.address_type = "shipping"',
        array('address_shipping.postcode as shippingpostcode')
    );

    $collection->getSelect()->join(
        array('address_billing' => $collection->getTable("sales/order_address")),
        'main_table.entity_id = address_billing.parent_id AND address_billing.address_type = "billing"',
        array('address_billing.postcode as billingpostcode')
    );
}

Second _prepareColumns() is here,

protected function _prepareColumns(){
     $this->addColumn('shippingpostcode', array(
        'header' => Mage::helper('sales')->__('Shipping Postcode'),
        'index' => 'shippingpostcode',
        'filter_index' => 'address_shipping.postcode'
     ));

    $this->addColumn('billingpostcode', array(
        'header' => Mage::helper('sales')->__('Billing Postcode'),
        'index' => 'billingpostcode',
        'filter_index' => 'address_billing.postcode'  
    ));

return parent::_prepareColumns();

}

if you want to know more about 'filter_index', comment in/out in this, then try sorting in your grid for post code column. You will see different result. If you remove filter_index, error in sorting.

pus

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'));

And in the method _prepareColumns paste:

$this->addColumn('bp', array(
            'header' => Mage::helper('sales')->__('Billing Postcode'),
            'index' => 'bp',
            'width' => '60px',
            'filter_index'  => 'billing.postcode'
        ));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!