Total noob in magento here.

What I want is to select a bunch of orders, and create credit memos for them if their status is completed, and after the creditmemo is created, I want to set their statuses to Refunded/Cancelled, based on the payment method used.

Any tips on how I should go about the whole process?



I have created an action in the sales_order_grid.xml as follows:

<action name="masscreditmemos_order">
                <url path="sales/order/masscreditmemos"/>
                <label translate="true">Generate Credit Memos</label>

Then created a file named "masscreditmemos.php" in


with the following content:

 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
namespace Magento\Sales\Controller\Adminhtml\Order;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class masscreditmemos extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
     * Authorization level of a basic admin session
    const ADMIN_RESOURCE = 'Magento_Sales::creditmemo';

     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory

public function __construct(
    \Magento\Sales\Model\Order $order,
    \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory,
    \Magento\Sales\Model\Order\Invoice $invoice,
    CollectionFactory $collectionFactory,
    \Magento\Sales\Model\Service\CreditmemoService $creditmemoService,
    array $data = []
    ) {
        $this->order = $order;
        $this->creditmemoFactory = $creditmemoFactory;
        $this->collectionFactory = $collectionFactory;
        $this->creditmemoService = $creditmemoService;
        $this->invoice = $invoice; 

public function massAction(AbstractCollection $collection)
    $order = $this->order;

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

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

    // Don't set invoice if you want to do offline refund


But when I click the on the "generate credit memo" action, it shows me a 500 error. The error is as follows:


What am I doing wrong?

Also. I know I am supposed to override the vendor modules, I will do that once this runs.

  • You need to create custom extenison and into which add new action for order grid and perform the logic part into that action. – Dhiren Vasoya Aug 22 '18 at 14:27
  • @dhirenVasoya Hi. I was able to make a custom module which added a section called "Generate Credit Memos" in the action drop down. but have some trouble formulating the logic behind the whole operation. I mean, traditionally, when we view an order, and if it's status is "completed/processing" a button for creating a credit memo is shown, which redirects to a form and creates a memo for the record, and changes the status to "closed". Not sure how i'll override the form and do a mass action of generating credit memos. – Fokker Aug 23 '18 at 5:32
  • you need to create credit memo or order programetically into your above action. – Dhiren Vasoya Aug 23 '18 at 5:40
  • Let me try that – Fokker Aug 23 '18 at 5:55
  • @DhirenVasoya Please check my edit. – Fokker Aug 30 '18 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.