0

I added an attribute in sales_order, and display it as a column in sales_order_grid, all is set but when I try to apply filter on my custom column it gives me an error with a pop-up like: Attention: Something went wrong, Yes! I also get some errors in console and in network please see below images:

enter image description here enter image description here enter image description here Here Is my code: app/code/EC/SampleModule/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="sample_status" class="EC\SampleModule\Ui\Component\Listing\Column\Status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">EC\SampleModule\Model\Source\printStatus</item>
                <item name="config" xsi:type="array">                    
                    <item name="filter" xsi:type="string">select</item>
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="label" xsi:type="string" translate="true">Sample Status</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

app/code/EC/SampleModule/Model/Source/printStatus.php

<?php

namespace EC\SampleModule\Model\Source;

class printStatus implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @return array
     */
    public function toOptionArray()
    {
        $options = [
            [
                'value' => 0,
                'label' => __('No')
            ],
            [
                'value' => 1,
                'label' => __('Yes')
            ]
        ];

        return $options;
    }
}

app/code/EC/SampleModule/Ui/Component/Listing/Column/Status

<?php 

namespace EC\SampleModule\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;
use \Magento\Framework\App\ResourceConnection;

class Status extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;
    protected $_resourceConnection;

    public function __construct(
        ContextInterface $context, 
        UiComponentFactory $uiComponentFactory, 
        OrderRepositoryInterface $orderRepository, 
        SearchCriteriaBuilder $criteria,
        ResourceConnection  $resourceConnection,
        array $components = [], 
        array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->_resourceConnection = $resourceConnection;
        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']);
                $sampleStatus = $order->getSampleStatus();
                $item['sample_status'] = $sampleStatus;
            }
        }

        return $dataSource;
    }
}
0

1 Answer 1

0

I solved my issue:

app/code/EC/OrderPrintStatus/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="custom_print_status" class="EC\OrderPrintStatus\Ui\Component\Listing\Column\CustomPrintStatus" component="Magento_Ui/js/grid/columns/select">
            <settings>
                <filter>select</filter>
                <options class="EC\OrderPrintStatus\Ui\Component\Listing\Column\CustomPrintStatus\Options"/>
                <dataType>select</dataType>
                <label translate="true">Print Status</label>
            </settings>
        </column>
    </columns>
</listing>

app/code/EC/OrderPrintStatus/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
         <arguments>
             <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">EC\OrderPrintStatus\Model\ResourceModel\Order\Grid\Collection</item>
             </argument>
         </arguments>
     </type>
</config>

app/code/EC/OrderPrintStatus/Model/ResourceModel/Order/Grid/Collection.php

<?php

namespace EC\OrderPrintStatus\Model\ResourceModel\Order\Grid;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;

/**
 * Order grid extended collection
 */
class Collection extends OriginalCollection
{
    protected function _initSelect()
    {
        $this->addFilterToMap('created_at', 'main_table.created_at');
        $this->addFilterToMap('increment_id', 'main_table.increment_id');
        $this->addFilterToMap('base_grand_total', 'main_table.base_grand_total');
        $this->addFilterToMap('grand_total', 'main_table.grand_total');
        $this->addFilterToMap('store_id', 'main_table.store_id');
        $this->addFilterToMap('billing_name', 'main_table.billing_name');
        $this->addFilterToMap('shipping_name', 'main_table.shipping_name');
        $this->addFilterToMap('status', 'main_table.status');
        parent::_initSelect();
    }
     protected function _renderFiltersBefore()
     {
         $joinTable = $this->getTable('sales_order');
         $this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order.entity_id', ['custom_print_status']);
         parent::_renderFiltersBefore();
     }
}

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.