0

I want invoice item data using order_item_id and I tried using

use Magento\Sales\Api\InvoiceItemRepositoryInterface;
private function getInvoiceItemById($orderItemId)
    {
        return $this->invItemRepository->get($orderItemId);
    }

But i am not getting any data and instead i am facing an error

Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Sales\Api\Data\InvoiceItemInterfacePersistor::__construct() must implement interface Magento\Sales\Model\Spi\InvoiceItemResourceInterface, instance of Magento\Sales\Model\ResourceModel\Order\Invoice\Item given, called in /vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/generation/Magento/Sales/Api/Data/InvoiceItemInterfacePersistor.php:58 Stack trace: #0 /vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Magento\Sales\Api\Data\InvoiceItemInterfacePersistor->__construct(Object(Magento\Sales\Model\ResourceModel\Order\Invoice\Item), Object(Magento\Sales\Api\Data\InvoiceItemInterfaceFactory), Object(Magento\Framework\App\ResourceConnection))

2 Answers 2

0

This seems to an issue related to DI files.

Can you run the following command from the root of the magento project and check if this resolves your problem?

php bin/magento setup:di:compile
1
  • hi bluscience thanks for the reply but still no luck same issue again. Jan 25, 2019 at 8:21
0

The below code is working fine

protected $invoiceItemFactory;

public function __construct(
    \Magento\Sales\Model\Order\Invoice\ItemFactory $invoiceItemFactory
) {
    $this->invoiceItemFactory = $invoiceItemFactory;
}


public function getInvoiceItemById()
{

    $orderItemId = 454;

    /*Get single invoice item data */
    $invoiceItem = $this->invoiceItemFactory->create()->load($orderItemId, 'order_item_id');
    $invoiceItemId = $invoiceItem->getId();

    /*Get collection invoice item data */
    $invoiceItemCollection = $this->invoiceItemFactory->create()->getCollection()
            ->addFieldToFilter('order_item_id', $orderItemId);

    foreach($invoiceItemCollection as $invoiceItem){
        $id = $invoiceItem->getId();
        $invoicedItemqty = $invoiceItem->getQty();
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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