• 放入购物车


    放入购物车

    <?php
    
    
    
    
    define('ACC',true);
    require('./include/init.php');
    
    
    // 设置一个动作参数,判断用户想干什么,比如是下订单/写地址/提交/清空购物车等
    $act = isset($_GET['act'])?$_GET['act']:'buy';
    
    
    
    $cart = CartTool::getCart(); // 获取购物车实例
    $goods = new GoodsModel();
    
    if($act == 'buy') { // 这是把商品加到购物车
        $goods_id = isset($_GET['goods_id'])?$_GET['goods_id']+0:0;
        $num = isset($_GET['num'])?$_GET['num']+0:1;
    
        if($goods_id) { // $goods_id为真,是想把商品放到购物车里
            $g = $goods->find($goods_id);
            if(!empty($g)) { // 有此商品
    
    
                // 需要判断此商品,是否在回收站
                // 此商品是否已下架
                if($g['is_delete'] == 1 || $g['is_on_sale'] == 0) {
                    $msg = '此商品不能购买';
                    include(ROOT . 'view/front/msg.html');
                    exit;
                }
    
                // 先把商品加到购物车
                $cart->addItem($goods_id,$g['goods_name'],$g['shop_price'],$num);
    
                // 判断库存够不够
                $items = $cart->all();
                
                if($items[$goods_id]['num'] > $g['goods_number']) {
                    // 库存不够了,把刚才加到购物车的动作撤回!
                    $cart->decNum($goods_id,$num);
                    
                    $msg = '库存不足';
                    include(ROOT . 'view/front/msg.html');
                    exit;
                }
    
            }
    
            
            //print_r($cart->all());
        }
    
        $items = $cart->all();
        
        if(empty($items)) { // 如果购物车为空,返回首页
            header('location: index.php');
            exit;
        }
    
        // 把购物车里的商品详细信息取出来
        $items = $goods->getCartGoods($items);
    
        //print_r($items);exit;
    
        $total = $cart->getPrice(); //获取购物车中的商品总价格
        $market_total = 0.0;
        foreach($items as $v) {
            $market_total += $v['market_price'] * $v['num'];
        }
    
        $discount = $market_total - $total;
        $rate = round(100 * $discount/$total,2);
    
        include(ROOT . 'view/front/jiesuan.html');
    } else if($act == 'clear') {
        $cart->clear();
        $msg = '购物车已清空';
        include(ROOT . 'view/front/msg.html');    
    } else if($act == 'tijiao') {
        
        $items = $cart->all(); // 取出购物车中的商品
    
        // 把购物车里的商品详细信息取出来
        $items = $goods->getCartGoods($items);
    
        //print_r($items);exit;
    
        $total = $cart->getPrice(); //获取购物车中的商品总价格
        $market_total = 0.0;
        foreach($items as $v) {
            $market_total += $v['market_price'] * $v['num'];
        }
    
        $discount = $market_total - $total;
        $rate = round(100 * $discount/$total,2);
    
    
        include(ROOT . 'view/front/tijiao.html'); 
    }
    
  • 相关阅读:
    new Date()的数据类型的问题
    GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format
    python安装numpy和matplotlib
    蚂蚁金服CTO程立:金融级分布式交易的技术路径
    《Python基础教程读书笔记》
    《storm实战-构建大数据实时计算读书笔记》
    Ubuntu 16.04 几个国内更新源
    vim
    我的MySQL整理
    MySql unique的实现原理简析
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4814224.html
Copyright © 2020-2023  润新知