Skip to content

Commit

Permalink
All functionalities connected with shopping cart was added. All based…
Browse files Browse the repository at this point in the history
… on AJAX calls, to add and delete from cart, dynamic displaying number of products in cart. Improvements in appearance and also refactoring
  • Loading branch information
vilgefortzz committed Apr 23, 2017
1 parent f6b22aa commit 66b89fa
Show file tree
Hide file tree
Showing 43 changed files with 564 additions and 294 deletions.
78 changes: 73 additions & 5 deletions app/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,81 @@

class Cart extends Model
{
public function user()
public $products = array();
public $totalQuantity = 0;
public $totalPrice = 0;

/**
* Cart constructor.
*/
public function __construct($oldCart)
{
return $this->belongsTo('App\User');
if ($oldCart){
$this->products = $oldCart->products;
$this->totalQuantity = $oldCart->totalQuantity;
$this->totalPrice = $oldCart->totalPrice;
}
}

public function items()
{
return $this->hasMany('App\Item');
public function addProduct($product){

$storedProduct = ['id' => 0, 'quantity' => 0, 'price' => $product->price, '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'];
$this->products[$product->id] = $storedProduct;
$this->totalQuantity++;
$this->totalPrice += $product->price;
}
}
else{

/**
* Add first product to cart
*/
$storedProduct['id'] = $product->id;
$storedProduct['quantity']++;
$storedProduct['price'] = $product->price * $storedProduct['quantity'];
$this->products[$product->id] = $storedProduct;
$this->totalQuantity++;
$this->totalPrice += $product->price;
}
}

public function deleteProduct($product){

if ($this->products){

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

public function setProductsQuantity($products_id, $newQuantity){

foreach ($products_id as $product_id){
if (array_key_exists($product_id, $this->products)){
$this->products[$product_id]['quantity'] = $newQuantity;
}
}
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers;

use App\Cart;
use App\Item;
use App\Product;
use Illuminate\Http\Request;
use Session;

class CartController extends Controller
{

public function showProducts() {

if (!Session::has('cart')){
return view('cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('cart', ['products' => $cart->products, 'totalPrice' => $cart->totalPrice, 'totalQuantity' => $cart->totalQuantity]);

}

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

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

$cart = new Cart($oldCart);

$cart->addProduct($product);

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

/*
* To have essential data in ajax request
*/
return response()->json($product);
}

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

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

$cart = new Cart($oldCart);

$cart->deleteProduct($product);

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

return response()->json($product);
}
}
6 changes: 0 additions & 6 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public function showSettings(User $user){

}

public function showItemsInCart(User $user){

$items = $user->cart->items;
return view('users.cart', compact('items'));
}

public function updateName(User $user, Request $request){

/*
Expand Down
18 changes: 0 additions & 18 deletions app/Item.php

This file was deleted.

5 changes: 5 additions & 0 deletions app/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function subcategory()
{
return $this->belongsTo('App\Subcategory');
}

public function items()
{
return $this->hasMany('App\Item');
}
}
5 changes: 0 additions & 5 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ class User extends Authenticatable
'password', 'remember_token',
];

public function cart()
{
return $this->hasOne('App\Cart');
}

public function orders()
{
return $this->hasMany('App\Order');
Expand Down
4 changes: 2 additions & 2 deletions config/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
|
*/

'lifetime' => 120,
'lifetime' => 3600,

'expire_on_close' => false,
'expire_on_close' => true,

/*
|--------------------------------------------------------------------------
Expand Down
12 changes: 0 additions & 12 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
];
});

$factory->define(App\Cart::class, function (Faker\Generator $faker) {

return [];
});

$factory->define(App\Category::class, function (Faker\Generator $faker) {

return [
Expand All @@ -52,10 +47,3 @@
'price' => $faker->randomFloat(2, 150, 900)
];
});

$factory->define(App\Item::class, function (Faker\Generator $faker) {

return [
'product_id' => rand(1, 20)
];
});
35 changes: 0 additions & 35 deletions database/migrations/2017_03_05_194709_create_carts_table.php

This file was deleted.

38 changes: 0 additions & 38 deletions database/migrations/2017_03_05_202645_create_items_table.php

This file was deleted.

1 change: 0 additions & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ public function run()
$this->call(CategoriesTableSeeder::class);
$this->call(SubcategoriesTableSeeder::class);
$this->call(ProductsTableSeeder::class);
$this->call(ItemsTableSeeder::class);
}
}
23 changes: 0 additions & 23 deletions database/seeds/ItemsTableSeeder.php

This file was deleted.

Loading

0 comments on commit 66b89fa

Please sign in to comment.