lutforrahman/nujhatcart
Composer 安装命令:
composer require lutforrahman/nujhatcart
包简介
Nujhatcart The Laravel Shoppingcart
README 文档
README
Nujhatcart The Laravel Shoppingcart
A simple shoppingcart implementation for Laravel >=5.4
Installation
Laravel 5.4
Install the package through Composer. Open your terminal in your project directory, wirte :
composer require lutforrahman/nujhatcart
Press enter and package will start downloading ...
OR
Edit your project's composer.json file and add :
"require": { "laravel/framework": "5.4.*", "lutforrahman/nujhatcart": "5.4" }
Next, run the Composer update command from the Terminal:
composer update
Now all you have to do is add the service provider of the package and alias the package. To do this open your config/app.php file.
Add a new line to the service providers array:
Lutforrahman\Nujhatcart\NujhatcartServiceProvider::class
After that add a new line to the aliases array:
'Cart' => Lutforrahman\Nujhatcart\Facades\Cart::class,
Now you're ready to start using the shoppingcart in your application.
Documentation
Look at one of the following topics to learn more about iLaCart
Add to Cart
The shoppingcart gives you the following methods to use:
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));
Update Cart
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);
// In Controller
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(); }
Remove an Item from Cart
Cart::remove()
/** * Remove a row from the cart * * @param string $rowId The rowid of the item * @return boolean */ $rowId = 'feeb69e1a11765b136a0de76c2baaa40'; Cart::remove($rowId);
// In Controller
public function removeCart($rowId){ Cart::remove($rowId); return Cart::contents(); }
Get a single Item from Cart
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);
Get all Items from Cart
Cart::contents()
/** * Get the cart content * * @return CartCollection */ Cart::contents();
Empty Cart [ remove all items from cart]
Cart::destroy()
/** * Empty the cart * * @return boolean */ Cart::destroy();
Get total amount of added Items in Cart
Cart::total()
/** * Total amount of cart * * @return float */ Cart::total();
[Subtotal] Get total amount of an added Item in Cart [single item with quantity > 1]
Cart::subtotal()
/** * Sub total amount of cart * * @return float */ Cart::subtotal();
Get total discount amount of items added in Cart
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();
Get total quantity of a single item added in Cart
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
Show Cart contents
foreach(Cart::contents() as $item) { echo "<img src=".$item->image." width='40'/> " . ' Name : ' . $item->name . ' Price : ' . $item->price . ' Size : ' . $item->options->size; }
Exceptions
The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
| Exception | Reason |
|---|---|
| NujhatcartInstanceException | When no instance is passed to the instance() method |
| NujhatcartInvalidItemException | When a new product misses one of it's arguments (id, name, quantity, price, tax) |
| NujhatcartInvalidDiscountException | When a non-numeric discount is passed |
| NujhatcartInvalidPriceException | When a non-numeric price is passed |
| NujhatcartInvalidQuantityException | When a non-numeric quantity is passed |
| NujhatcartInvalidItemIDException | When the $itemId that got passed doesn't exists in the current cart |
| NujhatcartInvalidTaxException | When a non-numeric tax is passed |
| NujhatcartUnknownModelException | When an unknown model is associated to a cart row |
Example
// Controller
/** * 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]); }
// View
<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>
Follow me
Follow me on twitter : https://twitter.com/social_lutfor
lutforrahman/nujhatcart 适用场景与选型建议
lutforrahman/nujhatcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.79k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ecommerce」 「shoppingcart」 「Laravel Shoppingcart」 「Nujhatcart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lutforrahman/nujhatcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lutforrahman/nujhatcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lutforrahman/nujhatcart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nevogate Payment Gateway SDK
Laravel Shoppingcart
Dealing with payments through the Egyptian payment gateway PayMob
Laravel Shoppingcart
A PHP (and Laravel) package to interface with the Snipcart api.
Alfabank REST API integration
统计信息
- 总下载量: 1.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-06