Skip to content

Commit

Permalink
Now users can change quantity of product in cart, remove all products…
Browse files Browse the repository at this point in the history
… in cart. Also major improvements in backend, in the way to displaying
  • Loading branch information
vilgefortzz committed May 3, 2017
1 parent be85757 commit d1cd82d
Show file tree
Hide file tree
Showing 28 changed files with 558 additions and 256 deletions.
77 changes: 58 additions & 19 deletions app/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
class Cart extends Model
{
public $products = array();
public $totalQuantity = 0;

/*
* Total quantity of products existed in cart
*/
public $numberOfProducts = 0;

/*
* Total amount of money for all products and items
*/
public $totalPrice = 0;

/**
Expand All @@ -17,26 +25,28 @@ public function __construct($oldCart)
{
if ($oldCart){
$this->products = $oldCart->products;
$this->totalQuantity = $oldCart->totalQuantity;
$this->numberOfProducts = $oldCart->numberOfProducts;
$this->totalPrice = $oldCart->totalPrice;
}
}

public function addProduct($product){
public function addProduct(Product $product){

$storedProduct = ['id' => 0, 'quantity' => 0, 'price' => $product->price, 'product' => $product];
$storedProduct = ['id' => 0, 'quantity' => 1,
'priceForOneItem' => $product->price, 'priceForAllItems' => $product->price,
'onStock' => $product->quantity, 'product' => $product];

/**
* Only one user can add one product to cart. Later he will decide how many items choose
*/
if ($this->products){

if (!array_key_exists($product->id, $this->products)){

$storedProduct['id'] = $product->id;
$storedProduct['quantity']++;
$storedProduct['price'] = $product->price * $storedProduct['quantity'];
$storedProduct['priceForAllItems'] = $product->price * $storedProduct['quantity'];
$this->products[$product->id] = $storedProduct;
$this->totalQuantity++;
$this->numberOfProducts++;
$this->totalPrice += $product->price;
}
}
Expand All @@ -46,41 +56,70 @@ public function addProduct($product){
* Add first product to cart
*/
$storedProduct['id'] = $product->id;
$storedProduct['quantity']++;
$storedProduct['price'] = $product->price * $storedProduct['quantity'];
$storedProduct['priceForAllItems'] = $product->price * $storedProduct['quantity'];
$this->products[$product->id] = $storedProduct;
$this->totalQuantity++;
$this->numberOfProducts++;
$this->totalPrice += $product->price;
}
}

public function deleteProduct($product){
public function deleteProduct(Product $product){

if ($this->products){

if (count($this->products) == 1){
if ($product->id == array_first($this->products)['id']){
$this->numberOfProducts--;
$this->totalPrice -= $this->products[$product->id]['priceForAllItems'];
unset($this->products[$product->id]);
$this->totalQuantity--;
$this->totalPrice -= $product->price;
}
}
else{
if (array_key_exists($product->id, $this->products)){
$this->numberOfProducts--;
$this->totalPrice -= $this->products[$product->id]['priceForAllItems'];
unset($this->products[$product->id]);
$this->totalQuantity--;
$this->totalPrice -= $product->price;
}
}
}

return $this->totalPrice;
}

public function deleteAllProducts(){

if ($this->products){
unset($this->products);
$this->numberOfProducts = 0;
$this->totalPrice = 0;
}
}

public function setProductsQuantity($products_id, $newQuantity){
public function setQuantity(Product $product, int $newQuantityValue){

foreach ($products_id as $product_id){
if (array_key_exists($product_id, $this->products)){
$this->products[$product_id]['quantity'] = $newQuantity;
if ($this->products){

if (count($this->products) == 1){
if ($product->id == array_first($this->products)['id']){
$this->products[$product->id]['quantity'] = $newQuantityValue;
$this->products[$product->id]['priceForAllItems'] = $product->price * $newQuantityValue;
$this->totalPrice = $this->products[$product->id]['priceForAllItems'];
}
}
else{
if (array_key_exists($product->id, $this->products)){
$this->products[$product->id]['quantity'] = $newQuantityValue;
$this->products[$product->id]['priceForAllItems'] = $product->price * $newQuantityValue;

$this->totalPrice = 0;

foreach ($this->products as $product){
$this->totalPrice += $product['priceForAllItems'];
}
}
}
}

return $this->totalPrice;
}
}
11 changes: 11 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class LoginController extends Controller
*
* @return void
*/

public function showLoginForm()
{
if(!session()->has('url.intended'))
{
session(['url.intended' => url()->previous()]);
}

return view('auth.login');
}

public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
Expand Down
42 changes: 39 additions & 3 deletions app/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function showProducts() {
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->products, 'totalPrice' => $cart->totalPrice, 'totalQuantity' => $cart->totalQuantity]);
return view('cart', ['products' => $cart->products, 'totalPrice' => $cart->totalPrice, 'numberOfProducts' => $cart->numberOfProducts]);

}

Expand Down Expand Up @@ -50,10 +50,46 @@ public function deleteProduct(Product $product, Request $request) {

$cart = new Cart($oldCart);

$cart->deleteProduct($product);
$newTotalPrice = $cart->deleteProduct($product);

$request->session()->put('cart', $cart);

return response()->json($product);
return response()->json(array(
'newTotalPrice' => $newTotalPrice,
'product' => $product
));
}

/*
* AJAX request
*/
public function deleteAllProducts(Request $request) {

$oldCart = $request->session()->has('cart') ? $request->session()->get('cart') : null;

$cart = new Cart($oldCart);

$cart->deleteAllProducts();

$request->session()->put('cart', $cart);
}

/*
* AJAX request
*/
public function setQuantity(Product $product, Request $request) {

$oldCart = $request->session()->has('cart') ? $request->session()->get('cart') : null;

$cart = new Cart($oldCart);

$newTotalPrice = $cart->setQuantity($product, $request->newQuantityValue);

$request->session()->put('cart', $cart);

return response()->json(array(
'newTotalPrice' => $newTotalPrice,
'product' => $product
));
}
}
28 changes: 0 additions & 28 deletions app/Http/Controllers/HomeController.php

This file was deleted.

6 changes: 3 additions & 3 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ProductController extends Controller
{
public function showReviews(Product $product){
public function show(Product $product){

// Get all reviews for this product and sort reviews by date from the newest one
$reviews = Review::where('product_id', $product->id)->orderBy('created_at', 'desc')->paginate(2);
Expand All @@ -19,9 +19,9 @@ public function showReviews(Product $product){

$isGiven = $reviews->contains('user_id', Auth::user()->id);

return view('product_details', compact('product', 'isGiven', 'reviews'));
return view('products.product_details', compact('product', 'isGiven', 'reviews'));
}

return view('product_details', compact('product', 'reviews'));
return view('products.product_details', compact('product', 'reviews'));
}
}
6 changes: 0 additions & 6 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

class UserController extends Controller
{
public function index(){

$users = User::all();

return view('users', compact('users'));
}

public function showSettings(User $user){

Expand Down
3 changes: 2 additions & 1 deletion database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
'subcategory_id' => rand(1, 7),
'name' => $faker->unique()->word,
'description' => $faker->text(800),
'price' => $faker->randomFloat(2, 150, 900)
'price' => $faker->randomFloat(2, 150, 900),
'quantity' => rand(0, 15)
];
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function up()
$table->string('description', 50)->nullable();
$table->string('image')->default('missing.png');
$table->float('price')->unsigned();
$table->integer('quantity')->unsigned()->nullable();
$table->boolean('recommended')->default(false);
$table->timestamps();

Expand Down
12 changes: 8 additions & 4 deletions database/seeds/ProductsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,35 @@ public function run()
factory(Product::class)->create([
'subcategory_id' => $subcategorySwords->id,
'name' => $productsToSwords[$i],
'image' => $swordsImages[$i]
'image' => $swordsImages[$i],
'quantity' => 5
]);
}

for ($i = 0; $i < count($productsToAxes); $i++) {
factory(Product::class)->create([
'subcategory_id' => $subcategoryAxes->id,
'name' => $productsToAxes[$i],
'image' => $axesImages[$i]
'image' => $axesImages[$i],
'quantity' => 8
]);
}

for ($i = 0; $i < count($productsToShields); $i++) {
factory(Product::class)->create([
'subcategory_id' => $subcategoryShields->id,
'name' => $productsToShields[$i],
'image' => $shieldsImages[$i]
'image' => $shieldsImages[$i],
'quantity' => 14
]);
}

for ($i = 0; $i < count($productsToHelmets); $i++) {
factory(Product::class)->create([
'subcategory_id' => $subcategoryHelmets->id,
'name' => $productsToHelmets[$i],
'image' => $helmetsImages[$i]
'image' => $helmetsImages[$i],
'quantity' => 3
]);
}

Expand Down
54 changes: 53 additions & 1 deletion public/css/app.css

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions public/js/add_to_cart_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(document).ready(function() {
$('.add_to_cart').on('click', function (e) {
e.preventDefault();

var url = $(this).attr("href");
var url = $(this).attr('href');

$.ajax({
type: 'POST',
Expand All @@ -14,15 +14,13 @@ $(document).ready(function() {
$('#added_to_cart').show();
$('#added_to_cart').delay(7000).fadeOut('slow');

var $badge = $('.badge'),
count = Number($badge.text());
var badge = $('.badge'),
count = Number(badge.text());

$badge.text(count + 1);
badge.text(count + 1);

$('#add_' + data.id).hide();
$('#remove_' + data.id).show();

localStorage.setItem('product_' + data.id, data.id);
}
})
});
Expand Down
13 changes: 0 additions & 13 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11252,19 +11252,6 @@ $('[data-toggle="tooltip"]').tooltip();

$(document).ready(function () {

if ($('#is_session').val() == '1') {
if (localStorage.length > 0) {
for (var i = 0; i < localStorage.length; i++) {

var id = localStorage.getItem(localStorage.key(i));
$('#add_' + id).hide();
$('#remove_' + id).show();
}
} else localStorage.clear();
} else {
localStorage.clear();
}

var orig_height = $('#review_text_area').height();
var new_height = 150;

Expand Down
Loading

0 comments on commit d1cd82d

Please sign in to comment.