php在线购物车设计代码分享
首先,购物车数据库设计:1。id 2。商品_id商品ID 3。session_id购物车ID 4。goods_sn商品编码5。商品名称商品名称6。店铺_价格商品商城价格7。goods_price商品真实价格(与shop_price的区别在于,在打折的情况下,shop_price是打折前商品的售价,而goods_price是打折后的价格)8。9号商品。货物重量。商品属性(如颜色和尺寸)11。promote_code促销代码(商品促销的战略模式识别码)12。is_promote促销ID 12。当然,如果有其他需求,比如商品的返点,商品是否是虚拟产品,商品是否是代别人发货等。您可以添加额外的标记。这里就不举例了。第二,我们的购物车要求无需用户登录就可以将商品添加到购物车中,这也是最近非常流行的一种方式。因此,在购物车数据库的设计中,没有添加user_id字段,考虑到用户也可以在不登录的情况下将商品放入购物车。然后,为了识别购物车是哪个用户,需要session_id,这是用户对应购物车的唯一识别码。生成的代码可以放在cart对象的构造函数中:复制的代码如下:Cart类的: /** *购物方法* @param string $cart_id购物车id */public function _ _ construct(){ Zend _ session 33603360 start();$ this-_ Session=new Zend _ Session _ Namespace(' shopper cart ');if(!isset($ this-_ session-session _ id)){ $ this-_ session-session _ id=MD5(uniqid(mt _ rand(),true));$ this-_ session-info=array();} $ this-_ cart _ id=$ this-_ session-session _ id;}凡事有利有弊。这种设计的好处是不用登录就可以使用购物车;缺点是两个人共用一台电脑,购物车无法判断商品属于谁的。第三,在购物车中添加产品。在购物车中添加一个商品,在这里,我认为是两个动作。第一个动作:将商品添加到购物车数据库。第二个动作:找到购物车里的所有商品并展示出来。首先是第一个动作:复制代码如下: /** *添加商品*/公共函数good addaction(){//添加商品使用get request $ good _ id=$ this-_ getparam(' good _ id ');//商品id $ goods _ spec=$ this-_ getparam(' filter _ name ');//商品属性(颜色、尺寸)$ goods _ number=$ this-_ getparam(' goods _ number ');//商品数量$ promote _ name=$ this-_ getparam(' promote _ name ',' default ');//促销策略//获取购物车实例$ CartB=$ this-_ GetCart();$cartB-goodsAdd($goods_id,$goods_spec,$goods_number,$ promote _ name);//添加成功,跳到下一步,找到购物车中的所有商品并显示出来。$ this-_ show message(Bll _ context : iserror()?bll _ context :3360 get error():“已成功添加到购物篮!”,Bll _ context : getrecit('/order v2 '),3);}前面代码的第15行:$ cart B- good add($ good _ id,$ goods _ spec,$ goods _ number,$ promote _ name);这是添加商品的操作功能,其中$promote_name是推广的一个参数,通过策略模式选择哪种推广策略,我将在下一篇文章中讨论。只要知道这里添加这个产品的基本信息,就可以存储在购物车数据库中。然后是第二个动作:复制代码如下: /** *购物清单*/公共函数index(){//获取购物车实例$ CartB=$ this-_ GetCart();//列出购物车中的所有商品$ this-view-goods _ List=$ cart B- goodwiewlist();//获取用于显示规则消息的规则实例列表$ this-view-tips=$ cart B- good rules();//购物车中的商品总数为$ this-view-total _ number=$ cartb-gettotalgoodsnumber();//获取购物车中商品的总金额$ this-view-total _ amount=$ cart B- gettotalamount();}在这里,第一个和第二个动作必须分开,因为用户也可以直接点击购物车,而无需添加商品。以下是渲染图:
第一次发帖不清楚,请见谅。欢迎讨论!
版权声明:php在线购物车设计代码分享是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。