5

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'];
                 $itemsArray[$order_item_id]=$quantity; 
            }

        $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);

        $creditmemo->setShippingAmount($shippingAmount);
        $creditmemo->setTaxAmount($taxAmount);
        $creditmemo->setBaseTaxAmount($taxAmount);
        $creditmemo->setSubtotal($subTotal);
        $creditmemo->setBaseSubtotal($subTotal);
        $creditmemo->setGrandTotal($grandTotal);
        $creditmemo->setBaseGrandTotal($grandTotal);

        $this->creditmemoService->refund($creditmemo);

       return $creditmemo->getId();




    }
}

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

1
  • 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

0

replace of your code line from :

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

to

$creditmemo = $this->creditmemoFactory->createByOrder($order,$itemsArray);
3
  • 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.