通过SOAP在Magento中按订单ID获取货件增量ID

时间:2016-06-21 22:21:20

标签: php magento soap

我们完全围绕SoapClient建立了与Magento的集成。例如,货件的创建方式如下:

$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;

同样添加跟踪。问题是,如果我因任何原因需要更新跟踪,我需要shipment_increment_id。从我们的系统中,我拉出order_id。所以我需要查询Magento以从shipment_increment_id获取关联的order_id

所以,this seems to be exactly the solution I need但是,我们的代码库中没有Mage个对象,我们完全通过SoapClient进行通信。通过docs on the sales object,我在这里找不到解决方案。

如何通过Magento的SOAP API使用订单ID获取货件ID?

1 个答案:

答案 0 :(得分:1)

使用默认肥皂方法,您无法按订单ID获取货件ID。为此,您需要覆盖Mage / Sales / Model / Order / Shipment / Api.php并扩展下面提到的方法。

在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>

现在在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;
    }
}

现在您可以调用此Soap方法获取货件信息(包括货件ID)

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