1

We load the following code to show the payment method inside the Sales/Order grid.

But this displays the payment code and we want to display the payment title.

How can we change our current code to show the title?

CODE:

$select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));

CODE filterPaymentMethod:

public function filterPaymentMethod($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $collection->getSelect()->having(
        "group_concat(`sales_flat_order_item`.sku SEPARATOR ', ') like ?", "%$value%");

    return;
}
7
  • use additional_information column instead of method column. in this column payment title is saved like - {"method_title":"Check \/ Money order"} Commented Oct 29, 2018 at 11:35
  • @ShashankKumrawat Thanks! Tried this but I can not get the right result, what should be the right code to use?
    – JGeer
    Commented Oct 29, 2018 at 11:41
  • which file you are using this code ? Commented Oct 29, 2018 at 11:44
  • Check my answer
    – Hitesh
    Commented Oct 29, 2018 at 11:45
  • @ShashankKumrawat We use a custom module for this, that loads the code. We tried this: $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('method_title' => 'additional_information'));
    – JGeer
    Commented Oct 29, 2018 at 11:50

2 Answers 2

1

Try below way

you can use like this to get the payment method in your custom module or override core files /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

protected function _prepareCollection()
    {
         $collection = Mage::getResourceModel($this->_getCollectionClass());
         $collection->join(array('payment'=>'sales/order_payment'),'main_table.entity_id=parent_id','method');
         $this->setCollection($collection);
         return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
         // end here //
    }

and in grid

 protected function _prepareColumns()
    {

 $payments = Mage::getSingleton('payment/config')->getActiveMethods();

        $methods = array();
        foreach ($payments as $paymentCode=>$paymentModel)
        {
                $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
                $methods[$paymentCode] = $paymentTitle;
        }

        $this->addColumn('method', array(
                'header' => Mage::helper('sales')->__('Payment Method'),
                        'index' => 'method',
                        'filter_index' => 'payment.method',
                        'type'  => 'options',
                        'width' => '70px',
                        'options' => $methods,
                ));
        // End here
    }

hope this will work for you

6
  • Thanks for your reply. But we can not use this, because we load the code using a custom module. So we need to modify the current code.
    – JGeer
    Commented Oct 29, 2018 at 11:51
  • then you should put above code into your custom module I mean only _prepareCollection and _prepareColumns methods
    – Hitesh
    Commented Oct 29, 2018 at 11:52
  • Thanks, we use the module: atwix.com/magento/column-to-orders-grid. How to implement your code into this?
    – JGeer
    Commented Oct 29, 2018 at 12:07
  • you can just find sales order grid code in atwix and just replace or append above code as your need OR override atwix's sales order grid file into your custom module
    – Hitesh
    Commented Oct 29, 2018 at 12:12
  • I do not really get it, how to extend this code. Can you show me what I need to edit?
    – JGeer
    Commented Oct 29, 2018 at 12:21
1

You need column renderer functionality, here is just basic idea how you can do it, (this code is not tested) -

in your module you need to use renderer param in file extendedgrid.xml like below --

<action method="addColumnAfter">
         <columnId>payment_method</columnId>
         <arguments>
            <header>Payment Method</header>
            <index>payment_method</index>
            <filter_index>payment.method</filter_index>
            <renderer>Atwix_ExtendedGrid_Block_Adminhtml_Sales_Order_Grid_Renderer</renderer>
            <type>text</type>
         </arguments>
        <after>shipping_name</after>
</action>

and create file 'Renderer.php' at path Atwix/ExtendedGrid/Block/Adminhtml/Sales/Order/Grid

<?php

    class Atwix_ExtendedGrid_Block_Adminhtml_Sales_Order_Grid_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
   {    
       public function render(Varien_Object $row)
       {
          $paymentMethodCode =  $row->getData($this->getColumn()->getIndex());
         /* $payments = Mage::getSingleton('payment/config')->getActiveMethods(); // will get all active payment method*/

          $payments = Mage::getModel('payment/config')->getAllMethods();  // get all available payment method

        
          $paymentTitle = '';
          foreach ($payments as $paymentCode=>$paymentModel)
          {
              if($paymentMethodCode == $paymentCode)
              {
                 $paymentTitle =Mage::getStoreConfig('payment/'.$paymentCode.'/title');
              
              }
              
           }

           return $paymentTitle;
        }
    } 

Reference link for renderer functionality :-

http://www.justwebdevelopment.com/blog/add-custom-column-and-get-custom-renderer-value-in-magento-grid/

http://magento.ikantam.com/qa/how-add-custom-renderer-magento-grid

EDIT:- in case if you use block file instead of layout file for column in grid, use below function to apply filter on renderer column.

public function filterPaymentMethod($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }
    
        $collection->getSelect()->join(
                    'sales_flat_order_payment',           
                    'main_table.entity_id = sales_flat_order_payment.parent_id',
                    array('method'))
                 ->where("sales_flat_order_payment.method like ?", "%$value%");

        return $collection;
    }
 

Edit2:- replace your function filterPaymentMethod in observer.php file with below code --

public function filterPaymentMethod($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $payments =  Mage::getSingleton('payment/config')->getAllMethods();

        $codeToFilter ='';
        foreach ($payments as $paymentCode=>$paymentModel) {
            $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
            
            if (stripos($paymentTitle, $value) !== false && !$codeTomatch) {
                $codeTomatch = $paymentCode;
            }
        }
 
        $collection->getSelect()->join(
                    'sales_flat_order_payment',           
                    'main_table.entity_id = sales_flat_order_payment.parent_id',
                    array('method'))
                 ->where("sales_flat_order_payment.method like ?", "%$codeToFilter%");

        return $collection;
    }
46
  • Thanks for your reply! That does seem to work, but the requested payment method should also be active to show. Will to be possible to display the title even if the method is disabled?
    – JGeer
    Commented Oct 29, 2018 at 12:35
  • Yes, because line Mage::getStoreConfig('payment/'.$paymentCode.'/title') is fetching payment title from DB regardless it is active or not. Commented Oct 29, 2018 at 12:37
  • Great, how to edit the code, so that it will be displayed, even if disabled?
    – JGeer
    Commented Oct 29, 2018 at 12:38
  • as suggested in above answer you need to modify extendedgrid.xml file of your module and follow next step as suggested. Commented Oct 29, 2018 at 12:40
  • Yes, we already did modify extendedgrid.xml. That part works perfect, but due to the line $payments = Mage::getSingleton('payment/config')->getActiveMethods(); it only loads active payment methods. And we want to show it always, even if disabled. So how to edit this?
    – JGeer
    Commented Oct 29, 2018 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.