PHP code example of lutforrahman / nujhatcart
1. Go to this page and download the library: Download lutforrahman/nujhatcart library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lutforrahman / nujhatcart example snippets
composer
""laravel/framework": "5.4.*",
"lutforrahman/nujhatcart": "5.4"
}
/**
* 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 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();
}
/**
* 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();
}
/**
* 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 the cart content
*
* @return CartCollection
*/
Cart::contents();
/**
* Empty the cart
*
* @return boolean
*/
Cart::destroy();
/**
* Total amount of cart
*
* @return float
*/
Cart::total();
/**
* Sub total amount of cart
*
* @return float
*/
Cart::subtotal();
/**
* Discount of cart
*
* @return float
*/
Cart::discount();
/**
* @param $amount
* @return bool
*/
Cart::setCustomDiscount(5.00);
/**
* Custom discount of cart
*
* @return float
*/
Cart::customDiscount();
/**
* 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>