warehouse_item
table, where the primary key is the combination of the warehouse identifier and product identifier: CREATE TABLE warehouse_item ( product_id ..., warehouse_id ..., qty ..., PRIMARY KEY (product_id, warehouse_id), ... )
cataloginventory_stock_item.qty
) with their total value SUM(warehouse_item.qty)
.Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider
, and data on the quantity of the product is added to the provider through the DI settings ( magento/module-catalog-inventory/etc/adminhtml/di.xml
): <type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider"> <arguments> <argument name="addFieldStrategies" xsi:type="array"> <item name="qty" xsi:type="object">Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection</item> </argument> ... </arguments> </type>
AddQuantityFieldToCollection
class: namespace Magento\CatalogInventory\Ui\DataProvider\Product; ... class AddQuantityFieldToCollection implements AddFieldToCollectionInterface { public function addField(Collection $collection, $field, $alias = null) { $collection->joinField( 'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left' ); } }
addField
method not to work out and not add to the collection the amount of product from cataloginventory_stock_item
, you need to intercept its execution using a plugin using the around
method.etc/di.xml
or etc/adminhtml/di.xml
): <?xml version="1.0"?> <config ...> <type name="Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection"> <plugin name="vendor_module_plugin" type="Vendor\Module\Plugin\AddQuantityFieldToCollection" sortOrder="100" disabled="false"/> </type> </config>
addField
method, addField
create in the plugin a method aroundAddField
that " does nothing ": namespace Vendor\Module\Plugin; use Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection as Subject; class AddQuantityFieldToCollection { public function aroundAddField(Subject $subject, \Closure $proceed) { return; } }
./var/generation/*
) and moving the WebUI Admin to the product grid, the newly created "interceptor" code can be found in the file ./var/generation/Magento/CatalogInventory/Ui/DataProvider/Product/AddQuantityFieldToCollection/Interceptor.php
: <?php namespace Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection; /** * Interceptor class for @see * \Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection */ class Interceptor extends \Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection implements \Magento\Framework\Interception\InterceptorInterface { use \Magento\Framework\Interception\Interceptor; public function __construct() { $this->___init(); } /** * {@inheritdoc} */ public function addField(\Magento\Framework\Data\Collection $collection, $field, $alias = null) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'addField'); if (!$pluginInfo) { return parent::addField($collection, $field, $alias); } else { return $this->___callPlugins('addField', func_get_args(), $pluginInfo); } } }
___callPlugins
after else), the white one is our own plugin.before
, after
, around
"wrappers" for the original methods: return $this->___callPlugins('addField', func_get_args(), $pluginInfo);
return parent::addField($collection, $field, $alias);
Source: https://habr.com/ru/post/279413/
All Articles