2

I am trying to add invoice increment ID to order grid in magento 2 admin like below:

app/code/Payment/Status/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="invoice_id" class="Payment\Status\Ui\Component\Listing\Column\Invoice">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Invoice #</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

app/code/Payment/Status/Ui/Component/Listing/Column/Invoice.php

<?php
namespace Payment\Status\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class Invoice extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $invoice_ids = array();
                foreach ($order->getInvoiceCollection() as $invoice)
                {
                    $invoice_ids[] = $invoice->getIncrementId();
                }
                if(count($invoice_ids)>0){
                    $invoice_id = implode(', ', $invoice_ids);
                }
                else{
                    $invoice_id = '';
                }

                $item[$this->getData('name')] = $invoice_id;
            }
        }

        return $dataSource;
    }
}

And it showing correctly but filter and sorting not working for that column.

Can you anyone help?

0

Your Answer

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

Browse other questions tagged or ask your own question.