Magento – How to properly trigger shipment emails via the SOAP API

apishipment

The shipment functions seem to be flawed such that shipment.create can trigger an email, but the shipment template includes values that cannot be filled in because tracking numbers cannot be added until after the shipment is created. The workaround seems to be using shipment.addcomment and adjusting that template to include the track fields.

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 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(); 
        }