سؤال

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?

هل كانت مفيدة؟

المحلول

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top