2

Is anyone knows how to load the model in magento2?

What we need to do in magento2 to translate the below line in Magento2

$order = $observer->getEvent()->getOrder();

        $orders = Mage::getModel('sales/order_invoice')->getCollection()
                        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
7
public function __construct(
    ..
    \Magento\Framework\ObjectManagerInterface $objectManager,
    ..
) {
    $this->_objectManager = $objectManager;
}

and then

$this->_objectManager->create('Magento\Sales\Model\Resource\Order\Invoice\Collection')
    ->addAttributeToFilter('order_id', array('eq'=>$order->getId()))

OR

public function __construct(
    \Magento\Sales\Model\Resource\Order\Invoice\CollectionFactory $invoiceCollectionFactory
) {
    $this->_invoiceCollectionFactory = $invoiceCollectionFactory;
}

and then

$this->_invoiceCollectionFactory->create()
        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
3

You can also instantiate an object inline (without declaring it in a constructor) with the code:

/** @var \Magento\Framework\ObjectManagerInterface $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\Sales\Model\Resource\Order\Invoice\Collection $invoices */
$invoices = $om->get('Magento\Sales\Model\Resource\Order\Invoice\Collection');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.