Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Http\Controllers;

use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\Facades\Cart as FacadesCart;

class CartController extends Controller
{
/**
Expand All @@ -21,6 +24,7 @@ public function __construct()
*/
public function index()
{
// dd(\Cart::content());
return view('cart');
}

Expand All @@ -29,9 +33,26 @@ public function add(int $id) {
$basePrice = 42.42;

/* Add the product */
\Cart::add($id, 'Product ' . $id, 1, $basePrice * $id, 500);
$cart = \Cart::add($id, 'Product ' . $id, 1, $basePrice * $id, 500);


/* Redirect to prevend re-adding on refreshing */
return redirect()->route('cart')->withSuccess('Product has been successfully added to the Cart.');
}

public function remove($rowId){
// get row id
\Cart::remove($rowId);

//return to cart page
return view('cart');
}

public function empty(){
// This will remove all CartItems from the cart for the current cart instance.
\Cart::destroy();

//return to cart page
return view('cart');
}
}
22 changes: 22 additions & 0 deletions resources/views/cart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,39 @@
<tr>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Tax Rate</th>
<th scope="col">Total</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@php $grandTotal = 0 @endphp
@foreach (Cart::content() as $item)
<tr>
<td>{{ $item->name }}</td>
<td>{{ $item->price }}</td>
<td>{{ $item->qty }}</td>
<td>{{ $item->taxRate }}</td>
<td>{{ $item->total }}</td>
<td><a href="{{ route('remove', ['rowId' => $item->rowId]) }}">Delete</a></td>
</tr>
@endforeach
<tr style="text-align:center">
<td colspan="5"><b style="color:red">Total Tax </b> {{Cart::tax()}} </td>
</tr>
<tr style="text-align:center">
<td colspan="5"><b style="color:red">Total Discount </b> {{Cart::discount()}} </td>
</tr>
<tr style="text-align:center">
<td colspan="5"><b style="color:red">Total: </b> {{Cart::subtotal()}} (without tax) </td>
</tr>
<tr style="text-align:center">
<td colspan="5"><b style="color:red">Grand Total: </b> {{ Cart::total() }} (with tax and discount)</td>
</tr>
<tr style="text-align:center">
<td colspan="5"><a class="btn btn-danger" href="{{ route('empty')}}">Empty Cart</a></td>
</tr>
</tbody>
</table>
@else
Expand Down