需求场景:导出待发货订单,数据要求如下:
分析:数据中是主订单和子订单两张表的混合数据,不能影响原有的订单导出
步骤:
一、新建model:
- custom/systrade/model/tradeorder.php
<?php
class systrade_mdl_tradeorder{
var $defaultOrder = array('tid','DESC');
public $schema = array();
public function __construct() {
$this->app->app_id = 'systrade';
$this->schema = app::get('systrade')->model('trade')->get_schema();
}
public function _columns(){
return app::get('systrade')->model('order')->_columns();
}
public function getList( $cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null )
{
$filter['status'] = 'WAIT_SELLER_SEND_GOODS';
return app::get('systrade')->model('order')->getList( $cols, $filter, $offset, $limit, $orderType );
}
}
ps: model不要继承,否则找不到dbschema会报错
二、新建扩展类
- custom/importexport/lib/data/trade/tradeorder.php
<?php
/**
* trade.php
*
* @copyright Copyright (c) 2005-2015 ShopEx Technologies Inc. (http://www.shopex.cn)
* @license http://ecos.shopex.cn/ ShopEx License
*/
class importexport_data_trade_tradeorder {
// 设置标题
public function get_title()
{
$fields = 'oid,tid,pay_time,title,spec_nature_info,num,bn,price,payment';
$fields = explode(',', $fields);
$tmpTitle = $this->_title();
$title = array();
foreach ($fields as $val)
{
if(array_key_exists($val, $tmpTitle))
{
$title[$val] = $tmpTitle[$val];
}
}
if(!$title)
{
$title = $tmpTitle;
}
$title['status'] = app::get('importexport')->_('订单状态');
$title['total_fee'] = app::get('importexport')->_('邮费');
$title['created_time'] = app::get('importexport')->_('订单创建时间');
$title['receiver_name'] = app::get('importexport')->_('收货人');
$title['receiver_mobile'] = app::get('importexport')->_('联系电话');
$title['receiver_state'] = app::get('importexport')->_('收货人所在省');
$title['receiver_city'] = app::get('importexport')->_('收货人所在市');
$title['receiver_district'] = app::get('importexport')->_('收货人所在区');
$title['receiver_address'] = app::get('importexport')->_('收货地址');
$title['buyer_message'] = app::get('importexport')->_('买家备注');
return $title;
}
private function _title()
{
$cols = app::get('systrade')->model('order')->_columns();
$title = array();
foreach( $cols as $col => $val )
{
if( !$val['deny_export'] ){//不进行导出导入字段
if(!$val['label'] && !$val['comment'])
{
$title[$col] = $col;
}
else
{
$title[$col] = $val['label'] ? $val['label'] : $val['comment'];
}
}
}
return $title;
}
public function get_content_row($row)
{
$objMdlTrade = app::get('systrade')->model('trade');
$result = $objMdlTrade->getRow('created_time,status,total_fee,receiver_name,receiver_mobile,receiver_state,receiver_city,receiver_district,receiver_address,buyer_message', array('tid'=>$row['tid']));
$row['status'] = $result['status'];
$row['total_fee'] = $result['total_fee'];
$row['created_time'] = $result['created_time'];
$row['receiver_name'] = $result['receiver_name'];
$row['receiver_mobile'] = $result['receiver_mobile'];
$row['receiver_state'] = $result['receiver_state'];
$row['receiver_city'] = $result['receiver_city'];
$row['receiver_district'] = $result['receiver_district'];
$row['receiver_address'] = $result['receiver_address'];
$row['buyer_message'] = $result['buyer_message'];
return $row;
}
}
三、注册service
- custom/importexport/services.xml 添加一下代码
<service id="importexport.systrade_mdl_tradeorder">
<class>importexport_data_trade_tradeorder</class>
</service>
ps:services.xml中class与扩展类名一致
四、添加权限
-
custom/topshop/controller/export.php
ps:BBC商家端订单导出需要,其他地方视情况而定
开发过程中遇到一个现象奇怪的问题:数据写入文件时写不进去,原因是下面的if条件不成立,对比两者数据发现是数组的键值顺序不一样,例如:created_time在title中在第九个位置,数据中 循环是created_time在第12个位置,造成原因不明,后来在扩展类中将数据顺序调整之后导出才可以。。