I am extending this class : Magento\Sales\Model\Service\InvoiceService

My extended file :

namespace Company\Vendor\Model\Service;

use Magento\Sales\Api\InvoiceManagementInterface;
use Magento\Sales\Model\Order;

class InvoiceService extends \Magento\Sales\Model\Service\InvoiceService implements InvoiceManagementInterface

    public function prepareInvoice(Order $order, array $qtys = [])
        $invoice = $this->orderConverter->toInvoice($order);
        $totalQty = 0;
        $qtys = parent::prepareItemsQty($order, $qtys); // modified line
        //$qtys = $this->prepareItemsQty($order, $qtys); // core line
        foreach ($order->getAllItems() as $orderItem) {
            if (!$this->_canInvoiceItem($orderItem)) {
            $item = $this->orderConverter->itemToInvoiceItem($orderItem);
            if ($orderItem->isDummy()) {
                $qty = $orderItem->getQtyOrdered() ? $orderItem->getQtyOrdered() : 1;
            } elseif (isset($qtys[$orderItem->getId()])) {
                $qty = (double) $qtys[$orderItem->getId()];
            } elseif (empty($qtys)) {
                $qty = $orderItem->getQtyToInvoice();
            } else {
                $qty = 0;
            $totalQty += $qty;
            $this->setInvoiceItemQuantity($item, $qty);
        return $invoice;

I am getting an error here:$this->prepareItemsQty($order, $qtys);

So I am a bit confused about how to call another function of parent functions here.

Note: I don't want to declare code of prepareItemsQty() here (extended class).

  • You want to override the \Magento\Sales\Model\Service\InvoiceService class or you want to extends \Magento\Sales\Model\Service\InvoiceService? – Amit Bera Sep 6 '19 at 7:57
  • I want to extend the class – Praveen Chelumalla Sep 6 '19 at 10:35

Function prepareItemsQty(Order $order, array $qtys = []) is declared as private in the parent class, so you can't access it directly from subclasses. You can declare the private method as protected instead but I not recommend do it with Magento Core files. Or you can use the following code:

public function prepareInvoice(Order $order, array $qtys = [])
    $invoice = parent::prepareInvoice($order, $array);

    // To do something

    return $invoice;

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.