Cancelling Particular Order Items in Magento 2
When dealing with orders in Magento 2, you might find yourself needing to cancel specific order items without affecting the entire order status. This tutorial will guide you through the process of creating a custom module that allows for the cancellation of individual order items.
Understanding Magento’s Order Management
In Magento 2, an order represents multiple line items (order items). Each item has its own SKU, quantity, and other attributes. The total order status is determined by the worst-performing item within itâfor example, if one item is on hold or pending payment, the entire order remains in that status.
Creating a Custom Module for Order Cancellation
To achieve this functionality, we’ll create an admin panel module with actions to cancel specific order items.
Step 1: Set Up Your Custom Module
Create a new directory under app/code named Company_CancelOrderItem. Inside this folder, create the required directories and files:
app/
âââ code/
âââ Company/
âââ CancelOrderItem/
âââ etc/
âââ module.xml
âââ Controller/
â âââ Adminhtml/
â âââ Index.php
âââ registration.php
Step 2: Define Your Module
In registration.php, define the basic information about your module:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Company_CancelOrderItem',
__DIR__
);
Step 3: Configure Your Module
Update the etc/module.xml file to include required dependencies and configurations:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Company_CancelOrderItem" setup_version="1.0.0">
<sequence>
<dependency name="Magento_Sales"/>
</sequence>
</module>
</config>
Step 4: Create the Cancel Order Item Controller
In Controller/Adminhtml/Index.php, create a controller action that handles the cancellation process:
<?php
namespace Company\CancelOrderItem\Controller\Adminhtml;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Backend\App\Action
{
protected $resultPageFactory;
public function __construct(Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
**$orderId = (int)$this->getRequest()->getParam('order_id');
$itemId = (int)$this->getRequest()->getParam('item_id');
try {
// Load the order and item
$order = $this->getOrderById($orderId);
$item = $this->getItemById($order, $itemId);
// Cancel the specific order item
**$item->setStatus(\Magento\Sales\Model\Order\Item::STATUS_CANCELED);**
$item->save();
// If all items are canceled, update the order status (optional)
if ($this->isAllItemsCanceled($order)) {
$order->setState(\Magento\Sales\Model\Order::STATE_CANCELED);
$order->save();
}
$this->messageManager->addSuccessMessage(__('The item was canceled successfully.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultPageFactory->create();
}
}
Step 5: Implement the Helper Functions
Implement helper functions getOrderById(), getItemById(), and isAllItemsCanceled() in your module to handle order and item retrieval, cancellation, and status checking.
Conclusion
With this custom module in place, you can now cancel specific order items from the Magento 2 admin panel without affecting the entire order status. By following these steps, you’ve gained more control over individual order item management within your store’s backend.
To learn more about developing custom modules for Magento 2 and other advanced e-commerce solutions, explore our articles at Laravel Company.
Remember to test your module thoroughly before deploying it to a production environment. This approach ensures that you can handle specific order item cancellations efficiently while maintaining the integrity of your order management system.
Happy coding!