产品列表网格中的自定义列未过滤

时间:2019-01-29 10:36:48

标签: magento2

我已经通过模块在Product网格中创建了一个自定义列,该列在那里显示了父产品的SKU。它工作正常,但是当我尝试对其进行过滤时,Magento给出“处理默认视图时出现了问题,并且我们已将过滤器恢复为原始状态。”错误。并且在exception.log中,错误是:

  

[2019-01-03 11:33:08] main.CRITICAL:无效的属性名称:   parentid_col {“ exception”:“ [对象]   (Magento \ Framework \ Exception \ LocalizedException(代码:0):无效   属性名称:位于的parentid_col   /html/vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php:1383)“}   []

这是我的代码

  

/app/code/CustomColumn/Addproductcolumn/view/adminhtml/ui_component/product_listing.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="product_columns">
<column name="parentid_col" class="CustomColumn\Addproductcolumn\Ui\Component\Listing\Column\ParentProductId">
<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
          <item name="label" xsi:type="string" translate="true">Parent id</item>
          <item name="filter" xsi:type="string">text</item>
          <item name="visible" xsi:type="boolean">true</item>
          <item name="sortOrder" xsi:type="number">50</item>
          <item name="align" xsi:type="string">left</item>
          <item name="dataType" xsi:type="string">text</item>
          <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
    </item>
</argument>
</column>
</columns>
</listing>
  

app / code / CustomColumn / Addproductcolumn / Ui / Component / Listing / Column / ParentProductId.php

<?php


namespace CustomColumn\Addproductcolumn\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;

class ParentProductId extends Column
{
    protected $configurable;
    protected $bundle;
    protected $_productFactory; 

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,

        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
        \Magento\Bundle\Model\Product\Type $bundle,

        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->_configurable = $configurable;
        $this->_bundle = $bundle;
        $this->_productFactory = $productFactory;

    }

    public function prepareDataSource(array $dataSource)
    {


        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$items) {
            if($items['type_id'] == 'simple'){

                $productId = $items['entity_id'];
                $parentProducts = '';

                $getParent = $this->_configurable->getParentIdsByChild($productId);
                $parentData = $this->_productFactory->create()->load($getParent);
                $getParentSku = $parentData->getSku();

                if(isset($getParentSku)){
                    $parentProducts .= $getParentSku;    
                }

                $getParent = $this->_bundle->getParentIdsByChild($productId);
                if(isset($getParent)){

                    foreach ($getParent as $p) {
                        $parentData = $this->_productFactory->create()->load($p);
                        $getParentSku = $parentData->getSku();
                        $parentProducts .= $getParentSku.',';
                      }  
                        $items['parentid_col'] = $parentProducts;
                }

            }
        }
    }
        return $dataSource;
    }


}

1 个答案:

答案 0 :(得分:0)

请在此处查看类似的问题。 https://magento.stackexchange.com/questions/188508/magento-2-how-to-add-a-custom-column-in-customer-grid-like-is-approved?rq=1用于客户属性,但相同的方法应该起作用。

基本知识是:

添加属性= app / code / Namespace / Modulename / Setup / InstallData.php和'is_used_in_grid'=> true

创建UI组件= app / code / Namespace / Modulename / view / adminhtml / ui_component / customer_listing.xml

将索引器添加到/ etc / = app / code / Namespace / Modulename / etc / indexer.xml

运行indexer = php bin / magento indexer:reindex