Magento – Send tracking number programmatically with SOAP

apishipmentshipment-trackingsoap

We're trying to send programatically the shipping update with the tracking number when our third party warehouse update our system.

They're using the SOAP sales_order_shipment.create and then sales_order_shipment.addTrack.

I tried a quick fix by modifying directly the API. I added sendemail to the function addtrack so it sends emails with the tracking number when it's called. I commented the email send in the createshipment but it sends the email no matters what. So we end up sending two.

I don't know how to stop the first email from sending, we just need the one with the tracking number, and also I would love to find a way to not modify directly the API to do that.

Best Answer

I had exactly the same problem and it seems that there is solution without extending the API!

The shipment API has an undocumented function called sendInfo. Thanks to [cameronhimself][1] for pointing this.

public function sendInfo($shipmentIncrementId, $comment = '')

So you need something like this (for SOAP V1)

try { 
        $orderIncrementId = "Your ORDER ID";
        $itemarray = array('3' => '3', '4' => '5'); //your order items id's and qty to ship
        $comments = "some optional comments";
        $email = FALSE; //this is important! You do NOT want to sent the shipping email (email is sent with tracking number later)
        $includeComments = TRUE; //if you want to sent the comments with the email to customer

        $shipment = array($orderIncrementId, $itemarray, $comments, $email, $includeComments);

        $nship = $proxy->call($sessionId, 'sales_order_shipment.create', $shipment); //create the shippment. $nship is the new shipment id

        $proxy->call($sessionId, 'sales_order_shipment.addTrack', array('shipmentIncrementId' => $nship, 'carrier' => 'custom', 'title' => 'CARRIER TITLE', 'trackNumber' => 'YOURTARCKINGNUMBER')); //add the tracking number to shippment

        $proxy->call($sessionId, 'sales_order_shipment.sendInfo', array('shipmentIncrementId' => $nship)); //this is the fun part. Send the email of the shippment WITH the tracking number

    } catch (Exception $e) { 
        echo 'Shipment creation failed on order '. $orderIncrementId . ': ', $e->getMessage(); 
    }