Skip to content

Instantly share code, notes, and snippets.

@fahu
Last active April 2, 2021 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahu/6a06e018803ec9c4b99ada2be5ae6dc1 to your computer and use it in GitHub Desktop.
Save fahu/6a06e018803ec9c4b99ada2be5ae6dc1 to your computer and use it in GitHub Desktop.
How to create a shipment programmatically in Magento 2
<?php
class CreateShipment extends \Magento\Backend\App\Action
{
/**
* The OrderRepository is used to load, save and delete orders.
*
* @var \Magento\Sales\Model\OrderRepository
*/
protected $orderRepository;
/**
* The ShipmentFactory is used to create a new Shipment.
*
* @var Order\ShipmentFactory
*/
protected $shipmentFactory;
/**
* The ShipmentRepository is used to load, save and delete shipments.
*
* @var Order\ShipmentRepository
*/
protected $shipmentRepository;
/**
* The ShipmentNotifier class is used to send a notification email to the customer.
*
* @var ShipmentNotifier
*/
protected $shipmentNotifier;
/**
* All specified parameters in the constructor will be injected via dependency injection.
*
* @param \Magento\Backend\App\Action\Context $context
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Sales\Model\OrderRepository $orderRepository,
\Magento\Sales\Model\Order\ShipmentRepository $shipmentRepository,
\Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory,
\Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier,
) {
parent::__construct($context);
$this->orderRepository = $orderRepository;
$this->shipmentFactory = $shipmentFactory;
$this->shipmentRepository = $shipmentRepository;
$this->shipmentNotifier = $shipmentNotifier;
}
/**
* Since our class extends the Magento Action class it *must* have an execute() function.
* This function is automatically called as soon as the request is sent.
* @return void
*/
public function execute()
{
// we are assuming that the order ID passed to this action via the request parameter.
$orderId = $this->getRequest()->getParam('order_id', 0);
if ($orderId <= 0) {
echo 'Order not found.';
return;
}
try {
// load order from database
$order = $this->orderRepository->get($orderId);
if ($order == null) {
echo "Order not loaded from database."
return;
}
$this->createShipment($order);
} catch (\Exception $exception) {
echo 'Error: ' . $exception->getMessage();
}
}
/**
* Creates a new shipment for the specified order.
*
* @param \Magento\Sales\Model\Order $order
*/
protected function createShipment($order)
{
// check if it's possible to ship the items
if ($order->canShip()) {
// create the shipment
$shipment = $this->shipmentFactory->create($order);
// save the newly created shipment
$this->shipmentRepository->save($shipment);
// send shipping confirmation e-mail to customer
$this->shipmentNotifier->notify($shipment);
}
}
}
@crashbtz
Copy link

crashbtz commented Aug 6, 2020

Hi,
This is working but, the email is not translated from the store locale which the order has been done.
From the backoffice, the email is translated but with command line, it's always send in english.

Do you have an idea how could I do that?

@atty31
Copy link

atty31 commented Apr 2, 2021

I am receiving this error message: We cannot create an empty shipment

@fahu
Copy link
Author

fahu commented Apr 2, 2021

Hi,
This is working but, the email is not translated from the store locale which the order has been done.
From the backoffice, the email is translated but with command line, it's always send in english.

Do you have an idea how could I do that?

Sorry I haven’t seen your message. I guess you solved it yourself in the meantime? :)

@fahu
Copy link
Author

fahu commented Apr 2, 2021

I am receiving this error message: We cannot create an empty shipment

Can you show me a sample of an order that you’re trying to ship?

@atty31
Copy link

atty31 commented Apr 2, 2021

hi @fahu.
What do you mean by sample of the order? I just have a normal order collection and I tried to use your code in the loop. I have nothing special with my orders. They were created manual from the backend . Thanks :)

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