cancel
Showing results for 
Search instead for 
Did you mean: 

Creating shipping programmatically problems

Creating shipping programmatically problems

Hey,

 

I'm using sales_order_save_after event and the idea is that when an order is going to processing get and save a tracking number.

But I'm having problems saving the tracking number, at the moment I send a request to API and get the tracking number as

$ShipmentNumber = (string) $result->NumeroEnvio;

Then I want to save it and found this that is how it is right now

// Check if order has already shipped or can be 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();
                
                $data = array(
                'carrier_code' => 'mrw',
                'title' => 'MRW',
                'number' => $ShipmentNumber, // Replace with your tracking number
                );

                try {
                    // Save created shipment and order
                    $track = $this->_objectManager->create('Magento\Sales\Model\Order\Shipment\TrackFactory')->create()->addData($data);
                    $shipment->addTrack($track)->save();
                    $shipment->save();
                    $shipment->getOrder()->save();
                } catch (\Exception $e) {
                    throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
                }

and it saves, if I go to the order there is the shipping, but when I try to open it I get

1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: shipment_id in /var/www/webroot/ROOT/vendor/magento/module-shipping/view/adminhtml/templates/view/form.phtml on line 58

I tried to check the database but I'm not familiarised with the database tables so I can't really see what the problem is.

Is the save wrong or missing something?

 

add edit:

 

btw in that file the line that gives error is

<a target="_blank" href="https://track.aftership.com/<?php echo $shipment_id; ?>?courier=mrw-spain" title="<?= /* @escapeNotVerified */ __('Track this shipment') ?>">
      <span><?= /* @escapeNotVerified */ __('Track this shipment') ?></span>
</a>

and the $shipment_id is 

$attribute_information = "Select * FROM peshipment_label WHERE order_id = ". $order->getId() ;
						// fetchOne it return the one value
						$result = $connection->fetchAll($attribute_information);
						foreach($result as $value){
							$shipment_id = $value['shipment_label_id'];
						}

and I can see that in the peshipment_label the item with the order ID is not created, but how should I create it? use sql to inject directly in the database? I don't usually like to do that but there is any other option?

 

Thanks Smiley Very Happy