lutforrahman / nujhatcart
Nujhatcart The Laravel 购物车
5.4.1
2017-07-22 18:14 UTC
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
README
Nujhatcart The Laravel 购物车
一个简单的 Laravel >=5.4 购物车实现
安装
Laravel 5.4
通过 Composer 安装包。在你的项目目录中打开终端,输入
composer require lutforrahman/nujhatcart
按回车键,包开始下载...
或者
编辑你的项目中的 composer.json
文件,并添加
"require": { "laravel/framework": "5.4.*", "lutforrahman/nujhatcart": "5.4" }
然后,从终端运行 Composer update 命令
composer update
现在你只需将包的服务提供者和别名添加到 config/app.php
文件中。
在 service providers
数组中添加新行
Lutforrahman\Nujhatcart\NujhatcartServiceProvider::class
之后,在 aliases
数组中添加新行
'Cart' => Lutforrahman\Nujhatcart\Facades\Cart::class,
现在你可以开始在应用程序中使用购物车了。
文档
查看以下主题之一,了解 iLaCart 的更多信息
添加到购物车
购物车提供以下方法供使用
Cart::insert()
/** * Add a row to the cart * @param string|array $id Unique ID of the item|Item formated as array|Array of items * @param string $sku Unique SKU of the item * @param string $name Name of the item * @param string $slug Slug of the item * @param string $image Image of the item * @param string $description Description of the item * @param int $quantity Item quantity to add to the cart * @param float $price Price of one item * @param float $discount Discount amount of one item * @param float $tax Tax amount of one item * @param array $options Array of additional options, such as 'size' or 'color' */ $product = Product::find($id); $item = [ 'id' => $product->id, 'sku' => $product->sku, 'name' => $product->name, 'slug' => $product->slug, 'image' => $product->thumbnail, 'description' => $product->description, 'quantity' => $quantity > 0 ? $quantity : 1, 'price' => $product->price, 'discount' => $product->discount_amount, 'tax' => 0, 'options' => array('size' => 'M', 'color' => 'White') ]; Cart::insert($item); // Insert multiple arry() $product = Product::find($id); $item = [ 'id' => $product->id, 'sku' => $product->sku, 'name' => $product->name, 'slug' => $product->slug, 'image' => $product->thumbnail, 'description' => $product->description, 'quantity' => $quantity > 0 ? $quantity : 1, 'price' => $product->price, 'discount' => $product->discount_amount, 'tax' => 0, 'options' => array('size' => 'M', 'color' => 'White') ]; $product2 = Product::find($id2); $item2 = [ 'id' => $product2->id, 'sku' => $product2->sku, 'name' => $product2->name, 'slug' => $product2->slug, 'image' => $product2->thumbnail, 'description' => $product2->description, 'quantity' => $product2 > 0 ? $quantity : 1, 'price' => $product2->price, 'discount' => $product2->discount_amount, 'tax' => 0, 'options' => array('size' => 'M', 'color' => 'White') ]; Cart::insert(array($item, $item2));
更新购物车
Cart::update()
/** * Update the quantity of one row of the cart * * @param string $rowId The rowid of the item you want to update * @param integer|Array $attribute New quantity of the item|Array of attributes to update * @return boolean */ $rowId = 'feeb69e1a11765b136a0de76c2baaa40'; Cart::update($rowId, 4); OR Cart::update($rowId, array('name' => 'Product name')); OR $product = Product::find($id); $item = [ 'id' => $product->id, 'sku' => $product->sku, 'name' => $product->name, 'slug' => $product->slug, 'image' => $product->thumbnail, 'description' => $product->description, 'quantity' => $quantity > 0 ? $quantity : 1, 'price' => $product->price, 'discount' => $product->discount_amount, 'tax' => 0, 'options' => array('size' => 'M', 'color' => 'White') ]; Cart::update($rowId, $item);
// 控制器中
public function updateCart($rowId){ $item = [ 'quantity' => 3, 'discount' => 9, 'tax' => 9.5, 'options' => array('size' => 'M', 'color' => 'White') ]; Cart::update($rowId, $item); return Cart::contents(); }
从购物车中移除商品
Cart::remove()
/** * Remove a row from the cart * * @param string $rowId The rowid of the item * @return boolean */ $rowId = 'feeb69e1a11765b136a0de76c2baaa40'; Cart::remove($rowId);
// 控制器中
public function removeCart($rowId){ Cart::remove($rowId); return Cart::contents(); }
从购物车中获取单个商品
Cart::get()
/** * Get a row of the cart by its ID * * @param string $rowId The ID of the row to fetch * @return CartRowCollection */ $rowId = 'feeb69e1a11765b136a0de76c2baaa40'; Cart::get($rowId);
从购物车中获取所有商品
Cart::contents()
/** * Get the cart content * * @return CartCollection */ Cart::contents();
清空购物车 [移除购物车中的所有商品]
Cart::destroy()
/** * Empty the cart * * @return boolean */ Cart::destroy();
获取购物车中添加商品的总额
Cart::total()
/** * Total amount of cart * * @return float */ Cart::total();
[小计] 获取购物车中添加商品的总额 [单个商品数量大于1时]
Cart::subtotal()
/** * Sub total amount of cart * * @return float */ Cart::subtotal();
获取购物车中添加商品的总折扣额
Cart::discount()
/** * Discount of cart * * @return float */ Cart::discount();
Cart::setCustomDiscount(5.00)
/** * @param $amount * @return bool */ Cart::setCustomDiscount(5.00);
Cart::customDiscount()
/** * Custom discount of cart * * @return float */ Cart::customDiscount();
获取购物车中单个商品的总量
Cart::cartQuantity()
/** * Get the number of items in the cart * * @param boolean $totalItems Get all the items (when false, will return the number of rows) * @return int */ Cart::cartQuantity(); // Total items Cart::cartQuantity(false); // Total rows
显示购物车内容
foreach(Cart::contents() as $item) { echo "<img src=".$item->image." width='40'/> " . ' Name : ' . $item->name . ' Price : ' . $item->price . ' Size : ' . $item->options->size; }
异常
当出现问题时,购物车包会抛出异常。这样,使用购物车包调试代码或根据异常类型处理错误就更容易了。购物车包可以抛出以下异常
示例
// 控制器
/** * Display the specified resource. * * @param int $id * @param int $quantity * @param string $size * @param string $color * @return \Illuminate\Http\Response */ public function storeCart($id, $quantity, $size, $color) { $product = Product::find($id); $item = [ 'id' => $product->id, 'sku' => $product->sku, 'name' => $product->name, 'slug' => $product->slug, 'image' => $product->thumbnail, 'description' => $product->description, 'quantity' => $quantity > 0 ? $quantity : 1, 'price' => $product->price, 'discount' => $product->discount_amount, 'tax' => 0, 'options' => ['size' => $size, 'color' => $color] ]; Cart::insert($item); $items = Cart::contents(); $quantity = Cart::cartQuantity(); $total = Cart::total(); return view('home', ['items' => $items, 'quantity' => $quantity, 'total' => $total]); }
// 视图
<table> <thead> <tr> <th>Product</th> <th>Quantity</th> <th>Item Price</th> <th>Discount</th> <th>Subtotal</th> </tr> </thead> <tbody> @foreach(Cart::contents() as $item) <tr> <td> <p><strong>{{ $item->name }} </strong></p> <p>{{ $item->options->has('size') ? $item->options->size : '' }} </p> <p>{{ $item->options->has('color') ? $item->options->color : '' }} </p> </td> <td><input type="text" value="{{ $item->quantity }}"></td> <td>${{ $item->price }} </td> <td>${{ $item->discount }} </td> <td>${{ $item->subtotal }}</td> </tr> @endforeach </tbody> </table>