How To Split Order in Magento 2

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 does it mean to split an order?

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. 

Steps Involved To Split An Order:

  1. Get the Order ID of which you want to split.
  2. Get the Item SKUs of the order which you want to pass it on to a child order.
  3. Create a new quote and assign all the parent order’s details/attributes which you want to pass on to the child order. Some of the examples are Customer Details, Billing & Shipping Address, Shipping Methods and Order Notes.
  4. 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;

}

}

}

About The Author

We take the guesswork out of ecommerce.
Schedule a consultation call today