Commit c7ae6238 authored by Artem's avatar Artem

add shopping cart view, add to cart method

parent c8862b3c
......@@ -6,37 +6,27 @@ namespace App;
class Cart /*extends Model*/
{
public $items = null;
public $total_qty = 0;
public $total_price = 0;
public function __construct($old_cart)
public function __construct()
{
if ($old_cart) {
$this->items = $old_cart->items;
$this->total_qty = $old_cart->total_qty;
$this->total_price = $old_cart->total_price;
}
$this->items = [];
$this->total_qty = 0;
$this->total_price = 0;
}
public function addItemToCart($item, $id)
public function addItem($product, $id)
{
$stored_item = ['qty' => 0, 'price' => $item->price, 'item' => $item];
if ($this->items) {
if (array_key_exists($id, $this->items)) {
$stored_item = $this->items[$id];
$this->items[$id]['qty']++;
} else {
$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_price += $item->price;
$this->total_price += $product->price;
}
}
......@@ -2,12 +2,29 @@
namespace App\Http\Controllers;
use App\Cart;
use Illuminate\Http\Request;
use App\Product;
use Session;
class CartController extends Controller
{
public function index()
{
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
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(/*Request $request*/)
public function store()
{
// request()->validate([
// 'reference' => 'required',
// 'description_short' => 'required',
// 'description_long' => 'required',
// 'price' => 'required',
// 'type' => 'required'
// ]);
//
// $product = new Product();
// $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::create([
// 'reference' => request('reference'),
// 'description_short' => request('description_short'),
// 'description_long' => request('description_long'),
// 'type' => request('type'),
// 'price' => request('price')
// ]);
//
// Product::create(request()->validate([
// 'reference' => 'required',
// 'description_short' => 'required',
// 'description_long' => 'required',
// 'type' => 'required',
// 'price' => 'required'
// ]));
/* request()->validate([
'reference' => 'required',
'description_short' => 'required',
'description_long' => 'required',
'price' => 'required',
'type' => 'required'
]);
$product = new Product();
$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::create([
'reference' => request('reference'),
'description_short' => request('description_short'),
'description_long' => request('description_long'),
'type' => request('type'),
'price' => request('price')
]);
Product::create(request()->validate([
'reference' => 'required',
'description_short' => 'required',
'description_long' => 'required',
'type' => 'required',
'price' => 'required'
]));*/
request()->validate([
'reference' => 'unique:products,reference'
......@@ -88,13 +88,9 @@ class ProductController extends Controller
*/
public function show(Product $product)
{
// $product = DB::table('products')->where('id', $product)->first();
// $product = Product::where('id', $product)->firstOrFail();
// $product = Product::findOrFail($product);
// return view('product', [
// 'product' => $product
// ]);
/* $product = DB::table('products')->where('id', $product)->first();
$product = Product::where('id', $product)->firstOrFail();
$product = Product::findOrFail($product);*/
return view('product.product', compact('product'));
}
......@@ -117,22 +113,8 @@ class ProductController extends Controller
* @param \App\Product $product
* @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());
......@@ -162,8 +144,4 @@ class ProductController extends Controller
]);
}
public function addToCart(Request $request, Product $product)
{
// return $product;
}
}
......@@ -3,6 +3,8 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Session;
use App\Cart;
class Product extends Model
{
......@@ -19,5 +21,15 @@ class Product extends Model
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) {
if (!e.target.classList.contains('btn-add-to-basket')) return;
function setAddToCardEventListener() {
document.querySelector('.products').addEventListener('click', function(e) {
if (!e.target.classList.contains('js-btn-add-to-basket')) return;
e.preventDefault();
const actionUrl = e.target.dataset.action;
......@@ -8,11 +9,13 @@ document.querySelector('.products').addEventListener('click', function(e) {
productId: e.target.dataset.product_id
})
.then(function (response) {
console.log(response);
const amountInCart = document.querySelector('.amount-in-cart');
amountInCart.textContent++;
const cartAmountElem = document.querySelector('.amount-in-cart');
cartAmountElem.textContent++;
})
.catch(function (error) {
console.log(error);
});
});
});
}
setAddToCardEventListener();
@extends('layouts.app')
@section('content')
<section class="cart">
<div class="container">
<div>OLOLO</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
......@@ -14,7 +14,7 @@
<!-- Left Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<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>
</li>
</ul>
......
......@@ -13,7 +13,6 @@
@foreach($products as $product)
<li class="product-list__item">
<div class="product">
{{-- <a href="/product/{{$product->id}}" class="product__image">--}}
<a href="{{ $product->path() }}" class="product__image">
<img src="/img/{{$product->type}}.jpg" alt="{{$product->reference}}">
</a>
......@@ -21,7 +20,7 @@
<p class="product__short-text">{{$product->description_short}}</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-action="{{ route('product.addToCart', $product) }}"
>Add to basket</button>
......
......@@ -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::get('/product/create', 'ProductController@create');
Route::get('/product/{product}', 'ProductController@show')->name('product.show');
......@@ -26,6 +26,6 @@ Auth::routes();
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');
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