phpmagentomagento-soap-api

Getting Shipment Increment ID by Order ID in Magento via SOAP


We have an integration with Magento built entirely around the SoapClient. For example, a shipment is created like so:

$this->_client = @new SoapClient($this->getWsdl(), $context);
        if ($this->_client) {
            $this->_session = $this->_client->login($this->magentoUser, $this->magentoKey);
            return $this;
        }

...

$result = $this->_client->salesOrderShipmentCreate(
            $this->_session,
            $id
        );
return $result;

And tracking is added similarly. The problem is, if I need to update tracking for whatever reason, I need the shipment_increment_id. From our system, I pull the order_id. So I need to query Magento to get the associated shipment_increment_id from the order_id.

So, this seems to be exactly the solution I need, however, there is no Mage object in our codebase, we're communicating entirely via the SoapClient. Looking through the docs on the sales object, I'm not really seeing a solution here.

How can I get the shipment ID using the order ID via Magento's SOAP API?


Solution

  • Using default methods of soap you wouldn't be able to get shipment id by order id. For this you need to override Mage/Sales/Model/Order/Shipment/Api.php and extend the method as mentioned below.

    In app/code/local/Namespace/Modulename/etc/config.xml

    <models>
        <sales>
            <rewrite>
                <order_shipment_api>Namespace_Modulename_Model_Sales_Order_Shipment_Api</order_shipment_api>
            </rewrite>
        </sales>
    </models>
    

    Now create a method in app/code/local/Namespace/Modulename/Model/Sales/Order/Shipment/Api.php

    class Namespace_Modulename_Model_Sales_Order_Shipment_Api extends Mage_Sales_Model_Order_Shipment_Api
    {
        /**
         * Retrieve shipment information
         *
         * @param string $shipmentIncrementId
         * @return array
         */
        public function info($id, $attribute = null)
        {
            if(!empty($attribute)){
                $ids = Mage::getModel('sales/order_shipment')->getCollection()
                    ->addAttributeToFilter($attribute, $id)
                    ->getAllIds();
                if (!empty($ids)) {
                    reset($ids);
                    $shipment = Mage::getModel('sales/order_shipment')->load(current($ids));
                }
            }else{
                $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($id);
            }
    
            /* @var $shipment Mage_Sales_Model_Order_Shipment */
            if (!$shipment->getId()) {
                $this->_fault('not_exists');
            }
    
            $result = $this->_getAttributes($shipment, 'shipment');
    
            $result['items'] = array();
            foreach ($shipment->getAllItems() as $item) {
                $result['items'][] = $this->_getAttributes($item, 'shipment_item');
            }
    
            $result['tracks'] = array();
            foreach ($shipment->getAllTracks() as $track) {
                $result['tracks'][] = $this->_getAttributes($track, 'shipment_track');
            }
    
            $result['comments'] = array();
            foreach ($shipment->getCommentsCollection() as $comment) {
                $result['comments'][] = $this->_getAttributes($comment, 'shipment_comment');
            }
    
            return $result;
        }
    }
    

    Now you can call this Soap method to get shipment info (including shipment id)

    $result = $this->_client->salesOrderShipmentInfo($sessionId, $orderId, 'order_id');
    var_dump($result);