Commit c7ae6238 authored by Artem's avatar Artem

add shopping cart view, add to cart method

parent c8862b3c
...@@ -6,37 +6,27 @@ namespace App; ...@@ -6,37 +6,27 @@ namespace App;
class Cart /*extends Model*/ class Cart /*extends Model*/
{ {
public $items = null; public function __construct()
public $total_qty = 0;
public $total_price = 0;
public function __construct($old_cart)
{ {
if ($old_cart) { $this->items = [];
$this->items = $old_cart->items; $this->total_qty = 0;
$this->total_qty = $old_cart->total_qty; $this->total_price = 0;
$this->total_price = $old_cart->total_price;
}
} }
public function addItemToCart($item, $id) public function addItem($product, $id)
{ {
$stored_item = ['qty' => 0, 'price' => $item->price, 'item' => $item]; if (array_key_exists($id, $this->items)) {
if ($this->items) { $this->items[$id]['qty']++;
if (array_key_exists($id, $this->items)) { } else {
$stored_item = $this->items[$id]; $this->items[$id] = [
} 'item' => $product,
'qty' => 1
];
} }
// $stored_item = ($this->items && array_key_exists($id, $this->items)) ?
// $stored_item = $this->items[$id] :
// ['qty' => 0, 'price' => $item->price, 'item' => $item];
$stored_item['qty']++;
$stored_item['price'] = $item->price * $stored_item['qty'];
$this->items[$id] = $stored_item;
$this->total_qty++; $this->total_qty++;
$this->total_price += $item->price; $this->total_price += $product->price;
} }
} }
...@@ -2,12 +2,29 @@ ...@@ -2,12 +2,29 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Cart;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Product;
use Session;
class CartController extends Controller class CartController extends Controller
{ {
public function index() public function index()
{ {
return view('cart');
if (!Session::has('cart')) {
return view('cart');
}
$cart = Session::get('cart');
$products = $cart->items;
return view('cart', compact('cart', 'products'));
}
public function addToCart(Request $request, Product $product)
{
$product->addItemToCart();
} }
} }
...@@ -37,39 +37,39 @@ class ProductController extends Controller ...@@ -37,39 +37,39 @@ class ProductController extends Controller
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function store(/*Request $request*/) public function store()
{ {
// request()->validate([ /* request()->validate([
// 'reference' => 'required', 'reference' => 'required',
// 'description_short' => 'required', 'description_short' => 'required',
// 'description_long' => 'required', 'description_long' => 'required',
// 'price' => 'required', 'price' => 'required',
// 'type' => 'required' 'type' => 'required'
// ]); ]);
//
// $product = new Product(); $product = new Product();
// $product->reference = request('reference'); $product->reference = request('reference');
// $product->description_short = request('description_short'); $product->description_short = request('description_short');
// $product->description_long = request('description_long'); $product->description_long = request('description_long');
// $product->price = request('price'); $product->price = request('price');
// $product->type = request('type'); $product->type = request('type');
// $product->save(); $product->save();
// Product::create([ Product::create([
// 'reference' => request('reference'), 'reference' => request('reference'),
// 'description_short' => request('description_short'), 'description_short' => request('description_short'),
// 'description_long' => request('description_long'), 'description_long' => request('description_long'),
// 'type' => request('type'), 'type' => request('type'),
// 'price' => request('price') 'price' => request('price')
// ]); ]);
//
// Product::create(request()->validate([ Product::create(request()->validate([
// 'reference' => 'required', 'reference' => 'required',
// 'description_short' => 'required', 'description_short' => 'required',
// 'description_long' => 'required', 'description_long' => 'required',
// 'type' => 'required', 'type' => 'required',
// 'price' => 'required' 'price' => 'required'
// ])); ]));*/
request()->validate([ request()->validate([
'reference' => 'unique:products,reference' 'reference' => 'unique:products,reference'
...@@ -88,13 +88,9 @@ class ProductController extends Controller ...@@ -88,13 +88,9 @@ class ProductController extends Controller
*/ */
public function show(Product $product) public function show(Product $product)
{ {
// $product = DB::table('products')->where('id', $product)->first(); /* $product = DB::table('products')->where('id', $product)->first();
// $product = Product::where('id', $product)->firstOrFail(); $product = Product::where('id', $product)->firstOrFail();
// $product = Product::findOrFail($product); $product = Product::findOrFail($product);*/
// return view('product', [
// 'product' => $product
// ]);
return view('product.product', compact('product')); return view('product.product', compact('product'));
} }
...@@ -117,22 +113,8 @@ class ProductController extends Controller ...@@ -117,22 +113,8 @@ class ProductController extends Controller
* @param \App\Product $product * @param \App\Product $product
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(/*Request $request,*/ Product $product ) public function update( Product $product )
{ {
// request()->validate([
// 'reference' => 'required',
// 'description_short' => 'required',
// 'description_long' => 'required',
// 'price' => 'required',
// 'type' => 'required'
// ]);
//
// $product->reference = request('reference');
// $product->description_short = request('description_short');
// $product->description_long = request('description_long');
// $product->price = request('price');
// $product->type = request('type');
// $product->save();
$product->update($this->validateProduct()); $product->update($this->validateProduct());
...@@ -162,8 +144,4 @@ class ProductController extends Controller ...@@ -162,8 +144,4 @@ class ProductController extends Controller
]); ]);
} }
public function addToCart(Request $request, Product $product)
{
// return $product;
}
} }
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace App; namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Session;
use App\Cart;
class Product extends Model class Product extends Model
{ {
...@@ -19,5 +21,15 @@ class Product extends Model ...@@ -19,5 +21,15 @@ class Product extends Model
return route('product.edit', $this); return route('product.edit', $this);
} }
public function addItemToCart()
{
if (Session::has('cart')) {
$cart = Session::get('cart');
$cart->addItem($this, $this->id);
} else {
$cart = new Cart();
$cart->addItem($this, $this->id);
Session::put('cart', $cart);
}
}
} }
document.querySelector('.products').addEventListener('click', function(e) { function setAddToCardEventListener() {
if (!e.target.classList.contains('btn-add-to-basket')) return; document.querySelector('.products').addEventListener('click', function(e) {
e.preventDefault(); if (!e.target.classList.contains('js-btn-add-to-basket')) return;
e.preventDefault();
const actionUrl = e.target.dataset.action; const actionUrl = e.target.dataset.action;
axios.post(actionUrl, { axios.post(actionUrl, {
productId: e.target.dataset.product_id productId: e.target.dataset.product_id
}) })
.then(function (response) { .then(function (response) {
console.log(response); const cartAmountElem = document.querySelector('.amount-in-cart');
const amountInCart = document.querySelector('.amount-in-cart'); cartAmountElem.textContent++;
amountInCart.textContent++; })
}) .catch(function (error) {
.catch(function (error) { console.log(error);
console.log(error); });
}); });
}); }
setAddToCardEventListener();
@extends('layouts.app') @extends('layouts.app')
@section('content') @section('content')
<div class="container"> <section class="cart">
<div>OLOLO</div> <div class="container">
</div>
@if(Session::has('cart'))
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Price</th>
<th style="width:10%">Quantity</th>
<th style="width:15%" class="text-center">Subtotal</th>
<th style="width:15%" class="text-right"><button class="btn btn-danger btn-sm ml-auto">Clear cart</button></th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>
<div class="row">
<div class="col-sm-3 hidden-xs"><img src="/img/{{ $product['item']->type }}.jpg" width="100" height="100" alt="{{ $product['item']->reference }}" class="img-responsive"/></div>
<div class="col-sm-9">
<h4 class="nomargin"><a href="{{ $product['item']->path() }}">{{ $product['item']->reference }}</a></h4>
<p>{{ $product['item']->description_short }}</p>
</div>
</div>
</td>
<td>€ {{ $product['item']->price }}</td>
<td>
<input type="number" class="form-control text-center" value="{{ $product['qty'] }}">
</td>
<td class="text-center">€ {{ $product['item']->price * $product['qty'] }}</td>
<td class="actions text-right">
<button class="btn btn-danger btn-sm">Remove</button>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<td>
<a href="{{ route('homepage') }}" class="btn btn-primary"> Continue Shopping</a>
</td>
<td colspan="2" class="text-right">
<button class="btn btn-warning">Update quantity</button>
</td>
<td class="hidden-xs text-center"><strong>Total € {{ $cart->total_price }}</strong></td>
<td><button class="btn btn-success btn-block">Send order</button></td>
</tr>
</tfoot>
</table>
@else
<div>There are no products in the cart. <a href="{{ route('homepage') }}">Check our products</a></div>
@endif
</div>
</section>
@endsection @endsection
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<!-- Left Side Of Navbar --> <!-- Left Side Of Navbar -->
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
<li class="nav-item"> <li class="nav-item">
<span class="amount-in-cart"></span> <span class="amount-in-cart badge badge-info text-white">{{ Session::has('cart') ? Session::get('cart')->total_qty : ''}}</span>
<a href="{{ route('cart.index') }}">Shopping cart</a> <a href="{{ route('cart.index') }}">Shopping cart</a>
</li> </li>
</ul> </ul>
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
@foreach($products as $product) @foreach($products as $product)
<li class="product-list__item"> <li class="product-list__item">
<div class="product"> <div class="product">
{{-- <a href="/product/{{$product->id}}" class="product__image">--}}
<a href="{{ $product->path() }}" class="product__image"> <a href="{{ $product->path() }}" class="product__image">
<img src="/img/{{$product->type}}.jpg" alt="{{$product->reference}}"> <img src="/img/{{$product->type}}.jpg" alt="{{$product->reference}}">
</a> </a>
...@@ -21,7 +20,7 @@ ...@@ -21,7 +20,7 @@
<p class="product__short-text">{{$product->description_short}}</p> <p class="product__short-text">{{$product->description_short}}</p>
<p class="product__price">{{$product->price}} EUR</p> <p class="product__price">{{$product->price}} EUR</p>
<button class="btn-add-to-basket btn btn-info" <button class="js-btn-add-to-basket btn btn-info"
data-product_id="{{ $product->id }}" data-product_id="{{ $product->id }}"
data-action="{{ route('product.addToCart', $product) }}" data-action="{{ route('product.addToCart', $product) }}"
>Add to basket</button> >Add to basket</button>
......
...@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Route; ...@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Route;
| |
*/ */
Route::get('/', 'ProductController@index'); Route::get('/', 'ProductController@index')->name('homepage');
Route::post('/product', 'ProductController@store'); Route::post('/product', 'ProductController@store');
Route::get('/product/create', 'ProductController@create'); Route::get('/product/create', 'ProductController@create');
Route::get('/product/{product}', 'ProductController@show')->name('product.show'); Route::get('/product/{product}', 'ProductController@show')->name('product.show');
...@@ -26,6 +26,6 @@ Auth::routes(); ...@@ -26,6 +26,6 @@ Auth::routes();
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home', 'HomeController@index')->name('home');
Route::post('/add-to-cart/{product}', 'ProductController@addToCart')->name('product.addToCart'); Route::post('/add-to-cart/{product}', 'CartController@addToCart')->name('product.addToCart');
Route::get('/cart', 'CartController@index')->name('cart.index'); Route::get('/cart', 'CartController@index')->name('cart.index');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment