Domanda

I want to use below code in Magento 2 constructor.

$stockItem = $objectManager->create('Magento\CatalogInventory\Model\Stock\Item');

Can any one tell me the right way of calling above create class into constructor as a dependency class?

È stato utile?

Soluzione

protected $stockItemFactory;
public function __construct(
    ...
    \Magento\CatalogInventory\Model\Stock\ItemFactory $stockItemFactory,
    ....
) {
    ....
    $this->stockItemFactory = $stockItemFactory;
    ....
}

Then you can use in your class

$stockItem = $this->stockItemFactory->create();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top