I am getting this error by programmatically creating credit memo.
any thoughts why this error is appearing ?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI am getting this error by programmatically creating credit memo.
any thoughts why this error is appearing ?
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');
}
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());
}
}