简体   繁体   English

简单的 PHP 购物车

[英]Simple PHP Shopping Cart

I use the following PHP Session Shopping Cart (it's ready to use)我使用以下 PHP Session Shopping Cart(已准备好使用)

http://pastebin.com/vzJy6pkD http://pastebin.com/vzJy6pkD

Example of usage:用法示例:

  1. cart.php?action=add&id=1 = Adding a new product in the cart with ID 1 cart.php?action=add&id=1 = 在购物车中添加 ID 为 1 的新产品

  2. cart.php?action=delete&id=1 = Deleting a new product in the cart with ID 1 cart.php?action=delete&id=1 = 删除购物车中 ID 为 1 的新产品

  3. cart.php?action=update&id=1&qty=5 = Updating the quantity of product with ID 1 cart.php?action=update&id=1&qty=5 = 更新 ID 为 1 的产品数量

Everything is fine, but now I want to add sizes , something like: cart.php?一切都很好,但现在我想添加尺寸,例如:cart.php? action=add&id=1&size=XXXL动作=添加&id=1&size=XXXL

Please help me, I'm very confused how this can be done请帮助我,我很困惑如何做到这一点

If you want to build this up in a more scalable way, you might consider making your cart an object.如果您想以更具可扩展性的方式构建它,您可以考虑将您的购物车设为一个对象。

Then I'd turn the items into objects, with different properties.然后我将项目变成具有不同属性的对象。 That would take care of the different "product attributes" (sizes for now, but what about different colors for later? Or printable text? etc).这将处理不同的“产品属性”(现在的尺寸,但以后的不同颜色呢?或可打印的文本?等)。

If you abstract it so that each part does what it's supposed to do, independent of the other parts (orthogonal programming), you'll make your life easier now, and in the future.如果您将其抽象化,以便每个部分独立于其他部分(正交编程)执行它应该做的事情,那么您现在和将来都会使您的生活更轻松。

Since you're using PHP, I'm guessing you're also using MySQL for storage.由于您使用的是 PHP,我猜您也在使用 MySQL 进行存储。 It's typically inefficient to store objects in the $_SESSION , but more efficient to store them in the database and unpack/unserialize/wake them up when that session is in use again (ie next page load).将对象存储在$_SESSION通常效率低下,但将它们存储在数据库中并在该会话再次使用时(即下一页加载)解压缩/反序列化/唤醒它们更有效。

Those are things for down the road.这些是未来的事情。 But right now, I'd take a good look at how scalable you intend on making this, and if you're seriously going to use it in production.但是现在,我会好好看看你打算做这个的可扩展性如何,以及你是否真的打算在生产中使用它。 There's a book called 'Usable Shopping Carts' that you can get on Amazon.com (it used to be cheap! http://www.amazon.com/Usable-Shopping-Carts-Jon-Stephens/dp/1904151140 ).有一本名为“可用购物车”的书,您可以在 Amazon.com 上买到(以前很便宜! http://www.amazon.com/Usable-Shopping-Carts-Jon-Stephens/dp/1904151140 )。

There are tons of different resources for this type of thing out there, but what you're always going to come back to is how it was originally designed.这种类型的东西有很多不同的资源,但你总是会回到它最初的设计方式。 Because that's where you'll find yourself either making a hack to accomplish what you want, or implementing properly what you want.因为在那里你会发现自己要么通过 hack 来完成你想要的,要么正确地实现你想要的。

Just use this JavaScript and jquery只需使用此 JavaScript 和 jquery

 simpleCart({ checkout: { type: "PayPal" , email: "sales@dermamor.com", currency: "GBP" // set the currency to pounds sterling }, cartStyle: 'table', cartColumns: [{ attr: "name", label: "Name" }, { attr: "price", label: "Price", view: 'currency' }, { attr: "quantity" , label: "Quantity" } , { view: "remove", text: "Remove", label: false }] }); $(".btn").on('click', function(){ checkCart() }); $(function() { checkCart(); }); // simpleCart.grandTotal() //simpleCart.total(); function checkCart(){ var sum = simpleCart.quantity(); $("#dLabel").html('<span class="glyphicon glyphicon-shopping-cart"></span> Cart '+ sum +' items <span class="caret"></span>') if (simpleCart.items().length == 0) { $("#dLabel").html('<span class="glyphicon glyphicon-shopping-cart"></span> Cart Empty<span class="caret"></span>'); }else{ $("#dLabel").html('<span class="glyphicon glyphicon-shopping-cart"></span> Cart '+ sum +' items <span class="caret"></span>') } }
 #existingDiv { padding: 10px; } .simpleCart_items table{width:100%} .test{color:brown;border-left:10px solid #ccc;margin-bottom:2px} .simpleCart_items {background-color: #ffffff;} .itemRow .item-remove a{ color:#ddd; } .simpleCart_shelfItem .item_Quantity {width:50px} .headerRow{background-color:#c5c4be;color:#ffffff;padding:3px} .simpleCart_quantity, .simpleCart_total{ font-size:25px; color:#000; } .headerRow .item-name, .headerRow .item-price, .itemRow .item-name, .itemRow .item-price, .itemRow .item-remove, .headerRow .item-quantity, .itemRow .item-quantity { font-size:18px; padding:10px; color:#222222; } .menu-large { position: static !important; } .megamenu{ padding: 20px 0px; width:100%; } .megamenu> li > ul { padding: 0; margin: 0; } .megamenu> li > ul > li { list-style: none; } .megamenu> li > ul > li > a { display: block; padding: 3px 20px; clear: both; font-weight: normal; line-height: 1.428571429; color: #333333; white-space: normal; } .megamenu> li ul > li > a:hover, .megamenu> li ul > li > a:focus { text-decoration: none; color: #262626; background-color: #f5f5f5; } .megamenu.disabled > a, .megamenu.disabled > a:hover, .megamenu.disabled > a:focus { color: #999999; } .megamenu.disabled > a:hover, .megamenu.disabled > a:focus { text-decoration: none; background-color: transparent; background-image: none; filter: progid:DXImageTransform.Microsoft.gradient(enabled = false); cursor: not-allowed; } .megamenu.dropdown-header { color: #428bca; font-size: 18px; } @media (max-width: 768px) { .megamenu{ margin-left: 0 ; margin-right: 0 ; } .megamenu> li { margin-bottom: 30px; } .megamenu> li:last-child { margin-bottom: 0; } .megamenu.dropdown-header { padding: 3px 15px !important; } .navbar-nav .open .dropdown-menu .dropdown-header{ color:#fff; } }
 <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>JQuery CART Bootstrap Responsive Basket Dropdown - jsFiddle demo</title> <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script> <link rel="stylesheet" type="text/css" href="/css/normalize.css"> <link rel="stylesheet" type="text/css" href="/css/result-light.css"> <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.css"> <script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.js"></script> <script type="text/javascript" src="http://www.dermamor.com/dev/js/simpleCart.js"></script> <style type="text/css"> #existingDiv { padding: 10px; } .simpleCart_items table{width:100%} .test{color:brown;border-left:10px solid #ccc;margin-bottom:2px} .simpleCart_items {background-color: #ffffff;} .itemRow .item-remove a{ color:#ddd; } .simpleCart_shelfItem .item_Quantity {width:50px} .headerRow{background-color:#c5c4be;color:#ffffff;padding:3px} .simpleCart_quantity, .simpleCart_total{ font-size:25px; color:#000; } .headerRow .item-name, .headerRow .item-price, .itemRow .item-name, .itemRow .item-price, .itemRow .item-remove, .headerRow .item-quantity, .itemRow .item-quantity { font-size:18px; padding:10px; color:#222222; } .menu-large { position: static !important; } .megamenu{ padding: 20px 0px; width:100%; } .megamenu> li > ul { padding: 0; margin: 0; } .megamenu> li > ul > li { list-style: none; } .megamenu> li > ul > li > a { display: block; padding: 3px 20px; clear: both; font-weight: normal; line-height: 1.428571429; color: #333333; white-space: normal; } .megamenu> li ul > li > a:hover, .megamenu> li ul > li > a:focus { text-decoration: none; color: #262626; background-color: #f5f5f5; } .megamenu.disabled > a, .megamenu.disabled > a:hover, .megamenu.disabled > a:focus { color: #999999; } .megamenu.disabled > a:hover, .megamenu.disabled > a:focus { text-decoration: none; background-color: transparent; background-image: none; filter: progid:DXImageTransform.Microsoft.gradient(enabled = false); cursor: not-allowed; } .megamenu.dropdown-header { color: #428bca; font-size: 18px; } @media (max-width: 768px) { .megamenu{ margin-left: 0 ; margin-right: 0 ; } .megamenu> li { margin-bottom: 30px; } .megamenu> li:last-child { margin-bottom: 0; } .megamenu.dropdown-header { padding: 3px 15px !important; } .navbar-nav .open .dropdown-menu .dropdown-header{ color:#fff; } } </style> </head> <div class="container"> <span class="simpleCart_quantity glyphicon glyphicon-shopping-cart"></span> items - <span class="simpleCart_total"></span> <nav class="navbar navbar-default " role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".derma"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">brand</a> </div> <div class="collapse navbar-collapse derma"> <ul class="nav navbar-nav"> <!-- place dynamic links here --> <li class="dropdown menu-large"> <a href="#" class="dropdown-toggle" id="dLabel" data-toggle="dropdown"><span class="glyphicon glyphicon-shopping-cart"></span> Basket <b class="caret"></b></a> <ul class="dropdown-menu megamenu"> <div class="col-sm-12 clearfix"> <div class="simpleCart_items"></div> <ul id="cart" class='clearfix'></ul><li class="divider"></li> <div class="btn-group pull-right"> <a href="#" class="simpleCart_empty btn btn-lg btn-danger">Clear Cart</a> <a href="#" class="simpleCart_checkout btn btn-lg btn-success">Checkout Now</a> </div> </div> </ul> </li> </ul> <ul class="nav navbar-nav navbar-right"> <a class="btn btn-success btn-sm navbar-btn" style="margin-left:10px;" href="#">Sign in</a> <a class="btn btn-danger btn-sm navbar-btn" href="#">Sign up</a> <div class="btn-group "> <button class="btn btn-info navbar-btn btn-sm">En</button> <button class="btn dropdown-toggle navbar-btn btn-info btn-sm" data-toggle="dropdown"> <span class="caret"></span> </button> <ul class="dropdown-menu" style="min-width:30px;"> <!-- dropdown menu links --> <li><a href="">es</a></li> <li><a href="">en</a></li> </ul> </div> </ul> </div><!-- /.navbar-collapse --> </div> </nav> <div class="row"> <div class="col-md-3 simpleCart_shelfItem"> <div class="panel panel-default"> <div class="panel-heading item_name">Product 1</div> <div class="panel-body"> <img src="http://placehold.it/1000x1000/cecc00/" class="img-thumbnail img-responsive item_thumb"><br> <p class='input-sm clearfix'>Description of this product is rather long and so we may need to trim it by using the fantabulous JQuery... again..</p><span class='row'></span> </div> <div class="panel-footer"> <p class="item_price">$ 88</p><span class='btn btn-danger btn-md item_add'>ADD</span> <label>Qty: <input class="item_Quantity form-control" type="text" value="1"></label> <select name="size" id="size" class="item_size"> <option value="#">120</option> <option value="small"><240></240></option> <option value="medium">Medium</option> <option value="large">Large</option> </select> </div> </div> </div><!-- end object --> <div class="col-md-3 simpleCart_shelfItem"> <div class="panel panel-default"> <div class="panel-heading item_name">Product 2</div> <div class="panel-body"> <img src="http://placehold.it/1000x1000/ffdc00/" class="img-thumbnail img-responsive item_thumb"><br>Description </div> <div class="panel-footer"><p class="item_price">$ 38</p> <span class='btn btn-danger btn-md item_add'>ADD</span> <label>Qty: <input class="item_Quantity form-control" type="text" value="1"></label></div> </div> </div><!-- end object --> <div class="col-md-3 simpleCart_shelfItem"> <div class="panel panel-default"> <div class="panel-heading item_name">Product 3</div> <div class="panel-body"> <img src="http://placehold.it/1000x1000/ffcc00/" class="img-thumbnail img-responsive item_thumb"><br>Description </div> <div class="panel-footer"><p class="item_price">$ 348</p> <span class='btn btn-danger btn-md item_add'>ADD </span> <label>Qty: <input class="item_Quantity form-control" type="text" value="1"></label></div> </div> </ <!-- begin snippet: js hide: false -->

http://jsfiddle.net/yrLZd/66/ Need help just ping Krishnpal singh chouhan http://krishnpal.com/ http://jsfiddle.net/yrLZd/66/需要帮助只是 ping Krishnpal singh chouhan http://krishnpal.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM