I am trying to add partial credit memo programatically but it is consuming whole quantity in first credit memo, How can I pass specific items to credit memo?

public function createCreditMemo ($entityId,$invId,$shipping,$subtotal,$grandtotal,$tax) {

        $post_data = json_decode(file_get_contents('php://input'), true);
    $itemsArray = array();

        foreach($post_data['items'] as $index => $details){ 
                 $quantity = $details['qty'];           
         $order_item_id = $details['order_item_id'];

        $orderId = $entityId; 
        $order = $this->_orderRepository->get($orderId);    
        $shippingAmount = $shipping;
        $subTotal = $subtotal;
        $taxAmount = $tax;
        $grandTotal = $grandtotal;
        $invoiceId = $invId;

        $invoiceobj =  $this->invoice->load($invId);
        $creditmemo = $this->creditmemoFactory->createByInvoice($invoiceobj,$itemsArray);



       return $creditmemo->getId();


I am passing item array still it is taking all the items of invoice.

  • You can check the native adminhtml flow here : vendor/magento/module-sales/Controller/Adminhtml/Order/Creditmemo/Save.php Nov 21, 2018 at 8:59

1 Answer 1


replace of your code line from :

$creditmemo = $this->creditmemoFactory->createByInvoice($invoiceobj,$itemsArray);


$creditmemo = $this->creditmemoFactory->createByOrder($order,$itemsArray);
  • Now it is taking full ordered quantity. Nov 13, 2018 at 15:24
  • do you verify that, you get proper data into $itemsArray? Nov 14, 2018 at 3:50
  • Yes, I am sending only one item I have verified that. Is there any specific form of array required? Nov 14, 2018 at 9:21

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.