在magento新增api方法
Arguments:
Type Name Description
string sessionId Session ID
string shipmentIncrementId
Order shipment increment ID 传入的参数是shipmentIncrementId。后面我们新增一个方法传入orderIncrementId,返回的内容是一样的。
首先来看一下salesOrderShipmentInfo这个接口在magento里是怎么样的。
3个文件
1.\app\code\core\Mage\Sales\Model\Order\Shipment\Api.php中
[php] view plain copy 在CODE上查看代码片派生到我的代码片
/**
* Retrieve shipment information
*
* @param string $shipmentIncrementId
* @return array
*/
public function info($shipmentIncrementId)
{
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncrementId);
/* @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;
}
2.\app\code\core\Mage\Sales\etc\api.xml中
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<sales_order_shipment>
<title>Shipment API</title>
<model>sales/order_shipment_api</model>
<acl>sales/order/shipment</acl>
<methods>
<list translate="title" module="sales">
<title>Retrieve list of shipments by filters</title>
<method>items</method>
<acl>sales/order/shipment/info</acl>
</list>
<info translate="title" module="sales">
<title>Retrieve shipment information</title>
<acl>sales/order/shipment/info</acl>
</info>
<info2 translate="title" module="sales">
<title>Retrieve shipment information2</title>
<acl>sales/order/shipment/info</acl>
</info2>
.....
</sales_order_shipment>
3.\app\code\core\Mage\Sales\etc\wsdl.xml中
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<message name="salesOrderShipmentInfoRequest">
<part name="sessionId" type="xsd:string" />
<part name="shipmentIncrementId" type="xsd:string" />
</message>
<message name="salesOrderShipmentInfoResponse">
<part name="result" type="typens:salesOrderShipmentEntity" />
</message>
#.......
<operation name="salesOrderShipmentInfo">
<documentation>Retrieve shipment information</documentation>
<input message="typens:salesOrderShipmentInfoRequest" />
<output message="typens:salesOrderShipmentInfoResponse" />
</operation>
#.......
<operation name="salesOrderShipmentInfo">
<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
<input>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
好了,看到上面3个文件,下面我们就新增一个salesOrderShipmentInfo2
要求传入的参数是
Arguments:
Type Name Description
string sessionId Session ID
string
orderIncrementId
orderIncrementId
第一步在api.xml文件里,紧接着info新增如下代码:
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<info2 translate="title" module="sales">
<title>Retrieve shipment information2</title>
<acl>sales/order/shipment/info</acl>
</info2>
第二部在wsdl.xml里,紧接着salesOrderShipmentInfo新增3段
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!--2016-07-27 start-->
<message name="salesOrderShipmentInfo2Request">
<part name="sessionId" type="xsd:string" />
<part name="orderIncrementId" type="xsd:string" />
</message>
<message name="salesOrderShipmentInfo2Response">
<part name="result" type="typens:salesOrderShipmentEntity" />
</message>
<!--2016-07-27 end-->
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!--2016-07-27 start-->
<operation name="salesOrderShipmentInfo2">
<documentation>Retrieve shipment information</documentation>
<input message="typens:salesOrderShipmentInfo2Request" />
<output message="typens:salesOrderShipmentInfo2Response" />
</operation>
<!--2016-07-27 end-->
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<!--2016-07-27 start-->
<operation name="salesOrderShipmentInfo2">
<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
<input>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<!--2016-07-27 end-->
然后在Api.php文件里,新增一个info2方法,接收orderIncrementId,先通过orderIncrementId获取到shipmentIncrementId。剩下代码就和info()一样了。
[php] view plain copy 在CODE上查看代码片派生到我的代码片
/**
* Retrieve shipment information2
*
* @param string $orderIncrementId
* @return array
*/
public function info2($orderIncrementId)
{
// 根据订单号先去查询$shipmentIncrementId
$orderIncrementId = (int)$orderIncrementId;
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "select increment_id from sales_flat_shipment_grid where order_increment_id=".$orderIncrementId;
$results = $read->fetchAll($sql);
if(!$results[0]['increment_id']){
$this->_fault('not_exists');
}else{
$shipmentIncrementId = $results[0]['increment_id'];
}
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncrementId);
/* @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;
}
完成之后,先刷新缓存在测试。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
$url = "http://192.168.9.12/magento/api/v2_soap/?wsdl";
$client = new SoapClient($url);
// If somestuff requires api authentification,
// then get a session token
$session = $client->login('uername', 'password');
$result = $client->salesOrderShipmentInfo2($session,100066780);//返回二维数组
echo'<pre>';
print_r($result);
浏览器访问这个地址:http://192.168.9.12/magento/api/v2_soap/?wsdl
然后ctrl+f搜索:salesOrderShipmentInfo2,发现有了,说明这个方法存在的。
(责任编辑:好模板) |