Ever wondered if it is possible how to split the parent order’s item and pass it on to create a child order in Magento? Yes, it is and you will have to build a custom code for the same.
What is Split Order in Magento 2?
The Concept of Splitting an order is to break the parent order items into smaller subsets(child order), Say you received an order with 4 items, and would only like to have 2 items on it and split the other 2 items into another child order. This breaking of parent order to a subset of child order is referred to as Split Order.
Why do you need to split an order?
With the adverse growth in the ecommerce marketplace, merchants would always find ways to be on top of the competitors, say it be delivery, shipment, or packing.
For Splitting an order there can be many reasons some of those are:
Order Fulfillment: The Order received may need to be processed from multiple warehouses based on availability and the nearest location of delivery.
Packaging: The items ordered cannot be packaged together and may need to be put in multiple boxes for shipment.
Stock availability: Out of a large order received a few need to go to back order, in such a situation why hold on to other deliveries?
So here you can find the basic concept of how an order can be split based on its items and a sample code.
Read Also: Top 5 B2B Extensions from Klizer Store
Steps Involved In Split Order in Magento 2:
- Get the Order ID of which you want to split.
- Get the Item SKUs of the order that you want to pass it on to a child order.
- Create a new quote and assign all the parent order details/attributes that you want to pass on to the child order. Some of the examples are Customer Details, Billing & Shipping Address, Shipping Methods, and Order Notes.
- The below code snippet explains how to achieve this functionality, you can find comments with each execution for better understanding.
<?php
namespace Vendor\Module\Controller;
class SplitOrder extends \Magento\Backend\App\Action{
public function __construct(
\Magento\Sales\Model\Order $order,
\Magento\Catalog\Model\Product $productModel,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Quote\Model\QuoteManagement $quoteManagement
){
$this->order = $order;
$this->productModel = $productModel;
$this->quoteFactory = $quoteFactory;
$this->quoteRepository = $quoteRepository;
$this->quoteManagement = $quoteManagement;
}
public function splitOrder($order_id){
$order = $this->order->load($order_id);
if(count($order->getAllItems()) > 0){
foreach ($order->getAllItems() as $item) {
$product = $this->productModel->load($item->getProduct()->getId());
/** Fetching the Item SKU’s which have not been cancelled yet, You can fetch the Item SKU’s based on your requirement **/
if($item->getQtyCanceled() == 0){
$items[$item->getSku()] = $item->getQtyOrdered();
}
}
}
if(count($items) > 0){
/**Here create a custom IncrementId to assign for the child order, Alternatively you can use your own too**/
$newIncrementId = $order->getIncrementId().”-1″;
if($newIncrementId != “”){
/** Here get and assign all required details from parent order to child order such as Order notes, Customer ID and Shipping Method **/
$order_notes = $order->getOrderNotes();
$customer_id = $order->getCustomerId();
$shipping_method = $order->getShippingMethod();
.
.
.
/** Similarly get and assign other required order details which you want to pass on to the child order **/
/** Create a new quote for the child order and assign all the parent order’s fetched details**/
$quote = $this->quoteFactory->create();
$quote->setIsSuperMode(true);
$quote->setStoreId($order->getStoreId());
$quote->setCustomerId($customer_id);
.
.
.
$quote->setTotalsCollectedFlag(false)->collectTotals();
$this->quoteRepository->save($quote);
$quote->save();
/** Up-on submitting the quote a new child order gets created with the selected Items. **/
$new_order = $this->quoteManagement->submit($quote);
return $new_order;
}
}
}
Important Features of the Magento 2 Split Order Marketplace
Automated Separation of Order IDs
Order IDs are automatically separated based on the seller associated with each order.
Separate Invoice and Shipment Generation
Both the store owner and the sellers can generate individual invoices and shipments for each order ID, catering to different vendors.
Efficient Return and Refund Management
The module facilitates easy management of returns and refunds, allowing sellers to handle these processes seamlessly.
Streamlined Order Management
The extension enables store owners and merchants to manage orders efficiently, providing a unique order ID for each seller.
Klizer Extensions: Split Order for Magento 2
Klizer’s extension allows your online store to split orders into individual orders for each item in the cart. With different order IDs, customers can view all their order IDs in their Order History and track each item separately. Admins can generate separate invoices and shipments for each split order. Shipping charges and taxes are also split based on items. This extension supports Magento 2’s default offline payment methods: Check/Money Order and Cash on Delivery.