2

Here am trying to make shipment from a controller. here is my controller

public function execute()
    {
      $orderId= 11;
      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 'Errorww: ' . $exception->getMessage();
    }
protected function createShipment($order)
    {
        if ($order->canShip()) {
            $shipment = $this->shipmentFactory->create($order);
            $this->shipmentRepository->save($shipment);
            $this->shipmentNotifier->notify($shipment);
        }
    }

Here am getting error as

Errorww: The shipment couldn't be saved.

Where am doing wrong. Can i get help? Thank you in advance.

0

Try below code

// Load the order increment ID
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->loadByIncrementID('000000001');

//load by order 
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load('1');

// Check if order can be shipped or has already shipped
if (! $order->canShip()) {
    throw new \Magento\Framework\Exception\LocalizedException(
                    __('You can\'t create an shipment.')
                );
}

// 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()) {
        continue;
    }

    $qtyShipped = $orderItem->getQtyToShip();

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

    // Add shipment item to shipment
    $shipment->addItem($shipmentItem);
}

// Register shipment
$shipment->register();

$shipment->getOrder()->setIsInProcess(true);

try {
    // Save created shipment and order
    $shipment->save();
    $shipment->getOrder()->save();

    // Send email
    $this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
        ->notify($shipment);

    $shipment->save();
} catch (\Exception $e) {
    throw new \Magento\Framework\Exception\LocalizedException(
                    __($e->getMessage())
                );
}
2
  • 1
    Thank you @Rizwan Khan, this is working – Praveen Negimani Sep 17 '20 at 8:57
  • The save() method is deprecated. Is there an alternative way ? Thanks – Attila Naghi Apr 2 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.