1

I am getting this error by programmatically creating credit memo.

any thoughts why this error is appearing ?

3
  • 1
    Hey, just by that I think it'll be hard, can you share the code you're using to create and save? Might be easier to someone to help with more info
    – Rui Silva
    Commented Apr 29, 2021 at 11:10
  • any thoughts on this ?
    – devhs
    Commented Jun 10, 2021 at 11:01
  • any thoughts on this ?
    – devhs
    Commented Jun 10, 2021 at 11:09

2 Answers 2

1
+50

You need to unregister "current_creditmemo" after initiating a new credit memo object.

try this

if (is_numeric($amount)) {
    $amount = max(0, min($creditmemo->getBaseCustomerBalanceReturnMax(), $amount));
    if ($amount) {
        $amount = $this->priceCurrency->roundPrice($amount);
        $creditmemo->setBsCustomerBalTotalRefunded($amount);
        $creditmemo->setCustomerBalTotalRefunded($amount);
        $creditmemo->setCustomerBalanceRefundFlag(true);
        if (!empty($tracking_info)) {
            $tracking_info_comment = implode("\n", $tracking_info);
            $creditmemo->addComment($tracking_info_comment);
        }
        $creditmemo = $this->creditmemoRepository->save($creditmemo);
        $this->registry->unregister('current_creditmemo');
        $this->creditmemoSender->send($creditmemo);
        $result[0] = true;
        return $result;
    }
}
if($this->registry->registry('current_creditmemo')){
    $this->registry->unregister('current_creditmemo');
}
1

try the below code to create a credit memo progrmatically

  $orderincrementid = "1000000001"
    $parentOrder = $this->orderInterface->loadByIncrementId($orderincrementid);
    $orderId = $parentOrder->getId();
    $orderItems = $parentOrder->getAllItems();
    $itemcol = [];
            
    foreach ($orderItems as $item) {
        $itemcol[$item->getItemId()] = [
             'qty' => $item['qty'],
             'back_to_stock' => 1,
            ];
    
    }
   $this->createCreditMemo($itemcol, $orderId, $parentOrder);

call your function

public function createCreditMemo($itemcol, $orderId, $parentOrder)
{
$creditMemoData = [];
$creditMemoData['items'] = $itemcol;
$creditMemoData['do_offline'] = 1;
$creditMemoData['shipping_amount'] = 0;
$creditMemoData['adjustment_positive'] = 0;
$creditMemoData['adjustment_negative'] = 0;
$creditMemoData['comment_text'] = 'comment_text_for_creditmemo';
$creditMemoData['send_email'] = 1;

try {

    $this->creditmemoLoader->setOrderId($orderId); //pass order id
    $this->creditmemoLoader->setCreditmemo($creditMemoData);

    $creditmemo = $this->creditmemoLoader->load();
    if ($creditmemo) {
        if (!$creditmemo->isValidGrandTotal()) {
            throw new LocalizedException(
                __('The credit memo\'s total must be positive.')
            );
        }

        if (!empty($creditMemoData['comment_text'])) {
            $creditmemo->addComment(
                $creditMemoData['comment_text'],
                isset($creditMemoData['comment_customer_notify']),
                isset($creditMemoData['is_visible_on_front'])
            );

            $creditmemo->setCustomerNote($creditMemoData['comment_text']);
            $creditmemo->setCustomerNoteNotify(isset($creditMemoData['comment_customer_notify']));

        }
            if (isset($data['do_offline'])) {
            //do not allow online refund for Refund to Store Credit
            if (!$data['do_offline'] && !empty($data['refund_customerbalance_return_enable'])) {
                throw new \Magento\Framework\Exception\LocalizedException(
                    __('Cannot create online refund for Refund to Store Credit.')
                );
            }
        }

        $creditmemoManagement = $this->_objectManager->create(
            'Magento\Sales\Api\CreditmemoManagementInterface'
        );
        $creditmemoManagement->refund($creditmemo, (bool)$creditMemoData['do_offline']);


        if (!empty($creditMemoData['send_email'])) {
            $this->creditmemoSender->send($creditmemo);
        }

    }
} catch (LocalizedException $e) {
    $this->_logger->info("Failed to create credit memo" . $e->getMessage());
}
}

Your Answer

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

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