How to properly add a shipping_description column in magento order grid?

后端 未结 3 1086
不思量自难忘°
不思量自难忘° 2021-01-24 21:51

There are many tutorials and suggestions including installing a custom extensions etc.

I\'ve added the shipping_description fine based on various tips and tricks by modi

相关标签:
3条回答
  • 2021-01-24 22:28

    In order to prevent this type of error in sorting columns

    SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘status’ in where clause is ambiguous ERROR 
    

    when sorting your orders…Make sure you add this:

    'filter_index'=>'main_table.status',
    

    to the addColumn Status array

    hope this will sure help you.

    0 讨论(0)
  • 2021-01-24 22:28

    The following worked in my case. Sorting now works from every single field. No core files overridden.

    Step by step for someone who is in the same boat. Tested in 1.8.1

    1. Copy your Grid.php from

      /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

      if the path doesn't exist create it.

    2. Open your new Grid.php and add the following code into _prepareCollection() after getResourceModel line:

      $collection->getSelect()->join(array('mt'=>'sales_flat_order'), 'mt.entity_id = main_table.entity_id', array('mt.increment_id','mt.store_id','mt.created_at','mt.shipping_description','mt.status','mt.base_grand_total','mt.grand_total'));

      $collection->getSelect()->group('main_table.entity_id');

    3. Add the following code into _prepareColumns() function (in between your desired addColumn() calls)

      $this->addColumn('shipping_description', array( 'header' => Mage::helper('sales')->__('Delivery'), 'type' => 'text', 'index' => 'shipping_description',
      'filter_index' => 'mt.shipping_description', ));

    4. Find addColumns() functions for increment_id, store_id, created_at, base_grand_total, grand_total, status and add the following argument in each:

      'filter_index' => 'mt.___your_column_name____',

    Any suggestion on how to optimise above code are welcome.

    0 讨论(0)
  • 2021-01-24 22:42

    I guess what you are doing is adding the "shipping_description" field in the collection but, i can help you out by an easy one. i.e using renderer in the grid. This is a lot easier in my view.

    After you override the grid block for order(Override the block is a good practice) add this to your _prepareColumn() function

     $this->addColumn('shipping_description', array(
                'header'=> Mage::helper('sales')->__('Shipping Description'),
                'index' => 'shipping_description',
                'filter' => false,
                'sortable' => false,
                'renderer' => 'PackageName_ModuleName_Block_Adminhtml_Sales_Order_Renderer_Shipping',
            )); 
    

    Here you can see the renderer this points to the class "PackageName_ModuleName_Block_Adminhtml_Sales_Order_Renderer_Shipping". Now,go on and create folder Named "Renderer" in the above mentioned path and inside that folder create "Shipping.php" file.

    <?php
    class Custom_Customer_Block_Adminhtml_Customer_Renderer_Lifetime extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
          $customerId = $row->getData('entity_id');
    
            $customer = Mage::getModel('customer/customer')->load($customerId);
            $customerTotals = Mage::getResourceModel('sales/sale_collection')
                 ->setCustomerFilter($customer)
                 ->load()
                 ->getTotals();
            $customerLifetimeSales = $customerTotals->getLifetime();
            //$customerNumberOfOrders = $customerTotals->getNumOrders();
           echo Mage::helper('core')->currency($customerLifetimeSales); 
        }
    }
    

    In above class i override the customer module to determine the lifetime sales of the customer. In this function you can do whatever operation and what you "return" or "echo" in this file will be displayed in the grid.

    In this way you don't need to join tables in the collection.Just call the model that gets you shipping description and print it.That's it.It will make lot easier

    Hope this will help.

    0 讨论(0)
提交回复
热议问题