Saya mencoba menambahkan kolom baru untuk memesan kisi di Magento 2.0. Jadi, saya perlu bergabung untuk memesan koleksi grid. Bagaimana saya bisa mencapai ini? Karena, di magento2, kisi-kisi menggunakan komponen UI.
Saya mencoba menambahkan kolom baru untuk memesan kisi di Magento 2.0. Jadi, saya perlu bergabung untuk memesan koleksi grid. Bagaimana saya bisa mencapai ini? Karena, di magento2, kisi-kisi menggunakan komponen UI.
Jawaban:
Magento 2 menambahkan kolom khusus ke kisi pesanan penjualan,
Bergabung
Koleksi Magento \ Sales \ Order \ Grid \
ke tabel mana saja, menggunakan plugin akan menjadi pilihan terbaik karena ini tidak bergantung pada penulisan ulang dan membuat kode lebih ramping.
Buat plugin di etc / di.xml modul Anda
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<plugin name="sales_order_additional_columns" type="Vendor\ModuleName\Plugins\AddColumnsSalesOrderGridCollection" sortOrder="100" disabled="false" />
</type>
Jadi, kami sedang mencegat
Magento \ Framework \ View \ Element \ UiComponent \ DataProvider \ CollectionFactory
karena jika Anda melihat
Magento \ Penjualan \ dll \ di.xml
kamu akan melihat
Koleksi Magento \ Sales \ Order \ Grid \
disuntikkan ke
Magento \ Framework \ View \ Element \ UiComponent \ DataProvider \ CollectionFactory
Buat folder Plugin dan kelas plugin di modul Anda
<?php namespace Vendor\ModuleName\Plugins;
use Magento\Framework\Message\ManagerInterface as MessageManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;
class AddColumnsSalesOrderGridCollection
{
private $messageManager;
private $collection;
public function __construct(MessageManager $messageManager,
SalesOrderGridCollection $collection
) {
$this->messageManager = $messageManager;
$this->collection = $collection;
}
public function aroundGetReport(
\Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
\Closure $proceed,
$requestName
) {
$result = $proceed($requestName);
if ($requestName == 'sales_order_grid_data_source') {
if ($result instanceof $this->collection
) {
$select = $this->collection->getSelect();
$select->join(
["soi" => "sales_order_item"],
'main_table.entity_id = soi.order_id AND soi.product_type="simple"',
array('weight', 'product_type')
)
->distinct();
$select->join(
["soa" => "sales_order_address"],
'main_table.entity_id = soa.parent_id AND soa.address_type="shipping"',
array('email', 'country_id', 'postcode', 'city', 'telephone')
)
->distinct();
}
}
return $this->collection;
}
}
Di sini kita mengamati sekitar acara metode getReport ().
Salinan
vendor / magento / modul-sales / view / adminhtml / ui_component / sales_order_grid.xml
ke ruang lingkup modul Anda
Vendor / ModuleName / view / adminhtml / ui_component / sales_order_grid.xml
Hapus semua konten sales_order_grid.xml yang Anda salin karena kami tidak ingin menimpa semua konten.
Masukkan kode berikut di sales_order_grid.xml modul Anda
<?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">
<!-- sales_order_item weight -->
<column name="weight">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Weight</item>
<item name="sortOrder" xsi:type="number">222</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_item product_type-->
<column name="product_type">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\ModuleName\Ui\Component\Listing\Column\ProductTypes</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Product Type</item>
<item name="sortOrder" xsi:type="number">232</item>
<item name="align" xsi:type="string">right</item>
<!--<item name="filter" xsi:type="string">select</item>-->
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="dataType" xsi:type="string">select</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address country_id -->
<column name="country_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Country ID</item>
<item name="sortOrder" xsi:type="number">242</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address post_code -->
<column name="postcode">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Postcode</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address city -->
<column name="city">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">City</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
<!-- sales_order_address telephone -->
<column name="telephone">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Telephone</item>
<item name="sortOrder" xsi:type="number">252</item>
<item name="align" xsi:type="string">right</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<!--<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>-->
</item>
</argument>
</column>
</columns>
</listing>
Sekarang, hapus cache dari folder var / cache atau segarkan cache Anda. Anda dapat melihat kolom yang ditambahkan di kotak pesanan penjualan.
Ketika Anda melihat \Magento\Framework\Data\Collection\AbstractDb
magento2 sendiri menyediakan operasi hook untuk koleksi Anda.
protected function _renderFilters()
{
if ($this->_isFiltersRendered) {
return $this;
}
$this->_renderFiltersBefore(); // Hook for operations before rendering filters
....................
}
Jadi, apa yang perlu Anda lakukan ini hanya dengan menambahkan koleksi Anda [ NAMESPACE\MODULENAME\Model\ResourceModel\YOUR_CLASSNAME\Grid\Collection
]
protected function _renderFiltersBefore() {
$joinTable = $this->getTable('catalog_product_entity_varchar');
$this->getSelect()->join($joinTable.' as cpev','main_table.entity_id = cpev.entity_id', array('*'));
parent::_renderFiltersBefore();
}
_renderFiltersBefore
, Anda juga dapat mengganti / memperluas _initSelect
.
Saya membuat kisi admin yang menggabungkan dua tabel khusus. Anda tidak dapat melakukan ini dengan menggunakan tipe virtual di.xml, jadi Anda harus mengikuti langkah-langkah ini dan memperbarui
dll / di.xml,
Model / Resource / Modulename / Collection.php tambahkan bergabung dalam file ini,
Model / Sumber Daya / Modulename / Grid / Collection.php,
DI dll / di.xml Anda
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="namespace_modulename_listing_data_source" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Namespace\Modulename\Model\Resource\Modulename\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">tablename</argument>
<argument name="eventPrefix" xsi:type="string">namespace_modulename_grid_collection</argument>
<argument name="eventObject" xsi:type="string">namespace_grid_collection</argument>
<argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename</argument>
</arguments>
</type>
DALAM Model / Sumber Daya / Modulename / Collection.php Anda
<?php
namespace Namespace\Modulename\Model\Resource\Modulename;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Define model & resource model
*/
const YOUR_TABLE = 'tablename';
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
$this->_init(
'Namespace\Modulename\Model\Modulename',
'Namespace\Modulename\Model\Resource\Modulename'
);
parent::__construct(
$entityFactory, $logger, $fetchStrategy, $eventManager, $connection,
$resource
);
$this->storeManager = $storeManager;
}
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()->joinLeft(
['secondTable' => $this->getTable('tablename')],
'main_table.columnname = secondTable.columnname',
['columnname1','columnname2','columnname3']
);
}
}
?>
DALAM Model / Sumber Daya / Modulename / Grid / Collection.php Anda
<?php
namespace Namespace\Modulename\Model\Resource\Modulename\Grid;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Search\AggregationInterface;
use Namespace\Modulename\Model\Resource\Modulename\Collection as ModulenameCollection;
/**
* Class Collection
* Collection for displaying grid
*/
class Collection extends ModulenameCollection implements SearchResultInterface
{
/**
* Resource initialization
* @return $this
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
$mainTable,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
$connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$storeManager,
$connection,
$resource
);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
$this->setMainTable($mainTable);
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
/**
* @param AggregationInterface $aggregations
*
* @return $this
*/
public function setAggregations($aggregations)
{
$this->aggregations = $aggregations;
}
/**
* Get search criteria.
*
* @return \Magento\Framework\Api\SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return null;
}
/**
* Set search criteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null
) {
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount()
{
return $this->getSize();
}
/**
* Set total count.
*
* @param int $totalCount
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setTotalCount($totalCount)
{
return $this;
}
/**
* Set items list.
*
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
}
?>
Dalam definisi xui ui ada node sumber data mirip dengan ini
<dataSource name="listing_name_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">UniqueNameGridDataProvider</argument>
<argument name="name" xsi:type="string">listing_name_data_source</argument>
di mana listing_name_data_source
dapat didefinisikan dalam Anda di.xml
atau hanya referensi kelas secara langsung. Kelas itu sendiri harus diperluas dari Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory
dan sebagai collections
argumen koleksi khusus Anda. Dalam _initSelect()
metode kelas koleksi Anda dapat bergabung dengan tabel Anda.
Bagi siapa pun yang memiliki masalah dengan solusi @ Asrar lakukan saja ini:
public function aroundGetReport(
\Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
\Closure $proceed,
$requestName
) {
$result = $proceed($requestName);
if ($requestName == 'sales_order_grid_data_source_firsty') {
if ($result instanceof $this->collection
) {
$select = $this->collection->getSelect();
$select->join(
["soi" => "sales_order_item"],
'main_table.entity_id = soi.order_id',
array('sku', 'name','item_id')
)
->distinct();
return $this->collection;
}
}
return $result;
}
Ini sepertinya berfungsi baik untuk saya.
["soi" => "sales_order_item"]
dan["soa" => "sales_order_address"]
.