【问题标题】:Magento 2 added custom column in sales order grid but unable to filterableMagento 2 在销售订单网格中添加了自定义列但无法过滤
【发布时间】:2022-11-22 16:54:21
【问题描述】:

我在 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="Custom\Module\Ui\Component\Listing\Columns\ProductName">
        <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;
    }
}

【问题讨论】:

    标签: magento2


    【解决方案1】:

    您的过滤器不适用于此方法。

    使用以下代码作为参考 -

    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. 网格.php

        getMainTable() === $collection->getConnection()->getTableName(self::$table)) {$where = $collection->getSelect()->getPart(MagentoFrameworkDBSelect::WHERE); foreach($where as $key => $w){ 如果 (str_contains($w, 'test_product')) { $where[$key] = str_replace("`test_product`","main_table.products",$w); } } $collection->getSelect->setPart(MagentoFrameworkDBSelect::WHERE, $where); } 返回$集合; } }

      希望能帮助到你!!

      您可能需要稍微修改代码才能获得所需的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多