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

问题描述 投票:0回答:1

我们与 Magento 的集成完全围绕 SoapClient 构建。例如,一个发货是这样创建的:

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

并且类似地添加跟踪。问题是,如果我出于某种原因需要 update 跟踪,我需要

shipment_increment_id
。从我们的系统中,我拉出了
order_id
。所以我需要查询 Magento 以从
shipment_increment_id
获取关联的
order_id

所以,这似乎正是我需要的解决方案,但是,我们的代码库中没有

Mage
对象,我们完全通过 SoapClient 进行通信。浏览有关销售对象的文档,我在这里并没有真正看到解决方案。

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

php magento magento-soap-api
1个回答
1
投票

使用soap的默认方法,您将无法通过订单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);
© www.soinside.com 2019 - 2024. All rights reserved.