Skip to content

Commit

Permalink
Add remove function for shopping cart products
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiril Kirkov committed Oct 10, 2017
1 parent 59b82e2 commit 1688f0c
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 73 deletions.
63 changes: 57 additions & 6 deletions app/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App;

use App\Models\Publics\ProductsModel;

/**
* This class manage shopping cart of users
*
Expand All @@ -14,26 +16,38 @@ class Cart
*/

private $cookieExpTime = 2678400;
public $countProducts = 0;

public function addProduct($id, $quantity)
{
$productsModel = new ProductsModel();
if (!isset($_SESSION['laraCart'])) {
$_SESSION['laraCart'] = array();
}
for ($i = 0; $i <= $quantity; $i++) {
for ($i = 1; $i <= $quantity; $i++) {
$_SESSION['laraCart'][] = (int) $id;
}
setcookie('laraCart', serialize($_SESSION['laraCart']), $this->cookieExpTime);
}

public function removeProductQuantity()
public function removeProductQuantity($id)
{

if (($key = array_search($id, $_SESSION['laraCart'])) !== false) {
unset($_SESSION['laraCart'][$key]);
}
}

public function removeProduct()
public function removeProduct($id)
{

$count = count(array_keys($_SESSION['laraCart'], $id));
$i = 1;
do {
if (($key = array_search($id, $_SESSION['laraCart'])) !== false) {
unset($_SESSION['laraCart'][$key]);
}
$i++;
} while ($i <= $count);
setcookie('laraCart', serialize($_SESSION['laraCart']), $this->cookieExpTime);
}

public function clearCart()
Expand All @@ -42,7 +56,7 @@ public function clearCart()
setcookie('laraCart', null, -1, '/');
}

public function getCartProducts()
private function getCartProductsIds()
{
$products = array();
if (!isset($_SESSION['laraCart']) || empty($_SESSION['laraCart'])) {
Expand All @@ -55,4 +69,41 @@ public function getCartProducts()
return $products;
}

public function getCartProducts()
{
$productsModel = new ProductsModel();

$products_ids = $this->getCartProductsIds();
$unique_ids = array_unique($products_ids);

$products = [];
if (!empty($products_ids)) {
$products = $productsModel->getProductsWithIds($unique_ids);
foreach ($products as &$product) {
$counts = array_count_values($products_ids);
$numAddedToCart = $counts[$product->id];
$product->num_added = $numAddedToCart;
}
}
$this->countProducts = count($products);
return $products;
}

public function getCartHtmlWithProducts()
{
$products = $this->getCartProducts();

$sum = 0;
if (!empty($products)) {
$sum = 0;
ob_start();
include '../resources/views/publics/cartHtml.php';
$content = ob_get_contents();
ob_end_clean();
return $content;
} else {
return $products;
}
}

}
19 changes: 2 additions & 17 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Models\Publics\ProductsModel;
use App\Cart;

class Controller extends BaseController
Expand All @@ -18,7 +17,7 @@ class Controller extends BaseController
DispatchesJobs,
ValidatesRequests;

protected $products = [];
protected $products;

/*
* Get all products from cart
Expand All @@ -29,21 +28,7 @@ class Controller extends BaseController
public function __construct()
{
$cart = new Cart();
$productsModel = new ProductsModel();

$products_ids = $cart->getCartProducts();
$unique_ids = array_unique($products_ids);

$products = [];
if (!empty($products_ids)) {
$products = $productsModel->getProductsWithIds($unique_ids);
foreach ($products as &$product) {
$counts = array_count_values($products_ids);
$numAddedToCart = $counts[$product->id];
$product->num_added = $numAddedToCart;
}
}
return $this->products = $products;
$this->products = $cart->getCartProducts();
}

}
22 changes: 21 additions & 1 deletion app/Http/Controllers/Publics/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,28 @@ public function addProduct(Request $request)
$quantity = (int) $post['quantity'];
if ($quantity == 0) {
$quantity = 1;
}
}
$this->cart->addProduct($post['id'], $quantity);
}

public function renderCartProductsWithHtml(Request $request)
{
if (!$request->ajax()) {
abort(404);
}
echo json_encode(array(
'html' => $this->cart->getCartHtmlWithProducts(),
'num_products' => $this->cart->countProducts
));
}

public function removeProductQuantity(Request $request)
{
if (!$request->ajax()) {
abort(404);
}
$post = $request->all();
$this->cart->removeProductQuantity($post['id']);
}

}
16 changes: 15 additions & 1 deletion nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
<group>
<file>file:/var/www/html/laravel/resources/views/publics/products.blade.php</file>
<file>file:/var/www/html/laravel/resources/lang/bg/public_pages.php</file>
<file>file:/var/www/html/laravel/resources/lang/en/public_pages.php</file>
<file>file:/var/www/html/laravel/public/css/public.css</file>
<file>file:/var/www/html/laravel/resources/views/layouts/app_public.blade.php</file>
<file>file:/var/www/html/laravel/public/js/public.js</file>
<file>file:/var/www/html/laravel/app/Cart.php</file>
<file>file:/var/www/html/laravel/app/Http/Controllers/Controller.php</file>
<file>file:/var/www/html/laravel/resources/views/publics/preview.blade.php</file>
<file>file:/var/www/html/laravel/routes/web.php</file>
<file>file:/var/www/html/laravel/app/Http/Controllers/Publics/CartController.php</file>
<file>file:/var/www/html/laravel/app/Models/Publics/ProductsModel.php</file>
<file>file:/var/www/html/laravel/resources/views/publics/home.blade.php</file>
</group>
</open-files>
</project-private>
6 changes: 4 additions & 2 deletions public/css/public.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ footer .copy-rights {background-color: #212331; color:#616f7a; padding: 10px 0;}
.cart-products-fast-view .content {position: relative; padding: 10px; padding-top: 30px; color:#ffffff;}
.cart-products-fast-view .content .close-me {position: absolute; color:#ffffff; right:5px; top:-1px; font-size:20px;}
.cart-products-fast-view ul {margin: 0; padding: 0; list-style: none; padding-bottom: 10px; margin-bottom: 10px; border-bottom:1px solid #51546c;}
.cart-products-fast-view ul li { margin-bottom: 10px; }
.cart-products-fast-view ul li a {color:#ffffff;}
.cart-products-fast-view ul li { margin-bottom: 10px; position: relative;}
.cart-products-fast-view ul li .link {color:#ffffff;}
.cart-products-fast-view ul li img {width:50px; margin-right: 10px; float:left;}
.cart-products-fast-view ul li .name {float:left; width:260px; font-size: 12px;}
.cart-products-fast-view ul li .price {float:left; width:260px; font-size: 12px; color:#99a9b5;}
Expand All @@ -128,6 +128,8 @@ footer .copy-rights {background-color: #212331; color:#616f7a; padding: 10px 0;}
.cart-products-fast-view .pay-sum .sum {float:right;}
.cart-products-fast-view .buy-now-fast-cart {background-color: #8ec63f; border:1px solid #74a62e; display: block; padding: 10px; text-align: center; color:#ffffff; text-transform: uppercase;}
.cart-products-fast-view .buy-now-fast-cart:hover {text-decoration: none;}
.cart-products-fast-view .removeQantity {position: absolute; right:5px; top:5px; color:#fff;}
.cart-products-fast-view ul li .link:hover .price {color:#00bcd4; text-decoration: none; -moz-transition: all .2s ease-in; -o-transition: all .2s ease-in; -webkit-transition: all .2s ease-in; transition: all .2s ease-in;}

.alert{text-align: center;}
.alert.alert-danger {background-color: #f55a4e; border-radius: 3px; box-shadow: 0 12px 20px -10px rgba(244, 67, 54, 0.28), 0 4px 20px 0 rgba(0, 0, 0, 0.12), 0 7px 8px -5px rgba(244, 67, 54, 0.2); color: #ffffff; border-color: transparent;}
Expand Down
38 changes: 36 additions & 2 deletions public/js/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $('.buy-now').click(function () {
$('#modalBuyBtn').modal('show');
var product_id = $(this).data('product-id');
addProduct(product_id);
renderCartProducts();
});
/*
* Show cart products in fast view
Expand All @@ -56,9 +57,9 @@ $('.cart-button').hover(function () {
/*
* Hide cart products in fast view
*/
$('.cart-products-fast-view .close-me').click(function () {
function closeFastCartView() {
$('.cart-products-fast-view').fadeOut(200);
});
}
function checkScroll() {
if ($(this).scrollTop() > 80) {
if (xsMode() === false) {
Expand Down Expand Up @@ -102,4 +103,37 @@ function addProduct(id) {
}).done(function (data) {

});
}
/*
* Render cart products
*/
function renderCartProducts() {
$('.cart-fast-view-container').empty();
$.ajax({
type: 'POST',
url: urls.getProducts,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}).done(function (data) {
var obj = JSON.parse(data);
$('.cart-fast-view-container').append(obj.html);
$('.header .user .badge').empty().append(obj.num_products);
});
}
/*
*
*/
function removeQuantity(id) {
$.ajax({
type: 'POST',
url: urls.removeProductQuantity,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {id: id}
}).done(function (data) {
renderCartProducts();
});
$('.cart-products-fast-view').show(); // lets show again cart box
}
88 changes: 44 additions & 44 deletions resources/views/layouts/app_public.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,54 @@
</div>
<div class="col-sm-3 col-md-2">
<div class="user">
<a href="" class="login">
{{__('public_pages.login')}}
<i class="fa fa-sign-in" aria-hidden="true"></i>
</a>
<a href="javascript:void(0);" class="cart-button">
<i class="fa fa-shopping-cart" aria-hidden="true"></i>
@php
if(!empty($cartProducts)) {
@endphp
<span class="badge">{{count($cartProducts)}}</span>
@php
}
@endphp
<i class="fa fa-shopping-cart" aria-hidden="true"></i>
<span class="badge">{{!empty($cartProducts) ? count($cartProducts): 0}}</span>
</a>
</div>
@php
$sum = 0;
if(!empty($cartProducts)) {
$sum = 0;
@endphp
<div class="cart-products-fast-view">
<div class="content">
<a href="javascript:void(0);" class="close-me"><i class="fa fa-times" aria-hidden="true"></i></a>
<ul>
@foreach($cartProducts as $cartProduct)
@php
$sum += $cartProduct->num_added * (int)$cartProduct->price;
@endphp
<li>
<a href="{{lang_url($cartProduct->url)}}"> <img src="{{asset('storage/'.$cartProduct->image)}}" alt="">
<span class="name">{{$cartProduct->name}}</span>
<span class="price">
{{$cartProduct->num_added}} x {{$cartProduct->price}}
</span>
</a>
<div class="cart-fast-view-container">
@php
$sum = 0;
if(!empty($cartProducts)) {
$sum = 0;
@endphp
<div class="cart-products-fast-view">
<div class="content">
<a href="javascript:void(0);" class="close-me" onclick="closeFastCartView()">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
<ul>
@foreach($cartProducts as $cartProduct)
@php
$sum += $cartProduct->num_added * (int)$cartProduct->price;
@endphp
<li>
<a href="{{lang_url($cartProduct->url)}}" class="link">
<img src="{{asset('storage/'.$cartProduct->image)}}" alt="">
<span class="name">{{$cartProduct->name}}</span>
<span class="price">
{{$cartProduct->num_added}} x {{$cartProduct->price}}
</span>
</a>
<a href="javascript:void(0);" class="removeQantity" onclick="removeQuantity({{$cartProduct->id}})">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
<div class="clearfix"></div>
</li>
@endforeach
</ul>
<div class="pay-sum">
<span class="text">{{__('public_pages.subtotal')}}</span>
<span class="sum">{{$sum}}</span>
<div class="clearfix"></div>
</li>
@endforeach
</ul>
<div class="pay-sum">
<span class="text">{{__('public_pages.subtotal')}}</span>
<span class="sum">{{$sum}}</span>
<div class="clearfix"></div>
</div>
<a href="{{lang_url('checkout')}}" class="buy-now-fast-cart">{{__('public_pages.payment')}}</a>
</div>
<a href="{{lang_url('checkout')}}" class="buy-now-fast-cart">{{__('public_pages.payment')}}</a>
</div>
@php
}
@endphp
</div>
@php
}
@endphp
</div>
</div>
</div>
Expand Down Expand Up @@ -229,7 +227,9 @@
@endif
<script>
var urls = {
addProduct: "{{ url('addProduct') }}"
addProduct: "{{ url('addProduct') }}",
removeProductQuantity: "{{ url('removeProductQuantity') }}",
getProducts: "{{ url('getGartProducts') }}"
};
</script>
<script src="{{ asset('js/jquery-3.2.1.min.js') }}" type="text/javascript"></script>
Expand Down
Loading

0 comments on commit 1688f0c

Please sign in to comment.