ecshop2.7实现购买套餐

2009-10-16 17:26 来源:www.chinab4c.com 作者:ecshop专家

     1:

   function get_fitting_price($parent_id = '', $fitting_id = ''){
 global $db;
 global $ecs;
  $sql = 'SELECT  gg.goods_price FROM ' . $GLOBALS['ecs']->table('group_goods') . ' AS gg ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . 'AS g ON g.goods_id = gg.goods_id ' .
            "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                    "ON mp.goods_id = gg.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
            "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS ggg ON ggg.goods_id = gg.parent_id ".
            "WHERE gg.parent_id = '$parent_id' AND g.is_delete = 0 and gg.goods_id = '$fitting_id' AND g.is_on_sale = 1 ".
            "ORDER BY gg.parent_id, gg.goods_id";
  return $db -> getOne($sql);

}

if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'price')
{
    include('includes/cls_json.php');

    $json   = new JSON;
    $res    = array('err_msg' => '', 'result' => '', 'qty' => 1);

    $attr_id    = isset($_REQUEST['attr']) ? explode(',', $_REQUEST['attr']) : array();
    $number     = (isset($_REQUEST['number'])) ? intval($_REQUEST['number']) : 1;

    if ($goods_id == 0)
    {
        $res['err_msg'] = $_LANG['err_change_attr'];
        $res['err_no']  = 1;
    }
    else
    {
        if ($number == 0)
        {
            $res['qty'] = $number = 1;
        }
        else
        {
            $res['qty'] = $number;
        }
  
        $shop_price  = get_final_price($goods_id, $number, true, $attr_id);
  $string = "已选购商品数量:手机$number|配件0  总金额".price_format($shop_price * $number)."";
       // $res['result'] = price_format($shop_price * $number);
    $res['result'] =  $string;
  

    }

    die($json->encode($res));
}
if(isset($_GET['act']) && $_GET['act'] == 'buy'){
  include('includes/cls_json.php');

    $json   = new JSON;
    $res    = array('error' => '','parent'=>'','peijian'=>'','parent_total'=>'','peijian_total'=>'','price' =>'');
 //商品ID
 $parent_id = $_GET['parent_id'];
 
 //商品数量
 $count = $_GET[number];
 //配件ID
 $id = $_GET[id];
 if (!empty($_COOKIE['ECS']['bu']))
 {
  $history = explode(',', $_COOKIE['ECS']['bu']);

  array_unshift($history, $id);
  $history = array_unique($history);
  $res[result] = implode(',', $history);
  
  $res['parent'] = $count;//住机数量
  $res['peijian'] = count($history);//配件数量
  $res['price'] = get_final_price($parent_id, $count);
  foreach($history as $k=>$v){
   
    $res['price']+= get_fitting_price($parent_id,$v);
   
  }
  setcookie('ECS[bu]', implode(',', $history), gmtime() + 3600 * 24 * 30);
   
 }
 else
 { $res['parent'] = $count;//住机数量
  $res['peijian'] = 1;//配件数量
  $res['price'] = get_final_price($parent_id, $count);
  $res['price']+= get_fitting_price($parent_id,$id);
  setcookie('ECS[bu]', $id, gmtime() + 3600 * 24 * 30);
   $res[result] =$id;
 }
  

 die($json->encode($res));
}

 

if(isset($_GET['act']) && $_GET['act'] == 'cancle_buy'){
  include('includes/cls_json.php');

    $json   = new JSON;
    $res    = array('error' => '','parent'=>'','peijian'=>'','parent_total'=>'','peijian_total'=>'','price' =>'');
 //商品ID
 $parent_id = $_GET['parent_id'];
 //商品数量
 $count = $_GET[number];
 //配件ID
 $id = $_GET[id];
 
 if (!empty($_COOKIE['ECS']['bu']))
 {
  $history = explode(',', $_COOKIE['ECS']['bu']);
  if($history){
   if(in_array($id,$history)){
    foreach($history as $k=>$v){
     if($v == $id){
      unset($history[$k]);
     }
    }
    $history = array_unique($history);
   }
  }
  
  $res[result] = implode(',', $history);
  $res['parent'] = $count;//住机数量
  $res['peijian'] = count($history);//配件数量
  $res['price'] = get_final_price(3, 1);
  foreach($history as $k=>$v){
    $res['price']+= get_fitting_price($parent_id,$v);
  }
  setcookie('ECS[bu]', implode(',', $history), gmtime() + 3600 * 24 * 30);
  
 }else{
  $res['parent'] = $count;//住机数量
  $res['peijian'] = 0;//配件数量
  $res['price'] = get_final_price($parent_id, $count);
  $res[result] = '';
 }

 
 die($json->encode($res));
}
 

2:

<center><font id="ECS_GOODS_AMOUNT" class="shop"></font>&nbsp;&nbsp;&nbsp;
 <a href="javascript:addToCart({$goods.goods_id})"><img src="images/taocan.jpg"></a>

3:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- {if $fittings} -->
<div class="box">
 <div class="box_1">
  <h3><span>{$lang.accessories_releate}</span></h3>
  <div class="boxCenterList clearfix">
    <!--{foreach from=$fittings item=goods}-->
    <ul class="clearfix">
      <li class="goodsimg">
      <a href="{$goods.url}" target="_blank"><img src="{$goods.goods_thumb}" alt="{$goods.name|escape:html}" class="B_blue" /></a>
      </li>
      <li>
      <a href="{$goods.url}" target="_blank" title="{$goods.goods_name|escape:html}">{$goods.short_name|escape:html}</a><br />
      {$lang.fittings_price}<font class="f1">{$goods.fittings_price}</font><br />
      </li>
    </ul>
    <!--{/foreach}-->
  </div>
 </div>
</div>
<div class="blank5"></div>
<!-- {/if} -->


4:

 if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent))
        {
            if ($_CFG['cart_confirm'] > 2)
            {
                $result['message'] = '';
            }
            else
            {
                $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2'];
            }
   $history = explode(',', $_COOKIE['ECS']['bu']);
   if($history){
    foreach($history as $k => $v){
     addto_cart($v, 1, array(), $goods->goods_id);
    }
    unset($_COOKIE['ECS']['bu']);
    
   }
            $result['content'] = insert_cart_info();
            $result['one_step_buy'] = $_CFG['one_step_buy'];
        }

 

5:

function flow_update_cart($arr)

    foreach ($arr AS $key => $val)
    {
        $val = intval(make_semiangle($val));
        if ($val <= 0)
        {
            continue;
        }

        $sql = "SELECT parent_id, `goods_id`, `goods_attr_id`, `extension_code` FROM" .$GLOBALS['ecs']->table('cart').
               " WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
        $goods = $GLOBALS['db']->getRow($sql);

        $sql = "SELECT g.goods_name, g.goods_number ".
                "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ".
                    $GLOBALS['ecs']->table('cart'). " AS c ".
                "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'";

        $row = $GLOBALS['db']->getRow($sql);

        
        if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
        {
            if ($row['goods_number'] < $val)
            {
                show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],
                $row['goods_number'], $row['goods_number']));
                exit;
            }
        }
        elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy')
        {
            if (judge_package_stock($goods['goods_id'], $val))
            {
                show_message($GLOBALS['_LANG']['package_stock_insufficiency']);
                exit;
            }
        }

     
        $sql = "SELECT a.goods_number, a.rec_id FROM " .$GLOBALS['ecs']->table('cart') . " AS b ".
                "LEFT JOIN " . $GLOBALS['ecs']->table('cart') . " AS a ".
                    "ON a.parent_id = b.goods_id AND a.session_id = '" . SESS_ID . "' AND a.extension_code <> 'package_buy'".
                "WHERE b.rec_id = '$key'";

        $fittings = $GLOBALS['db']->getAll($sql);


        if ($val > 0)
        {
            foreach ($fittings AS $k => $v)
            {
                if ($v['goods_number'] != null && $v['rec_id'] != null)
                {
                                       $num = $val;
     

                    $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') .
                            " SET goods_number = '$num' WHERE rec_id = $v[rec_id]";
     

                    $GLOBALS['db']->query($sql);
                }
            }

            if ($goods['extension_code'] == 'package_buy')
            {
                            $sql = "UPDATE " .$GLOBALS['ecs']->table('cart').
                        " SET goods_number = '$val' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
            }
            else
            { 
    if($goods[parent_id] == 0){

     $attr_id    = empty($goods['goods_attr_id']) ? array() : explode(',', $goods['goods_attr_id']);
     $goods_price = get_final_price($goods['goods_id'], $val, true, $attr_id);
     
    
     $sql = "UPDATE " .$GLOBALS['ecs']->table('cart').
       " SET goods_number = '$val', goods_price = '$goods_price' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
    }
            }
        }
        else
        {
            if (is_object($fittings) && $fittings->goods_number != null && $fittings->rec_id != null)
            {
                $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart'). " WHERE rec_id=$fittings[rec_id]";
                $GLOBALS['db']->query($sql);
            }

            $sql = "DELETE FROM " .$GLOBALS['ecs']->table('cart').
                " WHERE rec_id='$key' AND session_id='" .SESS_ID. "'";
        }
  
        $GLOBALS['db']->query($sql);
    }

   
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" .SESS_ID. "' AND is_gift <> 0";
    $GLOBALS['db']->query($sql);
 
}

   以上思路,基本实现了ecshop2.7购物时候套餐购买功能。

  来源:中国B4C电子商务