Question

I am facing an issue with creating a shipment programatically. This is my code:

    // Magento\Sales\Model\Convert\Order
    $shipment = $this->convertOrder->toShipment($order);
    foreach ($order->getAllItems() AS $orderItem) {
        if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
            continue;
        }

        $shipmentItem = $this->convertOrder->itemToShipmentItem($orderItem)->setQty($orderItem->getQtyToShip());
        $shipment->addItem($shipmentItem);
    }
    $shipment->register();
    try{
         //Magento\Sales\Api\ShipmentRepositoryInterface
         $this->shipmentRepositoryInterface->save($shipment);
    }catch (Exception $exception) {
        $this->logger->error(sprintf($exception->getMessage()))
    }

The script above, works, I got my shipment, but I still have the Ship button: enter image description here I would expect that button to be no longer available, once I created a shipment. Any idea what am I doing wrong ? with my script . Thank you

Was it helpful?

Solution 2

Eventually, I missed this part: $shipment->getOrder()->save();. By adding it , it fixed my problem!

OTHER TIPS

Just keep this script in your magento root directory and run after your store url also do not forget to replace you order id in the script

<?php
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$orderInterface = $objectManager->get('\Magento\Sales\Api\Data\OrderInterface');

//Use this if you have orderId
//$orderId = "100"; //Order Id
//$order = $orderInterface->load($orderId);

$incrementId = "000000165"; //Increment Id
$order = $objectManager->create('Magento\Sales\Model\Order')
        ->loadByAttribute('increment_id', $incrementId);

if ($order->canShip()) {
    // Initialize the order shipment object
    $convertOrder = $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();

    } catch (\Exception $e) {
       echo "Shipment Not Created". $e->getMessage(); exit;
    }

    echo "Shipment Succesfully Generated for order: #".$incrementId;
} else {
    echo "Shipment Not Created Because It's already created or something went wrong";
}

hope this helps! thankss

Try below

$shipment->register();

try{
     $shipment->getOrder()->setIsInProcess(true);
         //Magento\Sales\Api\ShipmentRepositoryInterface
         $this->shipmentRepositoryInterface->save($shipment);
    }catch (Exception $exception) {
        $this->logger->error(sprintf($exception->getMessage()))
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top