1

How to add column to sales returns grid, returns grid using _prepareColumns to add columns to grid \Magento\Rma\Block\Adminhtml\Rma\Grid

1
  • Have you try below solution? Apr 30, 2019 at 9:36

2 Answers 2

1

Try with below way!

Extend Magento RMA block file to your custom module.

app/code/Namespace/Modulename/Block/Adminhtml/Rma

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

namespace Namespace\Modulename\Block\Adminhtml\Rma;

/**
 * RMA Grid
 */
class Grid extends \Magento\Rma\Block\Adminhtml\Rma\Grid
{
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Rma\Model\ResourceModel\Rma\Grid\CollectionFactory $collectionFactory
     * @param \Magento\Rma\Model\RmaFactory $rmaFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Rma\Model\ResourceModel\Rma\Grid\CollectionFactory $collectionFactory,
        \Magento\Rma\Model\RmaFactory $rmaFactory,
        array $data = array())
    {
        parent::__construct(
            $context,
            $backendHelper,
            $collectionFactory,
            $rmaFactory,
            $data);
    }

    /**
     * Prepare related item collection
     *
     * @return \Magento\Rma\Block\Adminhtml\Rma\Grid
     */
    protected function _prepareCollection()
    {
        $this->_beforePrepareCollection();
        return parent::_prepareCollection();
    }

    /**
     * Configuring and setting collection
     *
     * @return $this
     */
    protected function _beforePrepareCollection()
    {
        if (!$this->getCollection()) {
            /** @var $collection \Magento\Rma\Model\ResourceModel\Rma\Grid\Collection */
            $collection = $this->_collectionFactory->create();
            $this->setCollection($collection);
        }
        return $this;
    }

    /**
     * Prepare grid columns
     *
     * @return \Magento\Rma\Block\Adminhtml\Rma\Grid
     */
    protected function _prepareColumns()
    {
        parent::_prepareColumns();
        $this->addColumn(
            'custom_column',
            [
                'header' => __('Custom Column'),
                'index' => 'custom_column',
                'header_css_class' => 'col-name',
                'column_css_class' => 'col-name'
            ]
        );
        return $this;
    }

    /**
     * Prepare massaction
     *
     * @return $this
     */
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('entity_ids');

        $this->getMassactionBlock()->addItem(
            'status',
            [
                'label' => __('Close'),
                'url' => $this->getUrl($this->_getControllerUrl('close')),
                'confirm' => __(
                    'You have chosen to change status(es) of the selected RMA requests to Close.'
                    . ' Are you sure you want to continue?'
                )
            ]
        );

        return $this;
    }

    /**
     * Get Url to action
     *
     * @param  string $action action Url part
     * @return string
     */
    protected function _getControllerUrl($action = '')
    {
        return '*/*/' . $action;
    }

    /**
     * Retrieve row url
     *
     * @param \Magento\Framework\DataObject $row
     * @return string
     */
    public function getRowUrl($row)
    {
        return $this->getUrl($this->_getControllerUrl('edit'), ['id' => $row->getId()]);
    }
}

It will be showing like below.

enter image description here

I hope it helps!

2
  • On search it's displaying blank page. May 13, 2019 at 15:37
  • I'm able to identify how the issue came, on search it looking for custom_column in sales_rma table, but the custom column is not there, because we are using rendere, so I made that column 'sortable' => false, 'filter' => false, in Grid.php _prepareColumns function. May 15, 2019 at 14:04
0
Step 1 : 
    Create Custom Column (Token Number) In Rma Grid Table
Step 2:
    Use preference in di.xml 
    "<preference for="Magento\Rma\Block\Adminhtml\Rma\Grid" type="<Vendor>\<ModuleName>\Block\Adminhtml\Rma\Grid" />"
Step 3:

    Create Grid Class   <Vendor>\<ModuleName>\Block\Adminhtml\Rma\Grid 
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace <Vendor>\<ModuleName>\Block\Adminhtml\Rma;

/**
 * Description of Grid
 *
 * @author sagar
 */
class Grid extends \Magento\Rma\Block\Adminhtml\Rma\Grid {


    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Rma\Model\ResourceModel\Rma\Grid\CollectionFactory $collectionFactory
     * @param \Magento\Rma\Model\RmaFactory $rmaFactory
     * @param array $data
     */
    public function __construct(
            \Magento\Backend\Block\Template\Context $context,
            \Magento\Backend\Helper\Data $backendHelper,
            \Magento\Rma\Model\ResourceModel\Rma\Grid\CollectionFactory $collectionFactory,
            \Magento\Rma\Model\RmaFactory $rmaFactory,
            array $data = array()) {
        parent::__construct(
                $context,
                $backendHelper,
                $collectionFactory,
                $rmaFactory,
                $data);
    }

    /**
     * Prepare related item collection
     *
     * @return \Magento\Rma\Block\Adminhtml\Rma\Grid
     */
    protected function _prepareCollection() {
        $this->_beforePrepareCollection();
        return parent::_prepareCollection();
    }

    /**
     * Configuring and setting collection
     *
     * @return $this
     */
    protected function _beforePrepareCollection() {
        if (!$this->getCollection()) {
            /** @var $collection \Magento\Rma\Model\ResourceModel\Rma\Grid\Collection */
            $collection = $this->_collectionFactory->create();
            $this->setCollection($collection);
        }
        return $this;
    }

    /**
     * Prepare grid columns
     *
     * @return \Magento\Rma\Block\Adminhtml\Rma\Grid
     */
    protected function _prepareColumns() {
        parent::_prepareColumns();
        $this->addColumn(
                'token_number',
                [
                    'header' => __('Token Number'),
                    'index' => 'token_number',
                    'header_css_class' => 'col-name',
                    'column_css_class' => 'col-name'
                ]
        );

        
        return $this;
    }

    /**
     * Prepare massaction
     *
     * @return $this
     */
    protected function _prepareMassaction() {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('entity_ids');

        $this->getMassactionBlock()->addItem(
                'status',
                [
                    'label' => __('Close'),
                    'url' => $this->getUrl($this->_getControllerUrl('close')),
                    'confirm' => __(
                            'You have chosen to change status(es) of the selected RMA requests to Close.'
                            . ' Are you sure you want to continue?'
                    )
                ]
        );

        return $this;
    }

    /**
     * Get Url to action
     *
     * @param  string $action action Url part
     * @return string
     */
    protected function _getControllerUrl($action = '') {
        return '*/*/' . $action;
    }

    /**
     * Retrieve row url
     *
     * @param \Magento\Framework\DataObject $row
     * @return string
     */
    public function getRowUrl($row) {
        return $this->getUrl($this->_getControllerUrl('edit'), ['id' => $row->getId()]);
    }

}

Your Answer

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

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