, (*1)
, (*2)
, (*3)
A simple shoppingcart implementation for Laravel >=5.4, (*4)
Installation
Laravel 5.4
Install the package through Composer . Open your terminal in your project directory, wirte :, (*5)
composer require lutforrahman/nujhatcart
Press enter and package will start downloading ..., (*6)
OR, (*7)
Edit your project's composer.json
file and add :, (*8)
"require": {
"laravel/framework": "5.4.*",
"lutforrahman/nujhatcart": "5.4"
}
Next, run the Composer update command from the Terminal:, (*9)
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., (*10)
Add a new line to the service providers
array:, (*11)
Lutforrahman\Nujhatcart\NujhatcartServiceProvider::class
After that add a new line to the aliases
array:, (*12)
'Cart' => Lutforrahman\Nujhatcart\Facades\Cart::class,
Now you're ready to start using the shoppingcart in your application., (*13)
Documentation
Look at one of the following topics to learn more about iLaCart, (*14)
Add to Cart
The shoppingcart gives you the following methods to use:, (*15)
Cart::insert() , (*16)
/**
* 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() , (*17)
/**
* 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, (*18)
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() , (*19)
/**
* Remove a row from the cart
*
* @param string $rowId The rowid of the item
* @return boolean
*/
$rowId = 'feeb69e1a11765b136a0de76c2baaa40';
Cart::remove($rowId);
// In Controller, (*20)
public function removeCart($rowId){
Cart::remove($rowId);
return Cart::contents();
}
Get a single Item from Cart
Cart::get() , (*21)
/**
* 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() , (*22)
/**
* Get the cart content
*
* @return CartCollection
*/
Cart::contents();
Empty Cart [ remove all items from cart]
Cart::destroy() , (*23)
/**
* Empty the cart
*
* @return boolean
*/
Cart::destroy();
Get total amount of added Items in Cart
Cart::total() , (*24)
/**
* 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() , (*25)
/**
* Sub total amount of cart
*
* @return float
*/
Cart::subtotal();
Get total discount amount of items added in Cart
Cart::discount() , (*26)
/**
* Discount of cart
*
* @return float
*/
Cart::discount();
Cart::setCustomDiscount(5.00) , (*27)
/**
* @param $amount
* @return bool
*/
Cart::setCustomDiscount(5.00);
Cart::customDiscount() , (*28)
/**
* Custom discount of cart
*
* @return float
*/
Cart::customDiscount();
Get total quantity of a single item added in Cart
Cart::cartQuantity() , (*29)
/**
* 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:, (*30)
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, (*31)
/**
* 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, (*32)
Product
Quantity
Item Price
Discount
Subtotal
@foreach(Cart::contents() as $item)
{{ $item->name }} , (*33)
{{ $item->options->has('size') ? $item->options->size : '' }} , (*34)
{{ $item->options->has('color') ? $item->options->color : '' }} , (*35)
${{ $item->price }}
${{ $item->discount }}
${{ $item->subtotal }}
@endforeach
Follow me
Follow me on twitter : https://twitter.com/social_lutfor, (*36)