Пример #1
0
                                         echo MES_Order::add_to_cart($goods, ANTI_SPAM($_REQUEST['goods_id']));
                                     } else {
                                         if ($action == 'shipping_fee_cal') {
                                             //计算配送的费用
                                             $city = ANTI_SPAM($_GET['city'], array('minValue' => 543, 'maxValue' => 573, 'type' => 'number'));
                                             $district = ANTI_SPAM($_GET['district'], array('minValue' => 0, 'maxValue' => 20, 'type' => 'number', 'empty' => true));
                                             echo MES_Order::shipping_fee_cal($city, $district);
                                         } else {
                                             if ($action == 'if_address_need_fee') {
                                                 //计算配一个地址id是否需要加收配送费
                                                 $address_id = ANTI_SPAM($_GET['address_id'], array('minLength' => 1, 'maxLength' => 12, 'type' => 'number'));
                                                 echo MES_Order::if_address_need_fee($address_id);
                                             } else {
                                                 if ($action == 'get_total_price_in_cart') {
                                                     //计算购物车里面的商品总价
                                                     echo MES_Order::get_total_price_in_cart();
                                                 } else {
                                                     header("Location: 404.html");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Пример #2
0
 public static function add_to_cart($goods, $goods_id)
 {
     global $db;
     global $ecs;
     include_once 'includes/cls_json.php';
     include_once 'includes/lib_order.php';
     $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
     if (empty($goods)) {
         $result['error'] = 1;
         return json_encode($result);
     }
     $json = new JSON();
     $goods = $json->decode($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 = $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;
             return json_encode($result);
         }
     }
     /* 更新:如果是一步购物,先清空购物车 */
     if ($_CFG['one_step_buy'] == '1') {
         clear_cart();
     }
     /* 检查:商品数量是否合法 */
     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['goods_id'] = stripslashes($goods->goods_id);
             $result['content'] = insert_right_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;
             }
         }
     }
     $sql = "select * from " . $ecs->table('cart') . " WHERE  session_id='" . SESS_ID . "'";
     $goods = $db->getAll($sql);
     foreach ($goods as $val) {
         $total += $val['goods_price'] * $val['goods_number'];
         //计算额外餐具的价格
         if ($_SESSION['extra_fork'][$val['goods_id']]) {
             $total += $_SESSION['extra_fork'][$val['goods_id']] / 2;
         }
         //蜡烛这玩意 需要在order页面返回给前端添加到订单里,其他商品不需要这么做
         if ($val['goods_id'] == 61) {
             $result['data'] = $val;
         }
     }
     $result['confirm_type'] = !empty($_CFG['cart_confirm']) ? $_CFG['cart_confirm'] : 2;
     $result['order_total'] = MES_Order::get_total_price_in_cart();
     return json_encode($result);
 }