public function execute($cartInstance) { // 使用引用取得内部数据,方便操作 $cartContext =& $cartInstance->getCartContextRef(); if ($cartContext->isEmpty()) { // 没有数据,不需要计算 goto out_fail; } // 计算每个商品的组合选择 foreach ($cartContext->orderGoodsArray as &$orderGoodsItem) { $goodsInfo = $orderGoodsItem->goods; if (empty($goodsInfo['goods_spec'])) { continue; // 普通商品,没有组合选择 } $goodsAttr = $orderGoodsItem->getValue('goods_attr'); // 计算这个规格对应的价格 $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsInfo['goods_spec']); $goodsSpecDataArray = $goodsSpecService->getGoodsSpecDataArray($goodsAttr); // 商品的销售价格 = 基础价格 + 规格额外加价 $shopPrice = $orderGoodsItem->goods['shop_price'] + @$goodsSpecDataArray['goods_spec_add_price']; // 设置这个规格对应的价格 $orderGoodsItem->setValue('shop_price', $shopPrice); } unset($orderGoodsItem); return true; out_fail: return false; // 返回 false,后面的 pipeline 就不会被执行了 }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required('商品id不能为空')->digits('商品id非法')->min(1, true, '商品id非法')->validate('goods_id'); if (!$this->validate($validator)) { goto out_fail; } // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|' . $goods_id . '|View'; // 开启并设置 smarty 缓存时间 enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_view')); // 缓存页面 if ($smarty->isCached('goods_view.tpl', $smartyCacheId)) { goto out_display; } // 查询商品信息 $goodsBasicService = new GoodsBasicService(); $goodsInfo = $goodsBasicService->loadGoodsById($goods_id); // 商品不存在,退出 if ($goodsInfo->isEmpty() || !Utils::isTagExist(PluginHelper::SYSTEM_MOBILE, $goodsInfo['system_tag_list'])) { $this->addFlashMessage('商品 [' . $goods_id . '] 不存在'); goto out_fail; } // 取商品推广信息设置 $goodsPromote = $goodsBasicService->loadGoodsPromoteByGoodsId($goods_id); // 取商品图片集 $goodsGalleryArray = GoodsGalleryCache::getGoodsGallery($goods_id); foreach ($goodsGalleryArray as &$galleryItem) { $galleryItem['img_url'] = RouteHelper::makeImageUrl($galleryItem['img_url']); $galleryItem['thumb_url'] = RouteHelper::makeImageUrl($galleryItem['thumb_url']); } unset($galleryItem); // 设置商品页面的 SEO 信息 $smarty->assign('seo_title', $goodsInfo['seo_title'] . ',' . $f3->get('sysConfig[site_name]')); $smarty->assign('seo_description', $goodsInfo['seo_description']); $smarty->assign('seo_keywords', $goodsInfo['seo_keyword']); // 给模板赋值 $smarty->assign('goodsInfo', $goodsInfo); $smarty->assign('goodsPromote', $goodsPromote); if (!Utils::isEmpty($goodsGalleryArray)) { $smarty->assign('goodsGalleryArray', $goodsGalleryArray); } // 设置商品规格 if (!empty($goodsInfo['goods_spec'])) { $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsInfo['goods_spec']); $smarty->assign('goodsSpec', $goodsSpecService->getGoodsSpecDataArray()); } out_display: $smarty->display('goods_view.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }
/** * 更新商品的库存 * * @param int $goods_id * @param string $specStr 商品规格 * @param int $goods_number 库存 */ public function updateGoodsGoodsNumber($goods_id, $specStr, $goods_number) { // 参数验证 $validator = new Validator(array('goods_id' => $goods_id, 'specStr' => $specStr, 'goods_number' => $goods_number)); $goods_id = $validator->required()->digits()->min(1)->validate('goods_id'); $specStr = $validator->validate('specStr'); $goods_number = $validator->required()->digits()->min(1)->validate('goods_number'); $this->validate($validator); $goodsBasicService = new GoodsBasicService(); $goods = $goodsBasicService->loadGoodsById($goods_id); if ($goods->isEmpty()) { // 不存在的商品,退出 return; } // 商品有规格选择,我们需要计算规格的库存 if (!empty($goods->goods_spec)) { $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goods->goods_spec); $goodsSpecDataArray = $goodsSpecService->getGoodsSpecDataArray($specStr); if (empty($goodsSpecDataArray)) { goto no_spec_goods_calc; // 不正常的商品规格选择,按照普通购买计算库存 } // 计算剩余库存 $specGoodsNumber = @$goodsSpecDataArray['goods_number']; $specGoodsNumber -= $goods_number; $specGoodsNumber = $specGoodsNumber >= 0 ? $specGoodsNumber : 0; // 确保库存不能为负数 // 更新商品规格对应的库存 $goodsSpecService->setGoodsSpecGoodsNumber($specStr, $specGoodsNumber); $goods->goods_spec = $goodsSpecService->getJsonStr(); $goods->save(); return; } no_spec_goods_calc: // 普通商品,没有商品规格选择 $goods->goods_number -= $goods_number; // 确保库存不能是负数 $goods->goods_number = $goods->goods_number >= 0 ? $goods->goods_number : 0; $goods->save(); }
public function post($f3) { // 这里操作加入购物车 // 首先做参数合法性验证 $validator = new Validator($f3->get('POST')); $goods_id = $validator->required('商品id不能为空')->digits('商品id非法')->min(1, true, '商品id非法')->validate('goods_id'); $goodsChooseSpecListStr = $validator->validate('goods_choose_speclist'); $goodsChooseBuyCount = $validator->required('商品最少购买 1 件')->digits('商品最少购买 1 件')->min(1, true, '商品最少购买 1 件')->validate('goods_choose_buycount'); if (!$this->validate($validator)) { goto out_fail; } // 查询商品信息 $goodsBasicService = new GoodsBasicService(); $goodsInfo = $goodsBasicService->loadGoodsById($goods_id); // 商品不存在,不允许显示等 if ($goodsInfo->isEmpty() || !$goodsInfo->is_on_sale || !Utils::isTagExist(PluginHelper::SYSTEM_MOBILE, $goodsInfo['system_tag_list'])) { $this->addFlashMessage('商品不存在或者不能购买'); goto out_fail; } $cartBasicService = new CartBasicService(); // 购物车服务 $cartBasicService->loadFromStorage(); // 恢复购物车中已有的数据 $cartContext =& $cartBasicService->getCartContextRef(); // 取得 cartContext // 商品多组合选择 if (!empty($goodsChooseSpecListStr)) { // 检查商品 Spec 是否合法 $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsInfo['goods_spec']); $goodsSpecDataArray = $goodsSpecService->getGoodsSpecDataArray($goodsChooseSpecListStr); if (!$goodsSpecDataArray) { $this->addFlashMessage('商品选择[' . $goodsChooseSpecListStr . ']非法'); goto out_fail; } // 检查商品库存 if ($goodsChooseBuyCount > @$goodsSpecDataArray['goods_number']) { $this->addFlashMessage($goodsChooseSpecListStr . '库存不足,只剩 ' . @$goodsSpecDataArray['goods_number'] . ' 件'); goto out_fail; } // 商品加入到购物车 $cartContext->addGoods($goods_id, $goodsChooseBuyCount, $goodsChooseSpecListStr, @$goodsSpecDataArray['goods_sn']); } else { // 检查库存 if ($goodsChooseBuyCount > $goodsInfo['goods_number']) { $this->addFlashMessage('库存不足,剩余 ' . $goodsInfo['goods_number'] . ' 件'); goto out_fail; } // 普通简单选择,加入购物车 $cartContext->addGoods($goods_id, $goodsChooseBuyCount); } $cartBasicService->syncStorage(); // 保存购物车的数据 out: Ajax::header(); echo Ajax::buildResult(null, null, null); return; out_fail: // 失败,返回出错信息 $errorMessage = implode('|', $this->flashMessageArray); Ajax::header(); echo Ajax::buildResult(-1, $errorMessage, null); }
public function execute($cartInstance) { // 使用引用取得内部数据,方便操作 $cartContext =& $cartInstance->getCartContextRef(); if ($cartContext->isEmpty()) { // 没有数据,不需要计算 goto out_fail; } // 当前主题允许哪些系统的商品被加入到购物车 $currentThemeInstance = ThemeHelper::getCurrentSystemThemeInstance(); $goodsFilterSystemArray = $currentThemeInstance->getGoodsFilterSystemArray(); // 从购物车中删除掉的 OrderGoods $removeOrderGoods = array(); // 对每个商品做检查 foreach ($cartContext->orderGoodsArray as &$orderGoodsItem) { // 商品已经下线了,不能购买 $goodsInfo = $orderGoodsItem->goods; if (empty($goodsInfo) || !$goodsInfo['is_on_sale']) { $removeOrderGoods[] = array($goodsInfo['goods_id'], null); $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '] 已经下线'); continue; } $goodsAttr = $orderGoodsItem->getValue('goods_attr'); // 有规格的商品,用户必须做出选择 if (!empty($goodsInfo['goods_spec']) && empty($goodsAttr)) { $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '] 必须选择规格'); $removeOrderGoods[] = array($goodsInfo['goods_id'], null); continue; } // 检查商品库存 if (!empty($goodsAttr)) { // 商品有规格选择 if (empty($goodsInfo['goods_spec'])) { $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '|' . $goodsAttr . '] 选择非法'); $removeOrderGoods[] = array($goodsInfo['goods_id'], $goodsAttr); continue; } // 检查规格是否合法 $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsInfo['goods_spec']); $goodsSpecDataArray = $goodsSpecService->getGoodsSpecDataArray($goodsAttr); if (empty($goodsSpecDataArray)) { $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '|' . $goodsAttr . '] 选择非法'); $removeOrderGoods[] = array($goodsInfo['goods_id'], $goodsAttr); continue; } // 检查规格对应的库存 if ($orderGoodsItem->getValue('goods_number') > @$goodsSpecDataArray['goods_number']) { $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '|' . $goodsAttr . '] 库存不足'); $removeOrderGoods[] = array($goodsInfo['goods_id'], $goodsAttr); continue; } } else { // 简单商品,没有规格,检查库存 if ($orderGoodsItem->getValue('goods_number') > $goodsInfo['goods_number']) { $cartContext->addErrorMessage('商品 [' . $goodsInfo['goods_id'] . '] 库存不足'); $removeOrderGoods[] = array($goodsInfo['goods_id'], null); continue; } } // 商品不属于当前系统,不能购买 $goodsSystemArray = Utils::parseTagString($goodsInfo['system_tag_list']); $intersectSystem = array_intersect($goodsSystemArray, $goodsFilterSystemArray); if (empty($intersectSystem)) { $removeOrderGoods[] = array($goodsInfo['goods_id'], null); continue; } } unset($orderGoodsItem); // 删除不合格的 orderGoods 记录 foreach ($removeOrderGoods as $removeItem) { $cartContext->removeGoods($removeItem[0], $removeItem[1]); $cartContext->addErrorMessage('商品 [' . $removeItem[0] . '] 不能购买,移除'); } return true; out_fail: return false; // 返回 false,后面的 pipeline 就不会被执行了 }
public function get($f3) { // 权限检查 $this->requirePrivilege('manage_goods_search'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits()->min(0)->validate('pageNo'); $pageSize = $validator->digits()->min(0)->validate('pageSize'); // 设置缺省值 $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = isset($pageSize) && $pageSize > 0 ? $pageSize : 10; // 搜索参数数组 $searchFormQuery = array(); $searchFormQuery['g.is_on_sale'] = $validator->digits()->min(0)->filter('ValidatorIntValue')->validate('is_on_sale'); $searchFormQuery['g.goods_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('goods_id'); $searchFormQuery['g.suppliers_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('suppliers_id'); $searchFormQuery['g.goods_name'] = $validator->validate('goods_name'); $searchFormQuery['g.cat_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('cat_id'); $searchFormQuery['g.type_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('type_id'); $searchFormQuery['g.brand_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('brand_id'); $searchFormQuery['g.goods_sn'] = $validator->validate('goods_sn'); $searchFormQuery['g.warehouse'] = $validator->validate('warehouse'); $searchFormQuery['g.shelf'] = $validator->validate('shelf'); $searchFormQuery['g.admin_user_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('admin_user_id'); $systemTag = $validator->validate('system_tag'); if (!empty($systemTag)) { $searchFormQuery['g.system_tag_list'] = Utils::makeTagString(array($systemTag)); } if (!$this->validate($validator)) { goto out_display; } // 查询条件 $searchParamArray = array(); // 根据推广渠道做搜索 $goods_promote = $validator->validate('goods_promote'); if (!empty($goods_promote)) { switch ($goods_promote) { case '360tequan': $searchParamArray[] = array('gp.360tequan_price > 0'); break; case '360tegong': $searchParamArray[] = array('gp.360tegong_enable = 1'); break; default: // do nothing break; } } // 建立查询条件 $searchParamArray = array_merge($searchParamArray, QueryBuilder::buildSearchParamArray($searchFormQuery)); // 查询商品列表 $totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsPromote, $searchParamArray); if ($totalCount <= 0) { // 没商品,可以直接退出了 goto out_display; } // 页数超过最大值,返回第一页 if ($pageNo * $pageSize >= $totalCount) { RouteHelper::reRoute($this, '/Goods/Search'); } // 商品列表 $goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsPromote, 'g.goods_id, g.system_tag_list, g.cat_id, g.admin_user_name, g.goods_name, g.goods_number' . ', g.goods_spec, g.is_on_sale, g.type_id' . ', g.market_price, g.shop_price, g.shipping_fee, g.shipping_free_number' . ', g.suppliers_id, g.suppliers_price, g.suppliers_shipping_fee, g.warehouse, g.shelf', $searchParamArray, array(array('g.goods_id', 'desc')), $pageNo * $pageSize, $pageSize); // 取得供货商 id 列表,商品分类 id $supplierIdArray = array(); $categoryIdArray = array(); $typeIdArray = array(); foreach ($goodsArray as $goodsItem) { $supplierIdArray[] = $goodsItem['suppliers_id']; $categoryIdArray[] = $goodsItem['cat_id']; $typeIdArray[] = $goodsItem['type_id']; } $supplierIdArray = array_unique($supplierIdArray); $categoryIdArray = array_unique($categoryIdArray); //取得供货商信息 $userSupplierService = new UserSupplierService(); $supplierArray = $userSupplierService->fetchSupplierArrayBySupplierIdArray($supplierIdArray); // 建立 suppliers_id --> supplier 的反查表,方便快速查询 $supplierIdToSupplierArray = array(); foreach ($supplierArray as $supplier) { $supplierIdToSupplierArray[$supplier['suppliers_id']] = $supplier; } $system_url_base_array = json_decode(ManageThemePlugin::getOptionValue('system_url_base_array'), true); // 放入供货商信息 foreach ($goodsArray as &$goodsItem) { if (isset($supplierIdToSupplierArray[$goodsItem['suppliers_id']])) { // 很老的订单,用户可能被删除了 $goodsItem['suppliers_name'] = $supplierIdToSupplierArray[$goodsItem['suppliers_id']]['suppliers_name']; } // 解析 system_tag_list,放入 system_array 的信息 $systeArray = Utils::parseTagString($goodsItem['system_tag_list']); $goodsItem['system_array'] = array(); foreach ($systeArray as $systemItem) { $goodsItem['system_array'][] = @$system_url_base_array[$systemItem]['name']; } // 商品规格 if (!empty($goodsItem['goods_spec'])) { $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsItem['goods_spec']); $goodsItem['goods_spec'] = $goodsSpecService->getGoodsSpecDataArray(); } } unset($goodsItem); // 取得分类信息 $categoryBasicService = new CategoryBasicService(); $categoryArray = $categoryBasicService->fetchCategoryArrayByIdArray($categoryIdArray); // 建立 cat_id ---> cateogry 信息的反查表 $categoryIdToCategoryArray = array(); foreach ($categoryArray as $categoryItem) { $categoryIdToCategoryArray[$categoryItem['meta_id']] = $categoryItem; } // 放入分类信息 foreach ($goodsArray as &$goodsItem) { if (isset($categoryIdToCategoryArray[$goodsItem['cat_id']])) { // 很老的商品,分类信息可能已经不存在了 $goodsItem['cat_name'] = $categoryIdToCategoryArray[$goodsItem['cat_id']]['meta_name']; } } unset($goodsItem); // 取得商品类型信息 $goodsTypeService = new GoodsTypeService(); $goodsTypeArray = $goodsTypeService->fetchGoodsTypeArrayByTypeIdArray($typeIdArray); // 建立 type_id ---> type 信息的反查表 $typeIdToTypeArray = array(); foreach ($goodsTypeArray as $goodsType) { $typeIdToTypeArray[$goodsType['meta_id']] = $goodsType; } // 放入类型信息 foreach ($goodsArray as &$goodsItem) { if (isset($typeIdToTypeArray[$goodsItem['type_id']])) { $goodsItem['type_name'] = $typeIdToTypeArray[$goodsItem['type_id']]['meta_name']; } } unset($goodsItem); // 给模板赋值 $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->assign('goodsArray', $goodsArray); $smarty->assign('system_url_base_array', json_decode(ManageThemePlugin::getOptionValue('system_url_base_array'), true)); out_display: $smarty->display('goods_search.tpl'); }
public function get($f3) { global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits()->min(0)->validate('pageNo'); $pageSize = $validator->digits()->min(0)->validate('pageSize'); // 设置缺省值 $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = isset($pageSize) && $pageSize > 0 ? $pageSize : 10; // 查询条件 $searchFormQuery = array(); $searchFormQuery['g.is_on_sale'] = $validator->digits()->min(0)->filter('ValidatorIntValue')->validate('is_on_sale'); $searchFormQuery['g.goods_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('goods_id'); $searchFormQuery['g.goods_name'] = $validator->validate('goods_name'); $searchFormQuery['g.goods_sn'] = $validator->validate('goods_sn'); $searchFormQuery['g.warehouse'] = $validator->validate('warehouse'); $searchFormQuery['g.shelf'] = $validator->validate('shelf'); if (!$this->validate($validator)) { goto out_display; } // 构造查询条件 $authSupplierUser = AuthHelper::getAuthUser(); $searchFormQuery['g.suppliers_id'] = intval($authSupplierUser['suppliers_id']); // 建立查询条件 $searchParamArray = QueryBuilder::buildSearchParamArray($searchFormQuery); // 查询商品列表 $totalCount = SearchHelper::count(SearchHelper::Module_Goods, $searchParamArray); if ($totalCount <= 0) { // 没商品,可以直接退出了 goto out_display; } // 页数超过最大值,返回第一页 if ($pageNo * $pageSize >= $totalCount) { RouteHelper::reRoute($this, '/Goods/Search'); } // 商品列表 $goodsArray = SearchHelper::search(SearchHelper::Module_Goods, $this->searchFieldSelector, $searchParamArray, array(array('g.goods_id', 'desc')), $pageNo * $pageSize, $pageSize); $system_url_base_array = json_decode(ManageThemePlugin::getOptionValue('system_url_base_array'), true); foreach ($goodsArray as &$goodsItem) { // 解析 system_tag_list,放入 system_array 的信息 $systeArray = Utils::parseTagString($goodsItem['system_tag_list']); $goodsItem['system_array'] = array(); foreach ($systeArray as $systemItem) { $goodsItem['system_array'][] = @$system_url_base_array[$systemItem]['name']; } // 商品规格 if (!empty($goodsItem['goods_spec'])) { $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsItem['goods_spec']); $goodsItem['goods_spec'] = $goodsSpecService->getGoodsSpecDataArray(); } } unset($goodsItem); // 给模板赋值 $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->assign('goodsArray', $goodsArray); out_display: $smarty->display('goods_search.tpl'); }