1

I am using 'Dirty Playground' for external retrieve/update Magento products & order information. It's working fine.
Now, I am trying to update shipment from the same concept, but not able to achieve. It is showing a blank page.
I had tried updating shipment by programmatic, it's also working fine in Magento frontend.

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../shoppersbay/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

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

    // Load the order static ID for testing 
    $order = $obj->create('Magento\Sales\Model\Order')
        ->loadByAttribute('increment_id', '000000040');
// 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 = $obj->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
        $obj->create('Magento\Shipping\Model\ShipmentNotifier')
            ->notify($shipment);

        $shipment->save();
    } catch (\Exception $e) {
        throw new \Magento\Framework\Exception\LocalizedException(
                        __($e->getMessage())
                    );
    }

How can I achieve this? Am I missing something in the code?

Thanks in Advance.

1

Finally, We achieve this by below changes. I am using for getting order details. As per "dirty playground" ObjectManager has been already created, so we can't create again.

$order = $obj->create('Magento\Sales\Model\Order') ->loadByAttribute('increment_id', '000000040');

$order = $obj->get('Magento\Sales\Model\Order')
                ->load('38');

I replace all $obj->get with $obj->create, now it's working fine.

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.