Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Magento 2 Programmatically Ship an Order with Tracking Details outside of Magento

Create a file called abstract.php and place it in your root Magento folder:

use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;

use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;

abstract class AbstractApp implements AppInterface
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        State $state,
        Filesystem $filesystem,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_filesystem = $filesystem;
        $this->registry = $registry;

    public function launch()
        return $this->_response;

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
        return false;

Then create a file called manualShip.php and place it in your root Magento folder.

require dirname(__FILE__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class Getapp extends AbstractApp

    public function run()

    	// Your order ID here
    	$orderIncrementId = '000000019';

            ->register('isSecureArea', true);

        $orders = $this->_objectManager->create('\Magento\Sales\Model\Order');
        $order = $orders->loadByIncrementId($orderIncrementId);

		// Initialize the order shipment object
		$convertOrder = $this->_objectManager->create('Magento\Sales\Model\Convert\Order');
		$shipment = $convertOrder->toShipment($order);

		// Loop through order items
		foreach ($order->getAllItems() AS $orderItem) {
		    // Check if order item has qty to ship or is virtual
		    if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {

		    $qtyShipped = $orderItem->getQtyToShip();

		    // Create shipment item with qty
		    $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);

		    // Add shipment item to shipment


		$track = $this->_objectManager->create('\Magento\Sales\Model\Order\Shipment\TrackFactory')->create();
        $track->setTitle('Shipment Tracking Number');


	    // Save created shipment and order




/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Getapp');

Then you can run manualShip.php on the command line from your Magento root folder like:

php manualShip.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment