I need to add a custom filter to the Order Grid to show just the orders that have this specific SKU (TEST). The select box should be has two options: Include (Show order’s that have this specific sku) or Exclude (Show order’s that doesn’t have this specific sku).
Actually, I got to add this filter as column in the order grid BUT I can’t use as filter. I try to select the option for example (Only) in the filter but show me a error.
app/code/Vendor/Module/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_order" class="VendorModuleUiComponentListingColumnCustomProduct">
<settings>
<filter>select</filter>
<options class="VendorModuleUiComponentListingColumnCustomProductOptions"/>
<dataType>select</dataType>
<label translate="true">Custom Product</label>
</settings>
</column>
</columns>
</listing>
app/code/Vendor/Module/Ui/Component/Listing/Column/CustomProduct.php
<?php
namespace VendorModuleUiComponentListingColumn;
use MagentoFrameworkEscaper;
use MagentoUiComponentListingColumnsColumn;
use MagentoFrameworkViewElementUiComponentContextInterface;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoSalesModelResourceModelOrderStatusCollectionFactory;
/**
* Class Items
*/
class CustomProduct extends Column
{
/**
* @var Escaper
*/
protected $escaper;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param Escaper $escaper
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Escaper $escaper,
MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
array $components = [],
array $data = []
) {
$this->escaper = $escaper;
$this->orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$orderIds = [];
foreach ($dataSource['data']['items'] as & $item) {
$orderIds[] = $item['entity_id'];
}
$orderCollection = $this->orderCollectionFactory->create()->addFieldToFilter('entity_id', ['in' => $orderIds]);
foreach ($dataSource['data']['items'] as & $item) {
$order = $orderCollection->getItemById($item['entity_id']);
$data = [];
foreach ($order->getAllVisibleItems() as $orderItem) {
$data[] = $orderItem->getSku();
}
if(in_array("Test1", $data)){
$item[$this->getData('name')] = 'Yes';
}else{
$item[$this->getData('name')] = 'No';
}
}
}
return $dataSource;
}
}
app/code/Vendor/Module/Ui/Component/Listing/Column/CustomProduct/Options.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModuleUiComponentListingColumnRFProduct;
use MagentoFrameworkDataOptionSourceInterface;
/**
* Class Options
*/
class Options implements OptionSourceInterface
{
/**
* @var array
*/
protected $options;
/**
* Get Yes/No options
*
* @return array
*/
public function toOptionArray() :array
{
if ($this->options === null) {
$this->options[] = [
'value' => 'yes',
'label' => __('Only')
];
$this->options[] = [
'value' => 'no',
'label' => __('Exclude')
];
}
return $this->options;
}
}
Someone can help me how I can add a query to filter each one of the option that was created? please
guilherme is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.