当前位置:Gxlcms > PHP教程 > javascript-ecshop中如何在猜你喜欢中加入库存的判断,库存小于0就不显示

javascript-ecshop中如何在猜你喜欢中加入库存的判断,库存小于0就不显示

时间:2021-07-01 10:21:17 帮助过:8人阅读

我加了goods_number 但是没取出来

  1. <code> /*
  2. *获取推荐商品(猜你喜欢)
  3. */
  4. function insert_like_product($arr){
  5. $need_cache = $GLOBALS['smarty']->caching;
  6. $need_compile = $GLOBALS['smarty']->force_compile;
  7. $GLOBALS['smarty']->caching = false;
  8. $GLOBALS['smarty']->force_compile = true;
  9. if (!in_array($arr['type'], array('like')))
  10. {
  11. $GLOBALS['smarty']->assign('number', $arr['number']);
  12. return array();
  13. }
  14. static $type_goods = array();
  15. if (empty($type_goods[$arr['type']]))
  16. {
  17. //初始化数据
  18. $type_goods['like'] = array();
  19. $goods_data = array();
  20. $data = read_static_cache('like_product_'.$arr['number']);
  21. if ($data == false)
  22. {
  23. $where = db_create_in($_COOKIE['ECS']['history'], 'goods_id');
  24. $sql = 'SELECT goods_id, cat_id FROM ' . $GLOBALS['ecs']->table('goods') ." WHERE ".$where." AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0";
  25. $like_goods =$GLOBALS['db']->getAll($sql);
  26. foreach($like_goods as $val)
  27. {
  28. $like = get_category_goods('', $val['cat_id'],20,false);
  29. foreach($like as $value)
  30. {
  31. if(count($goods_data) < $arr['number']){
  32. $goods_data[] = $value;
  33. $goods_data = unique_arr($goods_data);
  34. }
  35. }
  36. }
  37. write_static_cache('like_product_'.$arr['number'], $goods_data);
  38. }
  39. else
  40. {
  41. $goods_data = $data;
  42. }
  43. }
  44. $GLOBALS['smarty']->assign('like_goods', $goods_data);
  45. $GLOBALS['smarty']->assign('type', $arr['type']);
  46. $val= $GLOBALS['smarty']->fetch('library/like_product.lbi');
  47. $GLOBALS['smarty']->caching = $need_cache;
  48. $GLOBALS['smarty']->force_compile = $need_compile;
  49. return $val;
  50. }</code>

===================================================================================

/*

  1. <code> *获取分类推荐商品
  2. */
  3. function get_category_goods($type = '', $cats = '',$num=0,$status=true, $brand = 0, $min =0, $max = 0, $ext=''){
  4. $brand_where = ($brand > 0) ? " AND g.brand_id = '$brand'" : '';
  5. $price_where = ($min > 0) ? " AND g.shop_price >= $min " : '';
  6. $price_where .= ($max > 0) ? " AND g.shop_price <= $max " : '';
  7. $sql = 'SELECT g.goods_id, g.goods_name, g.goods_number, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  8. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  9. 'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
  10. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  11. 'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
  12. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  13. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
  14. 'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' . $brand_where . $price_where . $ext;
  15. $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot', 'promote'=>'recommend_promotion');
  16. if($status)
  17. $num = get_library_number($type2lib[$type]);
  18. switch ($type)
  19. {
  20. case 'best':
  21. $sql .= ' AND is_best = 1';
  22. break;
  23. case 'new':
  24. $sql .= ' AND is_new = 1';
  25. break;
  26. case 'hot':
  27. $sql .= ' AND is_hot = 1';
  28. break;
  29. case 'promote':
  30. $time = gmtime();
  31. $sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'";
  32. break;
  33. }
  34. if (!empty($cats))
  35. {
  36. $sql .= " AND (g.cat_id=" . $cats . " OR " . get_extension_goods($cats) .")";
  37. }
  38. $order_type = $GLOBALS['_CFG']['recommend_order'];
  39. $sql .= ($order_type == 0) ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY RAND()';
  40. $res = $GLOBALS['db']->selectLimit($sql, $num);
  41. $idx = 0;
  42. $goods = array();
  43. while ($row = $GLOBALS['db']->fetchRow($res))
  44. {
  45. if ($row['promote_price'] > 0)
  46. {
  47. $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
  48. $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
  49. }
  50. else
  51. {
  52. $goods[$idx]['promote_price'] = '';
  53. }
  54. $goods[$idx]['id'] = $row['goods_id'];
  55. $goods[$idx]['name'] = $row['goods_name'];
  56. $goods[$idx]['number'] = $row['goods_number'];//库存
  57. $goods[$idx]['brief'] = $row['goods_brief'];
  58. $goods[$idx]['brand_name'] = $row['brand_name'];
  59. $goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  60. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  61. $goods[$idx]['market_price'] = price_format($row['market_price']);
  62. $goods[$idx]['shop_price'] = price_format($row['shop_price']);
  63. $goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
  64. $goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
  65. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  66. $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
  67. $idx++;
  68. }
  69. return $goods;
  70. }
  71. </code>

回复内容:

我加了goods_number 但是没取出来

  1. <code> /*
  2. *获取推荐商品(猜你喜欢)
  3. */
  4. function insert_like_product($arr){
  5. $need_cache = $GLOBALS['smarty']->caching;
  6. $need_compile = $GLOBALS['smarty']->force_compile;
  7. $GLOBALS['smarty']->caching = false;
  8. $GLOBALS['smarty']->force_compile = true;
  9. if (!in_array($arr['type'], array('like')))
  10. {
  11. $GLOBALS['smarty']->assign('number', $arr['number']);
  12. return array();
  13. }
  14. static $type_goods = array();
  15. if (empty($type_goods[$arr['type']]))
  16. {
  17. //初始化数据
  18. $type_goods['like'] = array();
  19. $goods_data = array();
  20. $data = read_static_cache('like_product_'.$arr['number']);
  21. if ($data == false)
  22. {
  23. $where = db_create_in($_COOKIE['ECS']['history'], 'goods_id');
  24. $sql = 'SELECT goods_id, cat_id FROM ' . $GLOBALS['ecs']->table('goods') ." WHERE ".$where." AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0";
  25. $like_goods =$GLOBALS['db']->getAll($sql);
  26. foreach($like_goods as $val)
  27. {
  28. $like = get_category_goods('', $val['cat_id'],20,false);
  29. foreach($like as $value)
  30. {
  31. if(count($goods_data) < $arr['number']){
  32. $goods_data[] = $value;
  33. $goods_data = unique_arr($goods_data);
  34. }
  35. }
  36. }
  37. write_static_cache('like_product_'.$arr['number'], $goods_data);
  38. }
  39. else
  40. {
  41. $goods_data = $data;
  42. }
  43. }
  44. $GLOBALS['smarty']->assign('like_goods', $goods_data);
  45. $GLOBALS['smarty']->assign('type', $arr['type']);
  46. $val= $GLOBALS['smarty']->fetch('library/like_product.lbi');
  47. $GLOBALS['smarty']->caching = $need_cache;
  48. $GLOBALS['smarty']->force_compile = $need_compile;
  49. return $val;
  50. }</code>

===================================================================================

/*

  1. <code> *获取分类推荐商品
  2. */
  3. function get_category_goods($type = '', $cats = '',$num=0,$status=true, $brand = 0, $min =0, $max = 0, $ext=''){
  4. $brand_where = ($brand > 0) ? " AND g.brand_id = '$brand'" : '';
  5. $price_where = ($min > 0) ? " AND g.shop_price >= $min " : '';
  6. $price_where .= ($max > 0) ? " AND g.shop_price <= $max " : '';
  7. $sql = 'SELECT g.goods_id, g.goods_name, g.goods_number, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  8. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  9. 'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
  10. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  11. 'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
  12. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  13. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
  14. 'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' . $brand_where . $price_where . $ext;
  15. $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot', 'promote'=>'recommend_promotion');
  16. if($status)
  17. $num = get_library_number($type2lib[$type]);
  18. switch ($type)
  19. {
  20. case 'best':
  21. $sql .= ' AND is_best = 1';
  22. break;
  23. case 'new':
  24. $sql .= ' AND is_new = 1';
  25. break;
  26. case 'hot':
  27. $sql .= ' AND is_hot = 1';
  28. break;
  29. case 'promote':
  30. $time = gmtime();
  31. $sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'";
  32. break;
  33. }
  34. if (!empty($cats))
  35. {
  36. $sql .= " AND (g.cat_id=" . $cats . " OR " . get_extension_goods($cats) .")";
  37. }
  38. $order_type = $GLOBALS['_CFG']['recommend_order'];
  39. $sql .= ($order_type == 0) ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY RAND()';
  40. $res = $GLOBALS['db']->selectLimit($sql, $num);
  41. $idx = 0;
  42. $goods = array();
  43. while ($row = $GLOBALS['db']->fetchRow($res))
  44. {
  45. if ($row['promote_price'] > 0)
  46. {
  47. $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
  48. $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
  49. }
  50. else
  51. {
  52. $goods[$idx]['promote_price'] = '';
  53. }
  54. $goods[$idx]['id'] = $row['goods_id'];
  55. $goods[$idx]['name'] = $row['goods_name'];
  56. $goods[$idx]['number'] = $row['goods_number'];//库存
  57. $goods[$idx]['brief'] = $row['goods_brief'];
  58. $goods[$idx]['brand_name'] = $row['brand_name'];
  59. $goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  60. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  61. $goods[$idx]['market_price'] = price_format($row['market_price']);
  62. $goods[$idx]['shop_price'] = price_format($row['shop_price']);
  63. $goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
  64. $goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
  65. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  66. $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
  67. $idx++;
  68. }
  69. return $goods;
  70. }
  71. </code>

你找到了对应的方法,那改了以后就打印下SQL语句调试下吧

insert_like_product函数中where条件加个goods_number>0的判断啊

人气教程排行