cancel
Showing results for 
Search instead for 
Did you mean: 

Order without credit memo

SOLVED

Order without credit memo

Hello everyone,

 

I am creating orders and generating invoices programmatically.

Everything works perfectly but there is no credit memo button in the orders created.

 

Some resources on the web are saying that the credit memo will appear if a shipment is created first, and then and invoice is generated.

 

It's a bit surprising as a merchant could want to refund an order that doesn't have shipment (virtual), or refund a payment for which the items haven't been shipped yet.

 

How can I programmatically make sure that the orders created have a credit memo?

Is shipment really required for that to happen?

 

Thanks a lot for your answers.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Order without credit memo

Just in case it could help someone, here is how to create an invoice programmatically, making sure the credit memo button shows on the order. Compared to the code posted above, I was missing the 2 last steps below: create a transaction for the invoice and update the order totals.

 

use Magento\Sales\Model\Service\InvoiceService;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Framework\DB\Transaction;
... 
    public function __construct(
        InvoiceService $invoiceService,
        InvoiceRepositoryInterface $invoiceRepository,
        Transaction $transaction      
    ) {
        $this->invoiceService     = $invoiceService;
        $this->invoiceRepository  = $invoiceRepository;
        $this->transaction        = $transaction;
    }
...

    public function createInvoice() {
        // Prepare the invoice
        $invoice = $this->invoiceService->prepareInvoice($this->order);
        $invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
        $invoice->setState(Invoice::STATE_PAID);
        $invoice->setBaseGrandTotal($this->amount);
        $invoice->register();
        $invoice->getOrder()->setIsInProcess(true);
        $invoice->pay();
// Create the transaction $transactionSave = $this->transaction ->addObject($invoice) ->addObject($this->order); $transactionSave->save(); // Update the order $this->order->setTotalPaid($this->order->getTotalPaid()); $this->order->setBaseTotalPaid($this->order->getBaseTotalPaid()); $this->order->save(); // Save the invoice $this->invoiceRepository->save($invoice); }

Everything works perfectly now.

View solution in original post

7 REPLIES 7

Re: Order without credit memo

So I have managed to make the credit memo button appear after generating an invoice manually. It means that the shipment requirement is not relevant.

 

However, this credit memo button doesn't appear when I generate the invoice programmatically. Here is my code:

 

        // Prepare the invoice
        $invoice = $this->invoiceService->prepareInvoice($this->order);
        $invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
        $invoice->setState(Invoice::STATE_PAID);
        $invoice->setBaseGrandTotal($this->amount);
        $invoice->register();

        // Save the invoice
        $this->invoiceRepository->save($invoice);

This creates an invoice for the order, but no credit memo button. Is there something I should add to this code to make sure the credit memo shows up?

Re: Order without credit memo

Hi @jlinker

 

By default in native magento this functionality is not available  !

 

We can generate credit memo only when we have shipment generated and invoice generated for any order !

 

Once shipment is generated then and then credit memo button will be shown - this is native functionality and e-commerce workflow of magento !

 

if you want credit memo button will show before shipment then you need to do customization for the same !

 

For more reference refer this link for the same - https://docs.magento.com/m2/ee/user_guide/sales/credit-memos.html

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Order without credit memo

@Manthan Dave, thank you for your comment.

Please see my message above, it looks like I can generate invoice manually without shipment and it makes the credit memo button appear.

 

But credit memo button doesn't show if I generate the invoice programmatically with the code snippet. 

Re: Order without credit memo

Hi @jlinker

 

After generation of invoice , you are trying to create credit memo programatically ? 

 

Refer this link for the same - https://magento.stackexchange.com/questions/139432/how-to-create-credit-memo-programmatically-in-mag...

 

You will get an idea for the same !

 

 

if issue solved,Click Kudos & Accept as Solution

Re: Order without credit memo

No, I would like the credit memo button to show up when I create an invoice programmatically.

Re: Order without credit memo

Just in case it could help someone, here is how to create an invoice programmatically, making sure the credit memo button shows on the order. Compared to the code posted above, I was missing the 2 last steps below: create a transaction for the invoice and update the order totals.

 

use Magento\Sales\Model\Service\InvoiceService;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Framework\DB\Transaction;
... 
    public function __construct(
        InvoiceService $invoiceService,
        InvoiceRepositoryInterface $invoiceRepository,
        Transaction $transaction      
    ) {
        $this->invoiceService     = $invoiceService;
        $this->invoiceRepository  = $invoiceRepository;
        $this->transaction        = $transaction;
    }
...

    public function createInvoice() {
        // Prepare the invoice
        $invoice = $this->invoiceService->prepareInvoice($this->order);
        $invoice->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
        $invoice->setState(Invoice::STATE_PAID);
        $invoice->setBaseGrandTotal($this->amount);
        $invoice->register();
        $invoice->getOrder()->setIsInProcess(true);
        $invoice->pay();
// Create the transaction $transactionSave = $this->transaction ->addObject($invoice) ->addObject($this->order); $transactionSave->save(); // Update the order $this->order->setTotalPaid($this->order->getTotalPaid()); $this->order->setBaseTotalPaid($this->order->getBaseTotalPaid()); $this->order->save(); // Save the invoice $this->invoiceRepository->save($invoice); }

Everything works perfectly now.

Re: Order without credit memo

Hi @jlinker

 

Glad to know that you have resolved your issue !

 

Thanks for sharing the detailed answer , its helps to other people for the same issue !

 

 

if issue solved,Click Kudos & Accept as Solution