magento 对接物流接口,查询物流信息track order ,后台物流点击 查询
一:配置config.xml
<?xml version="1.0"?>
<config>
<modules>
<More_Track>
<version>0.2.0.1</version>
</More_Track>
</modules>
<global>
<models>
<sales>
<rewrite>
<order_shipment_track>More_Track_Model_Order_Shipment_Track</order_shipment_track>
</rewrite>
</sales>
<track>
<class>More_Track_Model</class>
<resourceModel>track_resource</resourceModel>
</track>
<track_resource>
<class>More_Track_Model_Resource</class>
<deprecatedNode>track_mysql4</deprecatedNode>
<entities>
<kuaidi>
<table>kuaidi</table>
</kuaidi>
</entities>
</track_resource>
</models>
<blocks>
<track>
<class>More_Track_Block</class>
</track>
<adminhtml>
<rewrite>
<sales_order_shipment_view_tracking>More_Track_Block_Adminhtml_Sales_Order_Shipment_View_Tracking</sales_order_shipment_view_tracking>
<sales_order_shipment_create_tracking>More_Track_Block_Adminhtml_Sales_Order_Shipment_Create_Tracking</sales_order_shipment_create_tracking>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<track>
<rewrite>
<data>More_Track_Helper_Data</data>
</rewrite>
</track>
</helpers>
<resources>
<track_setup>
<setup>
<module>More_Track</module>
</setup>
</track_setup>
</resources>
<rewrite>
<shipping_tracking_pop>
<from><![CDATA[#^/shipping/tracking#]]></from>
<to>/track/index</to>
</shipping_tracking_pop>
</rewrite>
</global>
<crontab>
<jobs>
<more_track_kuaidi>
<schedule><cron_expr>*/1 * * * *</cron_expr></schedule>
<run><model>track/observer::syncTrackActive</model></run>
</more_track_kuaidi>
</jobs>
</crontab>
<frontend>
<routers>
<track>
<use>standard</use>
<args>
<module>More_Track</module>
<frontName>track</frontName>
</args>
</track>
</routers>
<layout>
<updates>
<more_track>
<file>moretrack.xml</file>
</more_track>
</updates>
</layout>
<translate>
<modules>
<More_Track>
<files>
<default>More_Track.csv</default>
</files>
</More_Track>
</modules>
</translate>
</frontend>
<default>
<shipping>
<more_track>
<track_multi>0</track_multi>
</more_track>
</shipping>
</default>
</config>
二:点击sales_order_shipment/view/页面的 tracking/popup 按钮触发track controller
<?php
require('app/code/core/Mage/Shipping/controllers/TrackingController.php');
class More_Track_IndexController extends Mage_Shipping_TrackingController{
public function callbackAction(){
if(array_key_exists('param', $_REQUEST) || Mage::getStoreConfig('shipping/more_track/track_test_data')){
if(Mage::getStoreConfig('shipping/more_track/track_test_data')){
//测试数据
$param = '{"status":"polling","billstatus":"got","message":"","lastResult":{"message":"ok","state":"0","status":"200","condition":"F00","ischeck":"0","com":"yuantong","nu":"13020111000006","data":[{"context":"上海分拨中心/装件入车扫描 ","time":"2012-08-28 16:33:19","ftime":"2012-08-28 16:33:19","status":"在途","areaCode":"310000000000","areaName":"上海市",},{"context":"上海分拨中心/下车扫描 ","time":"2012-08-27 23:22:42","ftime":"2012-08-27 23:22:42","status":"在途","areaCode":"310000000000","areaName":"上海市",}]}}';
}else{
$param = $_REQUEST['param'];
}
$formatedPushData = Zend_Json_Decoder::decode($param);
if($formatedPushData['lastResult']['nu']){
$_kuaidiData = Mage::getModel('track/kuaidi')
->getCollection()
->addFieldToFilter('carrier_number', array('eq'=>$formatedPushData['lastResult']['nu']))
->getFirstItem()->getData();
$_kuaidiData['body'] = $param;
$_kuaidiData['updated_at'] = time();
try{
Mage::getModel('track/kuaidi')->setData($_kuaidiData)->save();
$returnMessage = array(
'result' => 'true',
'returnCode' => '200',
'message' => 'success',
);
}catch(Exception $e){
$returnMessage = array(
'result' => 'false',
'returnCode' => '500',
'message' => $e->getMessage(),
);
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($returnMessage));
}
}
}
/**
* Popup action
* Shows tracking info if it's present, otherwise redirects to 404
*/
public function popupAction()
{
/* @var $shippingInfoModel Mage_Shipping_Model_Info */
$shippingInfoModel = Mage::getModel('shipping/info')->loadByHash($this->getRequest()->getParam('hash'));
$AppKey = Mage::getStoreConfig('shipping/more_track/track_app');
$_order = Mage::getModel('sales/order')->load($shippingInfoModel->getOrderId());
//foreach($_orderList as $_order){
//track
$_tracks = Mage::getModel('sales/order_shipment_track')
->getCollection()
->addFieldToFilter('order_id', $_order->getId());
//iterate tarcks and check each of them, if not exists, append a new row of data and send a request to kuaidi100 api. opposite, do nothing.
foreach($_tracks as $track){
$_track = $track->getData();
$_kuaidi = Mage::getModel('track/kuaidi')
->getCollection()
->addFieldToFilter('carrier_number', $_track['track_number']);
if(count($_kuaidi) === 0){
echo $_track['track_number'].'______';
//push a data to kuaidi100.
$post_data = array();
$post_data["schema"] = 'json' ;
$post_data["param"] = '{"company":"'.strtolower($_track['title']).'", "number":"'.$_track['track_number'].'","from":"", "to":"'.sprintf("%s%s%s", Mage::helper('track')->__($_order->getShippingAddress()->getRegion()), Mage::helper('track')->__($_order->getShippingAddress()->getCity()), Mage::helper('track')->__($_order->getShippingAddress()->getDistrict())).'", "key":"'.$AppKey.'", "parameters":{"callbackurl":"'.Mage::getUrl('track/index/callback/').'","resultv2":"1"}}';
$url='http://www.kuaidi100.com/poll';
$o="";
foreach ($post_data as $k=>$v)
{
$o.= "$k=".urlencode($v)."&"; //
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);//
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch); //
if($result){
$formatedResult = Zend_Json::decode($result);
if($result['result']){
$custom['trackdetail'] = '';
$kuaidi = Mage::getModel('track/kuaidi');//->save();
$kuaidi->setData('carrier_number', $_track['track_number']);
$kuaidi->setCreatedAt(time());
$kuaidi->setUpdatedAt(time());
$kuaidi->save();
}
}
curl_close($ch);
}
}
//}
parent::popupAction();
}
}
(责任编辑:最模板) |