public function add_cart()
 {
     import('Think.ORG.Cart');
     // 导入分页类
     $cart = new \Common\ORG\Cart();
     $goodId = input('post.goodId', 'intval');
     //商品ID
     $quantity = input('post.quantity', 'intval');
     //购买数量
     $attr = input('post.attr');
     //商品属性
     $item = M('item')->field('id,title,img,price,goods_stock')->find($goodId);
     if (!is_array($item)) {
         $data = array('status' => 0, 'msg' => '不存在该商品', 'count' => $cart->getCnt(), 'sumPrice' => $cart->getPrice());
     } elseif ($item['goods_stock'] < $quantity) {
         $data = array('status' => 0, 'msg' => '没有足够的库存', 'count' => $cart->getCnt(), 'sumPrice' => $cart->getPrice());
     } else {
         $result = $cart->addItem($item['id'], $item['title'], $item['price'], $quantity, $item['img'], $attr);
         if ($result == 1) {
             $data = array('result' => $result, 'status' => 1, 'count' => $cart->getCnt(), 'sumPrice' => $cart->getPrice(), 'msg' => '该商品已经存在购物车');
         } else {
             $data = array('result' => $result, 'status' => 1, 'count' => $cart->getCnt(), 'sumPrice' => $cart->getPrice(), 'msg' => '商品已成功添加到购物车');
         }
     }
     //$data=array('status'=>2);
     echo json_encode($data);
 }
Example #2
0
 /**
  * 商品详细页
  */
 public function index()
 {
     $id = I('get.id', 0, 'intval');
     !$id && $this->_productNotExist();
     $itemMod = M('item');
     $item = $itemMod->where(array('id' => $id, 'status' => 1))->find();
     !$item && $this->_productNotExist();
     // 促销判断
     if ($item['is_sell'] == 1) {
         if ($item['sell_end_time'] <= time()) {
             $update = array('is_sell' => 0, 'sell_end_time' => 0, 'sell_start_time' => 0, 'sell_sold' => 0, 'sell_price' => 0.0);
             $itemMod->where(array('id' => $id))->save($update);
             $item = array_merge($item, $update);
         }
     }
     /**
      * ***品牌
      */
     $brand = M('brandlist')->field('name')->find($item['brand']);
     $item['brand'] = $brand['name'];
     //会员价格
     if ($this->visitor->info) {
         $cate_id = $this->visitor->info['id'];
         $cate = M('item_userprice')->field('user_price')->where(array('cate_id' => $cate_id, 'item_id' => $item['id']))->find();
         if ($cate) {
             $item['price'] = $cate['user_price'];
         }
     }
     //商品相册
     $img_list = M('item_img')->field('url')->where(array('item_id' => $id))->order('ordid')->select();
     //标签
     $item['tag_list'] = unserialize($item['tag_cache']);
     //可能还喜欢
     /*   $item_tag_mod = M('item_tag');
          $db_pre = C('DB_PREFIX');
          $item_tag_table = $db_pre . 'item_tag';
          $maylike_list = array_slice($item['tag_list'], 0, 3, true);
          foreach ($maylike_list as $key => $val) {
          $maylike_list[$key] = array('name' => $val);
          $maylike_list[$key]['list'] = $item_tag_mod->field('i.id,i.img,i.intro,' . $item_tag_table . '.tag_id')->where(array($item_tag_table . '.tag_id' => $key, 'i.id' => array('neq', $id)))->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->order('i.id DESC')->limit(9)->select();
          }
           */
     //第一页评论不使用AJAX利于SEO
     $item_comment_mod = M('order_comment');
     $pagesize = 8;
     $map = array('item_id' => $id);
     $count = $item_comment_mod->where($map)->count('id');
     $pager = $this->_pager($count, $pagesize);
     $pager->path = 'comment_list';
     $pager_bar = $pager->fshow();
     $cmt_list = $item_comment_mod->field('__ORDER_COMMENT__.*,__USER__.mobile')->join('__USER__ ON __USER__.id = __ORDER_COMMENT__.user_id')->where($map)->order('id DESC')->limit($pager->firstRow . ',' . $pager->listRows)->select();
     //$itemMod->where(array('id' => $id))->setInc('hits'); //点击量
     $this->assign('item', $item);
     $this->assign('img_list', $img_list);
     $this->assign('cmt_list', $cmt_list);
     $this->assign('page_bar', $pager_bar);
     $this->_config_seo(C('pin_seo_config.item'), array('item_title' => $item['title'], 'item_intro' => $item['intro'], 'item_tag' => implode(' ', (array) $item['tag_list']), 'seo_title' => $item['seo_title'], 'seo_keywords' => $item['seo_keys'], 'seo_description' => $item['seo_desc']));
     //属性
     $attr_list = M('item_attr')->where(array('item_id' => $id))->select();
     foreach ((array) $attr_list as $k => $v) {
         $attr_list[$k]['name_v'] = explode("|", $v['attr_value']);
     }
     $this->assign('attr_list', $attr_list);
     $this->assign('attr_list_count', count($attr_list));
     // user view log
     if (!empty($this->visitor->info['id'])) {
         $log = array('goods_id' => $id, 'user_id' => $this->visitor->info['id'], 'view_time' => time());
         D('item_view_log')->add($log);
     }
     import('Think.ORG.Cart');
     // 导入分页类
     $cart = new \Common\ORG\Cart();
     $num = $cart->getCnt();
     $this->assign('cartnum', $num ? $num : 0);
     $this->display();
 }