Unfortunately I find myself in a bit of difficulty. The company I work for has developed a Magento 2 extension that reads data from our proprietary software on a schedule and updates Products/Customers etc on a Magento 2 site. Unfortunately, the person who wrote the extension left the business and the ownership has been put onto me to make an enhancement to this extension.

Please be kind, I have never ever programmed in PHP or used Magento 2 before, but I find myself being given only a few days to deliver an enhancement for a customer who uses this extension.

I've been asked to programatically ship an order and apply a tracking number. After doing some research online and piecing together snippets of code, I have the following half complete function:

private function insertTrackingNumber($data)
        $success = true;

        try {
            //$searchCriteria = $this->_searchCriteriaBuilder->addFilter('increment_id', $data->SalesOrderId, 'eq')->create();

            // $searchCriteria = $this->_searchCriteriaBuilder->setField('increment_id')
            // ->setValue($data->SalesOrderId)
            // ->setConditionType('eq')
            // ->create();

            $searchCriteria = $this->_searchCriteriaBuilder
            ->addFilter('increment_id', '000000001')

            $orderList = $this->_orderRepository->getList($searchCriteria)->getItems();

            // $orderList is now an array of Orders with this incrementId, which is just one order obviously

            /** @var \Magento\Sales\Model\Order $order */
            $order = $orderList[0];
            // Your logic here
            //$order = $this->_orderRepository->save($order);

            // Check if order can be shipped or has already shipped
            if (! $order->canShip()) {
                if ($this->_helper->getEnableLogging($this->_currentWebsite->getCode())) {
                    $this->_logger->warning('You cant create a shipment for this sales order.');
                return false;

            $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()) {

                $qtyShipped = $orderItem->getQtyToShip();

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

                // Add shipment item to shipment

            // Register shipment


            try {
                // Save created shipment and order

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

            } catch (\Exception $e) {
                if ($this->_helper->getEnableLogging($this->_currentWebsite->getCode())) {
                    $this->_logger->warning('Failed to save shipment: '.$e->getMessage());

        } catch (\Exception $e) {
            if ($this->_helper->getEnableLogging($this->_currentWebsite->getCode())) {
                $this->_logger->warning('Failed to create shipment: '.$e->getMessage());

        return $success;

$data is an object, with a property called SalesOrderId which should match the increment_id in Magento. However, I keep getting an error on this line:

$order = $orderList[0];

saying: Notice: Undefined offset: 0 which I presume is because the array is empty.

I have tried a few different methods of using the SearchCriteriaBuilder as you can see I have commented them out in the code. I have even resorted to hardcoding the value of the increment_id which I know for sure does exist and it still doesn't return anything.

Please could someone point me in the right direction? I apologise if there is a really simple solution for this but I am incredibly inexperienced with PHP and Magento and have been dropped in the deep end with this.

1 Answer 1


Instead of direct all of getItems() i guess that use below logic

Below code

   $orderList = $this->_orderRepository->getList($searchCriteria)->getItems();
     $order = $orderList[0];

Replace by

 $searchResult = $this->_orderRepository->getList($searchCriteria)
if ($searchResult->getTotalCount() >= 0) {
        $orderList = $searchResult->getItems();

        $order = $orderList[0]; // OR  use current($orderList);
    return ;
  • Hi Amit, thanks for your response. The issue i'm having is that the array is empty and I don't understand why. Do you know why the getList($searchCriteria)->getItems(); method i'm using is returning an empty array? Especially since i'm passing in a valid increment_id in my $searchCriteria
    – Ross
    May 22, 2020 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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