即以游客身份登錄網站時以cookie的方式存儲購物車,而以登錄用戶的身份進入時將購物車信息存儲到數據庫中去,若是先以游客身份完成購物再登錄繼續購物,則將cookies購物車存入數據庫;
其中涉及到的存儲過程主要如下:
一:已登錄會員添加商品到購物車功能:
代碼如下:
/* @store_sum表示要添加的商品數量,添加同時確認購物車中自己已有的數量與將要加入的數量之和是否超過庫存 */
CREATE proc ncp_Cart_Add
(
@store_id int,
@store_sum int=1,
@member_id int
)
as
DECLARE @Amount int
DECLARE @NowAmount int
Begin
select @Amount=(select amount from ncp_store where id=@store_id)
IF EXISTS(SELECT 1 FROM [ncp_cart] WHERE store_id=@store_id and member_id=@member_id)
Begin
select @NowAmount=(select store_sum+@store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
UPDATE [ncp_cart] SET store_sum=store_sum+@store_sum,addtime=getDate() where store_id=@store_id and member_id=@member_id
return 1
End
ELSE
Begin
select @NowAmount=(select store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
INSERT INTO [ncp_cart](store_id,store_sum,member_id) values(@store_id,@store_sum,@member_id)
return 1
END
End
GO
二:購物車的刪除功能
代碼如下:
/* type 為1是全部刪 0時只刪一個 */
CREATE PROCEDURE ncp_Cart_Del
@type int=0,
@store_id int ,
@member_id int
AS
begin
if @type=0
delete from [ncp_cart] where store_id=@store_id and member_id=@member_id
else
delete from [ncp_cart] where member_id=@member_id
End
GO