heesapp / product-cart
产品购物车操作
Requires
- php: >=8.1.12
This package is auto-updated.
Last update: 2024-09-21 00:54:43 UTC
README
此包非常易于使用,可以操作购物车或使用withList,在此包中移动产品,可以使用三种类型的操作
1.opreation Cart or WitchList with Product Model
2. opreation Cart or WitchList with Cart Item Collection
3. opreation Cart or Witch List with Index of Collection
首先在应用程序中添加这两行
安装
composer require heesapp/product-cart
添加服务提供者和别名
//in provider in config/app.php add this line Heesapp\Productcart\ProductCartServiceProvider::class, // in aliases within config/app.php add this line 'Cart'=> Heesapp\Productcart\Facades\Cart::class,
1 配置
1. 此包可以通过配置文件productcart.php中的Driver属性值与两个会话或数据库一起工作。通过一个命令迁移购物车表和购物车项表,并迁移哪个列表表。
1.1 生成配置文件
要生成配置文件,请使用以下命令
php artisan ProductCart:Config
1.2 驱动程序
驱动程序配置此包使用会话驱动程序或数据库驱动程序,可以通过以下命令在会话和数据库驱动程序之间切换。默认驱动程序是数据库驱动程序
1.2.1 数据库驱动程序
此命令默认驱动程序是数据库驱动程序,可以使用此命令
php artisan ProductCart:Driver
或使用以下命令
php artisan ProductCart:Driver --driver=database
如果您将使用数据库驱动程序,可以使用以下命令生成迁移文件
如果只使用购物车,请运行此命令以将项目表迁移到与哪个列表一起使用
php artisan ProductCart:CartTables
如果使用哪个列表,请运行此命令
php artisan ProductCart:WitchListTable
1.2.2 会话驱动程序
如果您以会话方式工作,则只能运行此命令
php artisan ProductCart:Driver --driver=session
1.3 配置文件的属性
1. 运费:默认值为10,设置客户的运费。默认值是0,订单金额=小计-折扣,例如
100 product each product 3$
* the subtotal = 100*3$ =300$;
* the discount = 5% of total subtotal
* net total = subtotal - subtotal*(5/100)
* net total price after discount = 300$ - 300$*(5/100) =285
*if net total larger than threshold value of shipping charges and by add the shipping charges
* if defalut value = 10 and net total > shipping charges threshold
*net total + shipping charges = total price before add tax value
- 运费阈值:运费阈值的阈值值指定最小订单以适用运费。如果订单为100,则运费值为0,超过的运费递增
3. 税率:默认值为3,设置税率
example:
* total price = 285$ if add tax = 6%
* the total price = 285 + 285 *(6/100) = 302.1$
4. 四舍五入:默认值=0.05
example
* setting round off of total price such as
* 99.95$+.05 =100$
* 99.85$ +.05 = 99.90$
2 购物车操作
此操作是
1.add product to Cart
2.move to WitchList
3.Clear Cart
4.Incremment Item quantity
5.Decremment Item quantity
6.Remove Item From Cart
7.Print Cart
8.Apply Discount
此操作使用三种类型的方法进行操作,例如
1.add Cart by Proudct Model
2.add Cart by Cart Items collection from database or session
3.add Cart by Index
2.1 使用产品模型ID操作购物车
首先将此特质添加到产品模型中
............. use Heesapp\Productcart\Traits\Cartable; ............. class Product extends Model { use Cartable ; ..... }
2.1.1 通过模型ID添加项目操作
可以通过模型的ID将项目添加到购物车中,addToCart方法接受模型的ID和数量值,默认数量值为1,此方法返回带有购物车项的数组cart
...... $product = Product::where('id', $id)->first(); //with default quantity value $cart = Product::addToCart($product->id); //with change quantity value $cart = Product::addToCart($product->id , 5); .....
2.1.2 通过模型ID删除项目操作
可以通过模型的ID从购物车中删除项目,返回删除项目后的购物车数组
..... $product = Product::where('id', $id)->first(); $cart = Product::removeFromCart($product->id); ......
2.1.3 通过模型ID增加项目数量
可以通过模型的ID增加购物车中的项目数量,返回增加后的购物车数组
..... $product = Product::where('id', $id)->first(); //with default quantity $cart = Product::IncrementQuntity($product->id); //with change quntity (increment quantity by 5) $cart = Product::IncrementQuntity($product->id , 5); ......
2.1.4 通过模型ID减少项目数量
可以通过模型的ID减少购物车中的项目数量,返回减少后的购物车数组
..... $product = Product::where('id', $id)->first(); //with default quantity $cart = Product::DecrementQuntity($product->id); //with change quntity (decrement quantity by 5) $cart = Product::DecrementQuntity($product->id ,5); ......
2.1.5 将购物车项目移动到哪个列表
如果您使用Mastik将产品添加到购物车,并在产品订单中检测到此产品,则可以通过模型ID将其删除或移动到哪个列表
..... $product = Product::where('id', $id)->first(); $cart = Product::moveToWitchList($product->id); ......
2.2 使用辅助方法操作
可以使用ProductCart()而不使用Cartable特质
helper method ProductCart()
1.add item by model only avalibale
2.remove item by model or by database collection or index
3.Increment of quantity by model or by database collection or by index
4.decrement of quntity by by model or by database collection or index
5.move to witchList by by model or by database collection or index
6.Apply discount by presantge or value
7. print cart by two mtehod
2.2.1 通过模型使用辅助方法操作
2.2.1.1 通过模型添加到购物车
.... //with default quantity $product = Product::where('id', $id)->first(); ProductCart()->addCart($product); //with quantity value $product = Product::where('id', $id)->first(); ProductCart()->addCart($product,5); ....
2.1.1.2 通过模型删除购物车
..... $product = Product::where('id', $id)->first(); ProductCart()->removeMItem($product); .....
2.1.1.3 通过模型增加购物车
.... //with defalut quantity $product = Product::where('id', $id)->first(); ProductCart()->IncrementItem($product); // with quantity value =5 $product = Product::where('id', $id)->first(); ProductCart()->IncrementItem($product,5); ....
2.1.1.4 通过模型减少购物车
.... //with defalut quantity $product = Product::where('id', $id)->first(); ProductCart()->DecrementItem($product); // with quantity value =5 $product = Product::where('id', $id)->first(); ProductCart()->DecrementItem($product,5); ....
2.1.1.5 通过模型移动到哪个列表
.... $product = Product::where('id', $id)->first(); ProductCart()->moveMToWitchList($product); ....
2.2.2 通过项目数据库集合使用辅助方法操作
2.1.2.1 通过项目数据库集合删除购物车
..... $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->removeXItem($Item); .....
2.1.2.2 通过项目数据库集合增加购物车
.... //with defalut quantity $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->IncremenXtItem($Item); // with quantity value =5 $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->IncremenXtItem($Item,5); ....
2.1.2.3 通过项目数据库集合减少购物车
.... //with defalut quantity $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->DecremenXtItem($Item); // with quantity value =5 $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->DecremenXtItem($Item,5); ....
2.1.2.4 通过项目数据库集合移动到哪个列表
.... $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->moveXToWitchList($Item); ....
2.2.3 通过项目索引使用辅助方法操作
2.1.3.1 通过项目索引删除购物车
..... $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); ProductCart()->removeXItem($Item); .....
2.1.3.2 通过项目索引增加购物车
.... // increment first item with defalut quantity $cartItems = ProductCart()->toArray(true)['CartItems']; foreach ($cartItems as $key=>$items){ return ProductCart()->IncrementQuntity($key); } //increment first with quantity value =5 $cartItems = ProductCart()->toArray(true)['CartItems']; foreach ($cartItems as $key=>$items){ return ProductCart()->IncrementQuntity($key , 5); } ....
2.1.3.3 通过项目索引减少购物车
.... //with defalut quantity $cartItems = ProductCart()->toArray(true)['CartItems']; foreach ($cartItems as $key=>$items){ return ProductCart()->DecrementQuntity($key); } // with quantity value =5 $cartItems = ProductCart()->toArray(true)['CartItems']; foreach ($cartItems as $key=>$items){ return ProductCart()->DecrementQuntity($key,5); } ....
2.1.3.4 通过项目索引移动到愿望清单
.... $cartItems = ProductCart()->toArray(true)['CartItems']; foreach ($cartItems as $key=>$items){ return ProductCart()->moveToWitchList($key); } ....
2.2.4 带有应用折扣的辅助方法
1. 通过百分比应用折扣
//discount = subtotal * discount_percentage /100 .... ProductCart()->ApplyDiscount(30); //30 % ....
2. 通过价格应用折扣 php .... ProductCart()->ApplyDiscountValue(200); //200$ ....
2.2.5 打印购物车值辅助方法
打印总价、税费、运费、净总额和税费值
ProductCart()->PrintCartData();
or
ProductCart()->data();
or
ProductCart()->toArray(); // with not print Itemes
ProductCart()->toArray(true); with print Items //
输出
with discount value
{"subtotal":"572.97","discount":"30.00","discout_percentage":0,"coupon_id":null,"shipping_charges":0,"net_total":0,"tax":"16.29",
"total":"559.26","round_off":0,"payable":"559.30"}
with discount presantage
{"subtotal":"954.95","discount":"286.49","discout_percentage":0,"coupon_id":null,"shipping_charges":0,"net_total":0,"tax":"20.05",
"total":"688.51","round_off":0,"payable":"688.60"}
清空购物车
ProductCart()->clearCart();
3 愿望清单
愿望清单与购物车相同,只是愿望清单的工作方式略有不同。
1.add product to Witch List
2.move to Cart by model or database collection or index
3.Clear witchList
6.Remove Item From Witch List by model or database collection or index
7.print items
3.1 带有模型的愿望清单
可以使用此代码
.... use Heesapp\Productcart\Traits\WitchListable; .... class Product extends Model { use WitchListable; .... }
3.1.1 通过模型添加项目到愿望清单
.... $product = Product::where('id', $id)->first(); $witchList = Product::addToWithList($product->id); ....
3.1.2 从愿望清单中移除项目
.... $product = Product::where('id', $id)->first(); $witchList = Product::removeFromWitchList($product->id); ....
3.1.3 将项目移动到购物车
.... $product = Product::where('id', $id)->first(); $witchList = Product::moveToCart($product->id); ....
3.2 使用辅助方法进行操作
使用辅助方法
ProductWitchList()
3.2.1 通过模型添加到愿望清单
.... $product = Product::where('id', $id)->first(); ProductWitchList()->addWhitcList($product); ...
3.2.2 通过模型移除愿望清单
..... $product = Product::where('id', $id)->first(); $witchList = ProductWitchList()->removeMWItem($product); .....
3.2.3 通过模型移动到购物车
.... $product = Product::where('id', $id)->first(); $witchList = ProductWitchList()->moveMToCart($product); ....
3.2.4 通过数据库集合从愿望清单中移除
..... $Item = DB::table('items_cart')->where('model_id', $product->id)->first(); $witchList = ProductWitchList()->removeXWItem($Item); ....
3.2.5 通过数据库集合移动到愿望清单
.... $cart_item = DB::table('items_cart')->where('id' , $id)->first(); $witchList = ProductWitchList()->moveXToCart($product); ....
3.2.6 通过索引从愿望清单中移除
.... $witchListItems = ProductWitchList()->data()['WitchListItems']; foreach ($witchListItems as $key=>$item){ ProductWitchList()->removeWItem($key); } ....
3.2.7 通过索引移动到购物车
.... $witchListItems = ProductWitchList()->data()['WitchListItems']; foreach ($witchListItems as $key=>$item){ ProductWitchList()->moveToCart($key); } ....
3.2.8 打印愿望清单
ProductWitchList()->data(); or ProductWitchList()->toArray();
输出
print = {"WitchListItems":[{"model_type":"App\\Product","model_id":4,"name":"Bl.dfg","price":"190.99","image":null,"quantity":"1.00","id":39},{"model_type":"App\\Product","model_id":3,"name":"sdfcdhf","price":"189.99","image":null,"quantity":"1.00","id":40},{"model_type":"App\\Product","model_id":2,"name":"dsfg","price":"1300.00","image":null,"quantity":"1.00","id":41},{"model_type":"App\\Product","model_id":1,"name":"Al.kjsa","price":"200.25","image":null,"quantity":"1.00","id":42}]}