lutforrahman / nujhatcart
Nujhatcart The Laravel Shoppingcart
Installs: 1 785
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 6
Forks: 6
Open Issues: 1
Type:package
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
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:
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