分析ecshop中颜色属性和商品规格实现原理

2009-10-14 22:53 来源:www.chinab4c.com 作者:ecshop专家

      在B4C电子商务系统中,很可能在一个系统中,出售多种商品,而且不同的商品,却有不同的规格,不同的属性。所以在ecshop中,通过三个字段来控制.

    1:ecshop商品类型

  2:ecshop属性名称

 3:ecshop属性名称对应的属性值

  首先,需要增加商品类型.增加商品类型的步骤为:后台->商品管理->商品类型->新建商品类型A

  选择商品类型A,点属性列表.输入属性.

  其次,使用属性.

  新增商品->商品属性->输入属性所对应的值.

     ecshop系统中,商品类型是存储在ecs_goods_type表里面,属性名称存储在ecs_attribute里面,商品对应属性名称的值.存储在ecs_goods_attr表里面。用三字段,来控制他的值.

  在goods.php中。你发现,以下代码来处理属性的。

   $properties = get_goods_properties($goods_id);  // 获得商品的规格和属性

        $smarty->assign('specification',       $properties['spe']);    

  而ecshop函数get_goods_properties()就是取得某个商品属性数组的。

       function get_goods_properties($goods_id)
{
    /* 对属性进行重新排序和分组 */
    $sql = "SELECT attr_group ".
            "FROM " . $GLOBALS['ecs']->table('goods_type') . " AS gt, " . $GLOBALS['ecs']->table('goods') . " AS g ".
            "WHERE g.goods_id='$goods_id' AND gt.cat_id=g.goods_type";
    $grp = $GLOBALS['db']->getOne($sql);

    if (!empty($grp))
    {
        $groups = explode("\n", strtr($grp, "\r", ''));
    }

    /* 获得商品的规格 */
    $sql = "SELECT a.attr_id, a.attr_name, a.attr_group, a.is_linked, 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 g.goods_id = '$goods_id' " .
            'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
    $res = $GLOBALS['db']->getAll($sql);

    $arr['pro'] = array();     // 属性
    $arr['spe'] = array();     // 规格
    $arr['lnk'] = array();     // 关联的属性

    foreach ($res AS $row)
    {
        if ($row['attr_type'] == 0)
        {
            $group = (isset($groups[$row['attr_group']])) ? $groups[$row['attr_group']] : $GLOBALS['_LANG']['goods_attr'];

            $arr['pro'][$group][$row['attr_id']]['name']  = $row['attr_name'];
            $arr['pro'][$group][$row['attr_id']]['value'] = $row['attr_value'];
        }
        else
        {
            $arr['spe'][$row['attr_id']]['attr_type'] = $row['attr_type'];
            $arr['spe'][$row['attr_id']]['name']     = $row['attr_name'];
            $arr['spe'][$row['attr_id']]['values'][] = array(
                                                        'label'        => $row['attr_value'],
                                                        'price'        => $row['attr_price'],
                                                        'format_price' => price_format(abs($row['attr_price']), false),
                                                        'id'           => $row['goods_attr_id']);
        }

        if ($row['is_linked'] == 1)
        {
            /* 如果该属性需要关联,先保存下来 */
            $arr['lnk'][$row['attr_id']]['name']  = $row['attr_name'];
            $arr['lnk'][$row['attr_id']]['value'] = $row['attr_value'];
        }
    }

    return $arr;
}

     相关文章:

  ecshop函数手册

  ecshop的smarty模板中foreach的应用和分析

  直接将支付宝放在详细页面进行支付

      来源:中国B4C电子商务