munna / shopping-cart
1.0.0
2021-01-14 23:07 UTC
This package is auto-updated.
Last update: 2024-09-16 05:57:57 UTC
README
Laravel 购物车
更灵活且易于使用的购物车系统,兼容 Laravel 版本 5.6、5.7、5.8、6、7 和 8。
安装购物车
接下来,运行 Composer 命令以安装最新稳定版本
composer require munna/shopping-cart
简要概述
创建类实例
munna\shopping-cart 提供两种类型的实例。您可以直接调用 Cart 类作为静态类,或者您可以创建类对象。
首先我们检查如何创建类对象实例
// Use this as namespace use Munna\ShoppingCart\Cart; // call the cart class // as a parameter we can pass the instance name. // default instance is = shopping-cart // you can use any of instances name as you like $cart = new Cart(); // Example $info = $cart->info(); // You will get all info about your default instance like as bellow return $info;
作为 JSON 返回看起来像
{ "instance": "shopping-cart", "count": 0, "shipping": 0, "discount": 0, "tax": 0, "subtotal": 0, "total": 0, "items": [] }
其次我们检查如何创建静态类实例
// Use this as namespace use Munna\ShoppingCart\Facades\Cart; // init the cart class by calling init() method // as a parameter we can pass the instance name into the init() method. // default instance is = shopping-cart // you can use any of instances name as you like $cart = Cart::init(); // Example return Cart::info(); // If you want to use this globally as static class, then just add this line // at your config/app.php into aliases array 'Cart' => Munna\ShoppingCart\Facades\Cart::class, // You will get all info about your default instance like as bellow
作为 JSON 返回看起来像
{ "instance": "shopping-cart", "count": 0, "shipping": 0, "discount": 0, "tax": 0, "subtotal": 0, "total": 0, "items": [] }
如何使用实例
// default instance = shoppint-cart // 1st Example $cart = new Cart('whishlist'); // Example return $cart->info(); // 2nd Example Cart::init('whishlist'); // Example $cart = Cart::info(); return $cart;
添加
Cart::add() 或 $cart->add()
// You must maintaince these parameter value // Require Fields $product_id = "You product Id", // Required $product_name = "Product Name", // Required $product_qty = "Product Quantity", // Required $product_price = "Product Price", // Required // Optional Fields $product_weight = 0, // Optional $product_thumb = null, // Optional $discount = 0, // Optional $shipping_charge = 0, // Optional $tax = 0, // Optional $product_info = [];// Optional // 1st example $cart = new Cart(); $cart->add($product_id, $product_name, $product_qty, $product_price, $product_weight = 0, $product_thumb = null, $discount = 0, $shipping_charge = 0, $tax = 0, $product_info = []); // 2nd example Cart::add($product_id, $product_name, $product_qty, $product_price, $product_weight = 0, $product_thumb = null, $discount = 0, $shipping_charge = 0, $tax = 0, $product_info = []);
成功后,您将获得以下返回结果
{ "status": true, "message": "Product Has Been Added To Shopping Cart", "instance": "shopping-cart", "uid": "82qdiieeqvl0wftwyv7b1cfhidi4ry6k" }
更新
Cart::update() 或 $cart->update()
// You must maintaince these parameter value // Require Fields $uid = "You uid Id", // Required // Like as something 82qdiieeqvl0wftwyv7b1cfhidi4ry6k $quantity = 3; // Required // 1st example $cart = new Cart(); $cart->update($uid, $quantity); // 2nd example Cart::update($uid, $quantity);
成功后,您将获得以下返回结果
{ "status": true, "message": "Product Has Been Updated", "instance": "shopping-cart", "uid": "82qdiieeqvl0wftwyv7b1cfhidi4ry6k" }
删除
Cart::remove() 或 $cart->remove()
// You must maintaince these parameter value // Require Fields $uid = "You uid Id", // Required // Like as something m1ueddkrrayhkwi4prtjvxfyoytjmxpz // 1st example $cart = new Cart(); $cart->remove($uid); // 2nd example Cart::remove($uid);
成功后,您将获得以下返回结果
{ "status": true, "message": "Product Has Been Removed", "instance": "shopping-cart", "uid": "82qdiieeqvl0wftwyv7b1cfhidi4ry6k" }
搜索
Cart::search() 或 $cart->search() 或 Cart::get() 或 $cart->get()
// You must maintaince these parameter value // Require Fields $uid = "You uid Id", // Required // Like as something m1ueddkrrayhkwi4prtjvxfyoytjmxpz // 1st example $cart = new Cart(); $cart->search($uid); $cart->get($uid); // 2nd example Cart::search($uid); Cart::get($uid);
成功后,您将获得以下返回结果
{ "status": true, "message": "Product Item Found", "instance": "shopping-cart", "uid": "dsnufjalsd6cgohogi2aw3dyljb3y3kf", "product": 6, "name": "Product 6", "price": 80.5, "qty": 1, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "80.50", "total": "80.50", "created_at": "2021-01-14T22:44:33.411759Z", "updated_at": null }
清除
Cart::clear() 或 $cart->clear()
// 1st example $cart = new Cart(); $cart->clear(); // 2nd example Cart::clear();
成功后,您将获得以下返回结果
{ "status": true, "message": "Cart items has been cleared" }
计算
总计
Cart::total() 或 $cart->total()
// 1st example $cart = new Cart(); $cart->total(); // 2nd example Cart::total();
小计
Cart::subtotal() 或 $cart->subtotal()
// 1st example $cart = new Cart(); $cart->subtotal(); // 2nd example Cart::subtotal();
折扣
Cart::discount() 或 $cart->discount()
// 1st example $cart = new Cart(); $cart->discount(); // 2nd example Cart::discount();
运费
Cart::shipping() 或 $cart->shipping()
// 1st example $cart = new Cart(); $cart->shipping(); // 2nd example Cart::shipping();
税费
Cart::tax() 或 $cart->tax()
// 1st example $cart = new Cart(); $cart->tax(); // 2nd example Cart::tax();
数量
Cart::count() 或 $cart->count()
// 1st example $cart = new Cart(); $cart->count(); // 2nd example Cart::count();
商品
Cart::items() 或 $cart->items()
// 1st example $cart = new Cart(); $cart->items(); // 2nd example Cart::items(); // items() methos support a parameter that can able to sorted your cart item by ascending // You can sort your item by price, total, subtotal, product name or any key that exists. // Example Cart::items('price'); // or Cart::items('name'); // or Cart::items('qty'); // or Cart::items('total') //etc
看起来像
[ { "uid": "h6zc3duk5cqu69y5tcof0u01iwx47tyy", "product": 6, "name": "Product 6", "price": 80.5, "qty": 1, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "80.50", "total": "80.50", "created_at": "2021-01-14T23:00:14.590324Z", "updated_at": null }, { "uid": "iafu81ochafwyeehpkviy5s7dwdsogbf", "product": 1, "name": "Product 1", "price": 80.5, "qty": 1, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "80.50", "total": "80.50", "created_at": "2021-01-14T23:00:23.505786Z", "updated_at": null }, { "uid": "hrkzcpyxxkjup4hxz1td86yhhbyyq71g", "product": 2, "name": "Product 2", "price": 100, "qty": 3, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "300.00", "total": "300.00", "created_at": "2021-01-14T23:00:34.813746Z", "updated_at": null } ]
信息
Cart::info() 或 $cart->info()
提供所有查询,如 total()、subtotal()、tax()、discount()、count() 等
// 1st example $cart = new Cart(); $cart->info(); // 2nd example Cart::info(); // info() methos support a parameter that can able to sorted your cart item by ascending // You can sort your item by price, total, subtotal, product name or any key that exists. // Example Cart::info('price'); // or Cart::info('name'); // or Cart::info('qty'); // or Cart::info('total') //etc
例如
{ "instance": "shopping-cart", "count": 3, "shipping": "0.00", "discount": "0.00", "tax": "0.00", "subtotal": "461.00", "total": "461.00", "items": [ { "uid": "h6zc3duk5cqu69y5tcof0u01iwx47tyy", "product": 6, "name": "Product 6", "price": 80.5, "qty": 1, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "80.50", "total": "80.50", "created_at": "2021-01-14T23:00:14.590324Z", "updated_at": null }, { "uid": "iafu81ochafwyeehpkviy5s7dwdsogbf", "product": 1, "name": "Product 1", "price": 80.5, "qty": 1, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "80.50", "total": "80.50", "created_at": "2021-01-14T23:00:23.505786Z", "updated_at": null }, { "uid": "hrkzcpyxxkjup4hxz1td86yhhbyyq71g", "product": 2, "name": "Product 2", "price": 100, "qty": 3, "weight": 0, "discount": 0, "tax": 0, "shipping": 0, "thumb": null, "options": [], "subtotal": "300.00", "total": "300.00", "created_at": "2021-01-14T23:00:34.813746Z", "updated_at": null } ] }
视频
许可
此软件包是开源的,并使用MIT 许可证。非常感谢。如果您喜欢它,请给它加星标,并给我提出更好的建议。