Ecstore优惠券的使用实现如下:
优惠券使用时 调用 b2c_ctl_site_cart public function add($type='goods') 参数
array(4) {
["coupon"]=>
string(20) "Bqcm300j507005E00001"
["is_fastbuy"]=>
string(0) ""
["response_json"]=>
string(4) "true"
[0]=>
string(6) "coupon"
}
方法add 调用 b2c_cart_object_coupon 验证优惠券的有效性
public function check_object($arr_data,&$msg='')
{
if(empty($arr_data) || empty($arr_data['coupon']))
{
$msg = app::get('b2c')->_('优惠券为空!');
return false;
}
if (!$this->app->model("coupons")->verify_coupons($arr_data,$msg)){
$msg = $msg ? $msg : app::get('b2c')->_('优惠券添加失败!');
return false;
}
return true;
}
方法add 调用 b2c_cart_object_coupon 加入购物车数据
public function add_object($aData, &$msg='', $append=true)
{
$objIdent = $this->_generateIdent($aData);
$aCouponRule = $this->app->model('coupons')->getCouponByCouponCode($aData['coupon']);
$arr = $this->app->model('sales_rule_order')->getList( '*',array('rule_id'=>$aCouponRule[0]['rule_id']) );
$usedCoupon = $this->getAll();
if( !$arr || !is_array($arr) ) {
$msg = app::get('b2c')->_('优惠券信息错误!');
return false;
}
reset( $arr );
$arr = current( $arr );
if( $arr['status']!=='true' ) {
$msg = app::get('b2c')->_('该优惠券不能使用!!活动未开启!');
return false;
}
$curtime = time();
if( $curtime<$arr['from_time'] || $curtime>$arr['to_time'] ) {
$msg = app::get('b2c')->_('该优惠券不在可使用时间内,或者已过期!');
return false;
}
if($arr['stop_rules_processing'] == 'true' && count($usedCoupon) > 0 ){
$msg = app::get('b2c')->_('该优惠券无法与其他优惠券同时使用!');
return false;
}
foreach($usedCoupon as $key => $value){
$curRule = $this->app->model('sales_rule_order')->getList( '*',array('rule_id'=>$value['params']['rule_id']) );
if( !$curRule || !is_array($curRule) ) {
$msg = app::get('b2c')->_('优惠券信息错误!');
return false;
}
reset( $curRule );
$curRule = current( $curRule );
if($curRule['stop_rules_processing'] == 'true'){
$msg = app::get('b2c')->_('已添加的优惠券无法与其他优惠券同时使用!');
return false;
}
}
$aSave = array(
'obj_ident' => $objIdent,
'member_ident' => $this->member_ident,
'obj_type' => 'coupon',
'params' => array(
'name' => $aData['coupon'],
'rule_id' => $aCouponRule[0]['rule_id'],
'cpns_id' => $aCouponRule[0]['cpns_id'],
'cpns_type' => $aCouponRule[0]['cpns_type'],
'extends_params' => $aData['extends_params'],
),
'quantity' => 1, // 一张优惠券只能使用一次不能叠加
);
if(kernel::single("b2c_cart_object_goods")->get_cart_status()) {
$this->coupon_object[$aSave['obj_ident']] = $aSave;
return $aSave['obj_ident'];
//todo
}; //no database
$is_save = $this->oCartObjects->save($aSave);
if (!$is_save){
$msg = app::get('b2c')->_('优惠券使用失败!');
return false;
}
return $aSave['obj_ident'];
}
调用 b2c_mdl_cart_objects 保存车数库
public function save( &$data,$mustUpdate = null ,$mustInsert = false){
$arr_member_info = $this->get_member_info();
if( $arr_member_info['member_id'] )
$data['member_ident'] = $this->get_md5_ident( $arr_member_info['member_id'] );
if( !$data['member_ident'] ) $data['member_ident']=kernel::single('base_session')->sess_id();
if( !$data['member_ident'] || !$data['obj_type'] || !$data['obj_ident'] ) return false;
$data['member_id'] = ( $arr_member_info['member_id'] ? $arr_member_info['member_id'] : -1 );
if( empty( $data['member_id'] ) ) return false;
if( empty( $data['obj_ident']) ) return false;
if( empty( $data['params'] ) ) {
$arr = $this->getList( '*', array( 'obj_ident'=>$data['obj_ident'] ) );
$arr = $arr[0];
$data['params'] = $arr['params'];
}
$data['time'] = time();
// echo '<pre>';var_dump($data);exit;
return $this->parent_save( $data, $mustUpdate );
}