use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\DB\TransactionFactory;
use Magento\Sales\Model\Order\Email\Sender\ShipmentSender;
use Magento\Sales\Model\Order\ShipmentDocumentFactory;
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
use Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
use Magento\Framework\Registry;
DI
public function __construct(
\Psr\Log\LoggerInterface $logger,
OrderRepositoryInterface $orderRepository,
TransactionFactory $transactionFactory,
ShipmentSender $shipmentSender,
ShipmentDocumentFactory $shipmentDocumentFactory,
ShipmentItemCreationInterfaceFactory $shipmentItemCreationInterfaceFactory,
ShipmentTrackCreationInterfaceFactory $shipmentTrackCreationInterfaceFactory,
Registry $registry
) {
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->transactionFactory = $transactionFactory;
$this->shipmentSender = $shipmentSender;
$this->shipmentDocumentFactory = $shipmentDocumentFactory;
$this->shipmentItemCreationInterfaceFactory = $shipmentItemCreationInterfaceFactory;
$this->shipmentTrackCreationInterfaceFactory = $shipmentTrackCreationInterfaceFactory;
$this->registry = $registry;
}
Now:
/** @var \Magento\Sales\Model\Order $order */
$order = $this->orderRepository->get(7);
// Check if order can be shipped or has already shipped
if (! $order->canShip()) {
$this->logger->error(
"Order {$order->getIncrementId()} cannot be shipped"
);
return false;
}
$title = $order->getShippingDescription();
$carrierCode = $order->getShippingMethod();
$trackingNumber = $order->getIncrementId();
$number = 1;
foreach ($order->getAllItems() AS $orderItem) {
if ($this->registry->registry('current_shipment')) {
$this->registry->unregister('current_shipment');
}
try {
// Check if order item has qty to ship or is virtual
if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
continue;
}
$qtyShipped = $orderItem->getQtyToShip();
// Create shipment item with qty
$shipmentItem = [];
/** @var ShipmentItemCreationInterface $item */
$item = $this->shipmentItemCreationInterfaceFactory->create();
$item->setOrderItemId($orderItem->getId());
$item->setQty($qtyShipped);
$shipmentItem[] = $item;
$trackingCreation = [];
/** @var ShipmentTrackCreationInterface $trackCreation */
$trackCreation = $this->shipmentTrackCreationInterfaceFactory->create();
$trackCreation->setTrackNumber($trackingNumber.'-'.$number);
$trackCreation->setTitle($title);
$trackCreation->setCarrierCode($carrierCode);
$trackingCreation[] = $trackCreation;
$shipment = $this->shipmentDocumentFactory->create(
$order,
$shipmentItem,
$trackingCreation
);
// Register shipment
$shipment->register();
$number++;
$this->registry->register('current_shipment', $shipment);
$shipment->getOrder()->setCustomerNoteNotify(1);
$shipment->getOrder()->setIsInProcess(true);
/** @var \Magento\Framework\DB\Transaction $transaction */
$transaction = $this->transactionFactory->create();
$transaction->addObject(
$shipment
)->addObject(
$shipment->getOrder()
)->save();
$this->shipmentSender->send($shipment);
} catch (\Exception $e) {
error_log($e->getMessage());
$this->logger->error($e->getMessage());
}
}
Tested for 2.3