ECSHOP "加入购物车"弹窗的实现以及"直接购买"功能

一、加入购物车的弹窗

在ECSHOP商品详情页点“加入购物车”,直接在当前页弹出一个漂亮的小窗口,效果如下图:

ECSHOP

此方法超级简单,不需要修改php程序,也不需要在ECSHOP模板页预设隐藏层,主要是修改 JS 文件


1.首先将下面四个图片下载到你网站的 /data/images/ 下面,注意,保存图片的时候不要重命名,要用我原来的名字直接保存

ECSHOP

div_bg.gif


div_close.gif


div_hs.gif


div_gwc.gif


2.以下修改以官方默认模板为例,打开 ECSHOP模板文件 /themes/default/goods.dwt

      
  • 修改为
          

  • 3.打开 /js/common.js,找到function addToCartResponse()中的

    			case '1' :
    			if (confirm(result.message)) location.href = cart_url;
    			break;
    			case '2' :
    			if (!confirm(result.message)) location.href = cart_url;
    			break;
    			case '3' :
    			location.href = cart_url;
    			break;

    修改为

    			case '1' :
    			openDiv_ecshop120(result.content);
    			break;
    			case '2' :
    			openDiv_ecshop120(result.content);
    			break;
    			case '3' :
    			openDiv_ecshop120(result.content);
    			break;

    在文件的最末尾增加如下两段代码

    function cancel_div_ecshop120(){
    	document.getElementById("gwc").removeChild(docEle('speDiv'));
    	var i=0;
    	var sel_obj= document.getElementsByTagName('select');
    	while(sel_obj[i]){
    		sel_obj[i].style.visibility="visible";
    		i++;
    	}
    }
    
    /* *
    * 点击购物后弹出提示层
    * 参数 cartinfo:购物车信息
    */
    function openDiv_ecshop120(cartinfo){
    	var _id = "speDiv";
    	var m = "mask";
    	if (docEle(_id)) document.removeChild(docEle(_id));
    	if (docEle(m)) document.removeChild(docEle(m));
    	//计算上卷元素值
    	var scrollPos;
    	if(typeof window.pageYOffset != 'undefined'){
    		scrollPos = window.pageYOffset;
    	}
    	else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
    		scrollPos = document.documentElement.scrollTop;
    	}
    	else if (typeof document.body != 'undefined'){
    		scrollPos = document.body.scrollTop;
    	}
    	var i = 0;
    	var sel_obj = document.getElementsByTagName('select');
    	while (sel_obj[i]){
    		sel_obj[i].style.visibility = "hidden";
    		i++;
    	}
    	// 新激活图层
    	var newDiv = document.createElement("div");
    	newDiv.id = _id;
    	newDiv.style.position = "absolute";
    	newDiv.style.zIndex = "10000";
    	newDiv.style.width = "289px";
    	newDiv.style.height = "120px";
    	newDiv.style.top = "-120px";
    	newDiv.style.left = "1px";
    	newDiv.style.overflow = "hidden";
    	newDiv.style.background = "#FFF";
    	newDiv.style.border = "0px solid #59B0FF";
    	newDiv.style.padding = "0px";
    	//生成层内内容
    	newDiv.innerHTML = '
    '; newDiv.innerHTML += '
    该商品已成功放入购物车
    '+cartinfo +'
    <<继续购物
    '; document.getElementById('gwc').appendChild(newDiv); }

    转载自:http://www.loveooo.com/ecshopercikaifa/851.html


    二、直接购买功能的实现

    ecshop如果设置了购买停留在商品页面,将ecshop商品加入购物车的时候就不会直接进入购物车。这样使用购物车非常不方便。我们为了既保持该页面功能,又能进入购物车功能。所以增加了另外一个购买按扭

    点购买之后。可以直接进入ecshop的购物车。这样只牵涉到修改ecshop模板,对ecshop二次开发稍微修改就可以做到。

    proQuikBuyBtn.jpg


    1.goods.dwt中,添加


    2.js/common.js,添加
    function addToCart1(goodsId, parentId)
    {
      var goods        = new Object();
      var spec_arr     = new Array();
      var fittings_arr = new Array();
      var number       = 1;
      var formBuy      = document.forms['ECS_FORMBUY'];
      var quick     = 0;
    
      // 检查是否有商品规格
      if (formBuy)
      {
        spec_arr = getSelectedAttributes(formBuy);
    
        if (formBuy.elements['number'])
        {
          number = formBuy.elements['number'].value;
        }
    
     quick = 1;
      }
    
      goods.quick    = quick;
      goods.spec     = spec_arr;
      goods.goods_id = goodsId;
      goods.number   = number;
      goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
    
      Ajax.call('flow.php?step=add_to_cart1', 'goods=' + goods.toJSONString(), addToCartResponse1, 'POST', 'JSON');
    }
    
    function addToCartResponse1(result)
    {
      if (result.error > 0)
      {
        // 如果需要缺货登记,跳转
        if (result.error == 2)
        {
          if (confirm(result.message))
          {
            location.href = 'user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec;
          }
        }
        // 没选规格,弹出属性选择框
        else if (result.error == 6)
        {
          openSpeDiv(result.message, result.goods_id, result.parent);
        }
        else
        {
          alert(result.message);
        }
      }
      else
      {
        var cartInfo = document.getElementById('ECS_CARTINFO');
        var cart_url = 'flow.php?step=cart';
        if (cartInfo)
        {
          cartInfo.innerHTML = result.content;
        }
     location.href = cart_url;
       
      }
    }

    3.flow.php中,添加
    elseif ($_REQUEST['step'] == 'add_to_cart1')
    {
        include_once('includes/cls_json.php');
        $_POST['goods'] = json_str_iconv($_POST['goods']);
    
        if (!empty($_REQUEST['goods_id']) && empty($_POST['goods']))
        {
            if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
            {
                ecs_header("Location:./\n");
            }
            $goods_id = intval($_REQUEST['goods_id']);
            exit;
        }
    
        $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
        $json  = new JSON;
    
        if (empty($_POST['goods']))
        {
            $result['error'] = 1;
            die($json->encode($result));
        }
    
        $goods = $json->decode($_POST['goods']);
    
        /* 检查:如果商品有规格,而post的数据没有规格,把商品的规格属性通过JSON传到前台 */
        if (empty($goods->spec) AND empty($goods->quick))
        {
            $sql = "SELECT a.attr_id, a.attr_name, a.attr_type, ".
                "g.goods_attr_id, g.attr_value, g.attr_price " .
            'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' .
            "WHERE a.attr_type != 0 AND g.goods_id = '" . $goods->goods_id . "' " .
            'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
    
            $res = $GLOBALS['db']->getAll($sql);
    
            if (!empty($res))
            {
                $spe_arr = array();
                foreach ($res AS $row)
                {
                    $spe_arr[$row['attr_id']]['attr_type'] = $row['attr_type'];
                    $spe_arr[$row['attr_id']]['name']     = $row['attr_name'];
                    $spe_arr[$row['attr_id']]['attr_id']     = $row['attr_id'];
                    $spe_arr[$row['attr_id']]['values'][] = array(
                                                                'label'        => $row['attr_value'],
                                                                'price'        => $row['attr_price'],
                                                                'format_price' => price_format($row['attr_price'], false),
                                                                'id'           => $row['goods_attr_id']);
                }
                $i = 0;
                $spe_array = array();
                foreach ($spe_arr AS $row)
                {
                    $spe_array[]=$row;
                }
                $result['error']   = ERR_NEED_SELECT_ATTR;
                $result['goods_id'] = $goods->goods_id;
                $result['parent'] = $goods->parent;
                $result['message'] = $spe_array;
    
                die($json->encode($result));
            }
        }
    
     
    
        /* 检查:商品数量是否合法 */
        if (!is_numeric($goods->number) || intval($goods->number) <= 0)
        {
            $result['error']   = 1;
            $result['message'] = $_LANG['invalid_number'];
        }
        /* 更新:购物车 */
        else
        {
            // 更新:添加到购物车
            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'];
                }
    
                $result['content'] = insert_cart_info();
                $result['one_step_buy'] = $_CFG['one_step_buy'];
            }
            else
            {
                $result['message']  = $err->last_message();
                $result['error']    = $err->error_no;
                $result['goods_id'] = stripslashes($goods->goods_id);
                if (is_array($goods->spec))
                {
                    $result['product_spec'] = implode(',', $goods->spec);
                }
                else
                {
                    $result['product_spec'] = $goods->spec;
                }
            }
        }
    
        $result['confirm_type'] =3;
        die($json->encode($result));
    }
    转载自: http://www.ecmoban.com/article-1351.html

    你可能感兴趣的:(ecshop)