For adding custom fields to Orders Grid i choose this way: Magento2 : sales order grid add new column with it’s values
I create file [Vendor]/[Module]/view/adminhtml/ui_component/sales_order_grid.xml
With code:
<?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="export_status" class="[Vendor][Module]UiComponentListingColumnGiftstatus">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Gift Option</item>
</item>
</argument>
</column>
</columns>
</listing>
And added this file: [Vendor]/[Module]/Ui/Component/Listing/Column/Giftstatus.php
With code:
<?php
namespace VendorModuleUiComponentListingColumn;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoFrameworkViewElementUiComponentContextInterface;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoUiComponentListingColumnsColumn;
use MagentoFrameworkApiSearchCriteriaBuilder;
class Giftstatus 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"]);
$status = $order->getData("gift_message_id");
if($status){
$message = 'Yes';
}
else{
$message = 'No';
}
$item[$this->getData('name')] = $message;
}
}
return $dataSource;
}
}
I have successfully added the fields I need, but When I try to sort the grid by my custom columns, I get this error
In exception.log file i have this message:
[2021-12-27 17:02:10] report.CRITICAL: SQLSTATE[42S22]: Column not found: 1054
Unknown column "sender" in "order clause", query was: SELECT "main_table".*, "braintree_transaction_details"."transaction_source" FROM "sales_order_grid" AS "main_table"
LEFT JOIN "braintree_transaction_details" ON braintree_transaction_details.order_id = main_table.entity_id ORDER BY sender ASC
LIMIT 20 {"exception":"[object] (Zend_Db_Statement_Exception(code: 42): SQLSTATE[42S22]: Column not found: 1054 Unknown column "sender" in "order clause", query was: SELECT "main_table".*, "braintree_transaction_details"."transaction_source" FROM "sales_order_grid" AS "main_table"
LEFT JOIN "braintree_transaction_details" ON braintree_transaction_details.order_id = main_table.entity_id ORDER BY sender ASC
LIMIT 20 at vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column "sender" in "order clause" at /vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} []
How i can fix this?