当前位置: 首页 > 后端技术 > PHP

Laravel购物车:电商购物车包在线完美运行

时间:2023-03-30 01:49:36 PHP

我们坚持受益于开??源社区,为开源社区做贡献的原则。我们会逐步将线上正在使用的稳定包提交到github,同时也会在后续的开源产品中使用,大家可以放心使用。购物车基本上是电商场景中的必备模块。我们基于overtrue/laravel-shopping-cart进行扩展开发。BTW:github上已经有很多优秀的轮子了,但是在实际应用场景中,会出现不符合要求的情况,这个时候需要修改一下。另外,似乎又看到了超真神轮。主要overtrue轮子很好,很符合国人的习惯。主要实现如下扩展:购物车数据支持Database存储Item,增加Model属性的返回。因为购物车可能是SPU也可能是SKU,直接通过model属性返回相关对象。支持多个守卫。因为iBrand产品里面有购物车和购物车。包地址:laravel-shopping-cartInstallationcomposerrequireibrand/laravel-shopping-cart:~1.0-vvvphpartisanvendor:publish--provider="iBrand\Shoppingcart\ServiceProvider"低于Laravel5.5版本config/app.php文件中'providers'添加iBrand\Shoppingcart\ServiceProvider::classconfig/app.php文件中'aliases'添加'Cart'=>iBrand\Shoppingcart\Facade::classUsageSelectStorageYoucanchangedataStorageinconfig/ibrand/cart.phpfile.'storage'=>\iBrand\Shoppingcart\Storage\DatabaseStorage::class,'storage'=>\iBrand\Shoppingcart\Storage\SessionStorage::class,如果使用DatabaseStorage,需要执行phpartisanmigrateAdditemtocartAdda新项目。nullCart::add(string|int$id,string$name,int$quantity,int|float$price[,array$attributes=[]]);例如:$row=Cart::add(37,'Item名称',5,100.00,['颜色'=>'红色','尺寸'=>'M']);//Item://id=>37//name=>'Itemname'//qty=>5//price=>100.00//color=>'red'//size=>'M'//总计=>500.00//__raw_id=>'8a48aa7c8e5202841ddaf767bb4d10da'$rawId=$row->rawId();//获取__raw_id$row->qty;//5...Updateitem更新指定的item.ItemCart::update(string$rawId,int$quantity);ItemCart::update(string$rawId,array$arrtibutes);example:Cart::update('8a48aa7c8e5202841ddaf767bb4d10da',['name'=>'Newitemname');//oronlyupdatequantityCart::update('8a48aa7c8e5202841ddaf767bb4d10da',5);GetallitemsGetalltheitems.CollectionCart::all();example:$items=Cart::all();GetitemGetthespecifieditem.ItemCart::get(string$rawId);example:$item=Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');Removeitem通过rawID移除指定的商品.booleanCart::remove(string$rawId);例子:Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');DestroycartCleanShoppingCart.booleanCart::destroy();booleanCart::clean();//aliasofdestroy();example:Cart::destroy();//orCart::clean();READMORE:ibrandcc/laravel-shopping-cart欢迎大家star提交issue:)讨论交流