2

Given an order that has been invoiced (by cash on delivery for testing) and is in state "processing", how can I create a credit memo? in code that is outside of Magento routing

EDIT:

I have found many solutions for creating a credit memo. But I want to create a credit memo from the root of Magento.

3
  • we need to code inside the magento 2 root just like a custome script to perticuller to one order is generate the credit memo. Apr 10, 2018 at 10:34
  • I reopened your question. Becuase you have different question than other. Check my answer for creating credit memo from the root. Apr 10, 2018 at 10:57
  • If you know how to create a credit memo then the question is not how to do that, but how to do something from the root of Magento. So then your question would be duplicate of the question how to run a script from the root of Magento...
    – 7ochem
    Apr 10, 2018 at 11:37

2 Answers 2

4

Here is the script for creating credit memo from Magento root

Create createcreditmemo.php at the root of magento

use \Magento\Framework\App\Bootstrap;

include('app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInterface = $objectManager->get('\Magento\Sales\Api\Data\OrderInterface');

//Use this if you have orderId
//$orderId = "100"; //Order Id
//$order = $orderInterface->load($orderId);

$incrementId = "000000214"; //Increment Id
$order = $objectManager->create('Magento\Sales\Model\Order')
        ->loadByAttribute('increment_id', $incrementId);
$invoice = $objectManager->create('Magento\Sales\Model\Order\Invoice');
$creditMemoFacory = $objectManager->create('Magento\Sales\Model\Order\CreditmemoFactory');
$creditmemoService = $objectManager->create('Magento\Sales\Model\Service\CreditmemoService');

try {
    $invoices = $order->getInvoiceCollection();
    foreach ($invoices as $invoice) {
        $invoiceincrementid = $invoice->getIncrementId();
    }

    $invoiceobj = $invoice->loadByIncrementId($invoiceincrementid);
    $creditmemo = $creditMemoFacory->createByOrder($order);

    // Don't set invoice if you want to do offline refund
    $creditmemo->setInvoice($invoiceobj);

    $creditmemoService->refund($creditmemo); 

    echo "CreditMemo Succesfully Created For Order: ".$incrementId;
} catch (\Exception $e) {
   echo "Creditmemo Not Created". $e->getMessage();
}

Change order increment id or order id as per your requirement. And run this script at the root of Magento to create creditmemo.

1
  • How to create credit memo by product item not all items May 12, 2020 at 10:25
3

Your Can Creaditmemo Using Below code.

protected $refundOrder;

public function __construct(
     \Magento\Sales\Model\RefundOrder $refundOrder,
     \Magento\Sales\Model\Order\Creditmemo\ItemCreationFactory $itemCreationFactory
){
     $this->refundOrder = $refundOrder;
     $this->itemCreationFactory = $itemCreationFactory;
}

public function creaditMemo($itemId, $qty, $orderId) {

        try {
            $creditmemoItem = $this->itemCreationFactory->create();
            $creditmemoItem->setQty($qty)->setOrderItemId($itemId);
            $itemIdsToRefund[] = $creditmemoItem;
            $this->refundOrder->execute($orderId, $itemIdsToRefund);
        } catch (\Exception $ex) {
            throw $ex;
        }
    }

Pass Your Item Id, Qty of CreaditMemo for that item & OrderId.

7
  • we need to code inside the magento 2 root just like a custome script to perticuller to one order is generate the credit memo. Apr 10, 2018 at 10:34
  • You can also refer magento.stackexchange.com/a/139661/51548 Apr 10, 2018 at 10:40
  • we need to code custome just like <?php use Magento\Framework\App\Bootstrap; require DIR . '/app/bootstrap.php'; error_reporting(E_ALL); ini_set('display_errors', 1); echo "<pre>"; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $objectManager->get('\Magento\Framework\App\State')->setAreaCode('frontend'); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); Apr 10, 2018 at 10:42
  • How can I do online refund using same? Oct 26, 2018 at 12:09
  • How to pass here an array of specific items? May 12, 2020 at 10:26

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