Magento 2在销售订单网格中添加了自定义列,但无法进行筛选



我已经从XML在销售订单网格中添加了一个产品名称列,它在网格上正确显示,但如果我以过滤器形式使用它,它会给出错误:;出了问题"并且错误日志的错误低于

main.CRITICAL: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products' in 'where clause', query was: SELECT COUNT(*) FROM `sales_order_grid` AS `main_table` WHERE  (((`products` LIKE '%nato%')))

在自定义模块中的sales_order_grid文件中添加了以下代码

<column name="products" class="CustomModuleUiComponentListingColumnsProductName">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">true</item>
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Items Name</item>
<item name="disableAction" xsi:type="boolean">true</item>
<item name="sortable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="number">3</item>
</item>
</argument>
</column>

然后在Custom\Module\UI\component\Listing\Columns 中创建UI组件列

class ProductName 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 &$items) {

$productArr = [];
$order  = $this->_orderRepository->get($items["entity_id"]);
foreach ($order->getAllVisibleItems() as $item) {
$productArr[] = $item->getName(); //to get product name
}
$items['products'] = implode(" , " , $productArr);
unset($productArr);
}
}
return $dataSource;
}
}

您的Filter将无法使用此方法。

使用以下代码作为参考-

  1. sales_order_grid.xml

    <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="column_name">
    <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">Colum Test</item>
    </item>
    </argument>
    </column>
    </columns>
    </listing>
    
  2. 在模块的adminhtml中创建di.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Plugins -->
    <!-- Adds additional data to the orders grid collection -->
    <type name="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
    <plugin name="mageworx_extended_orders_grid_add_data_to_orders_grid"
    type="VendorNamePluginAddDataToOrdersGrid"
    sortOrder="10"
    disabled="false"/>
    </type>
    <type name="MagentoFrameworkViewElementUiComponentDataProviderReporting">
    <plugin name="sales_grid_collection" type="VendorNameModelPluginSalesOrderGrid"/>
    </type>
    </config>
    
  3. 将数据添加到订单网格.php

<?php
namespace VendorNamePlugin;
class AddDataToOrdersGrid
{

private $logger;
public function __construct(
PsrLogLoggerInterface $customLogger,
array $data = []
) {
$this->logger   = $customLogger;
}
public function afterGetReport($subject, $collection, $requestName)
{
if ($requestName !== 'sales_order_grid_data_source') {
return $collection;
}
if ($collection->getMainTable() === $collection->getResource()->getTable('sales_order_grid')) {
try {


$collection->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array('main_table.*','main_table.products AS test_product')); 
} catch (Zend_Db_Select_Exception $selectException) {
$this->logger->log(100, $selectException);
}
}
return $collection;
}
}
  1. Grid.php

    getMainTable((==$collection->getConnection((->getTableName(self::$table(({$where=$collection->getSelect((->getPart(\Magento\Framework\DB\Select::where(;foreach($where as$key=>$w({if(str_contains($w,'test_product'(({$where[$key]=str_replace("`test_product`","main_table.products",$w(;}}$collection->getSelect->setPart(\Magento\Framework\DB\Select::WHERE,$WHERE(;}退货$collection;}}

希望它能有所帮助!!

您可能需要对代码进行一点修改,以获得您想要的结果。

相关内容

  • 没有找到相关文章

最新更新