diff --git a/app/Cart.php b/app/Cart.php index 50b3435..4c30629 100644 --- a/app/Cart.php +++ b/app/Cart.php @@ -2,6 +2,8 @@ namespace App; +use App\Models\Publics\ProductsModel; + /** * This class manage shopping cart of users * @@ -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() @@ -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'])) { @@ -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; + } + } + } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 1838970..3c0731e 100755 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -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 @@ -18,7 +17,7 @@ class Controller extends BaseController DispatchesJobs, ValidatesRequests; - protected $products = []; + protected $products; /* * Get all products from cart @@ -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(); } } diff --git a/app/Http/Controllers/Publics/CartController.php b/app/Http/Controllers/Publics/CartController.php index aff5ae1..fc26248 100644 --- a/app/Http/Controllers/Publics/CartController.php +++ b/app/Http/Controllers/Publics/CartController.php @@ -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']); + } + } diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 6807a2b..be3922a 100755 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -2,6 +2,20 @@ - + + file:/var/www/html/laravel/resources/views/publics/products.blade.php + file:/var/www/html/laravel/resources/lang/bg/public_pages.php + file:/var/www/html/laravel/resources/lang/en/public_pages.php + file:/var/www/html/laravel/public/css/public.css + file:/var/www/html/laravel/resources/views/layouts/app_public.blade.php + file:/var/www/html/laravel/public/js/public.js + file:/var/www/html/laravel/app/Cart.php + file:/var/www/html/laravel/app/Http/Controllers/Controller.php + file:/var/www/html/laravel/resources/views/publics/preview.blade.php + file:/var/www/html/laravel/routes/web.php + file:/var/www/html/laravel/app/Http/Controllers/Publics/CartController.php + file:/var/www/html/laravel/app/Models/Publics/ProductsModel.php + file:/var/www/html/laravel/resources/views/publics/home.blade.php + diff --git a/public/css/public.css b/public/css/public.css index d57bae2..9692c74 100644 --- a/public/css/public.css +++ b/public/css/public.css @@ -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;} @@ -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;} diff --git a/public/js/public.js b/public/js/public.js index 075e20e..993181b 100644 --- a/public/js/public.js +++ b/public/js/public.js @@ -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 @@ -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) { @@ -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 } \ No newline at end of file diff --git a/resources/views/layouts/app_public.blade.php b/resources/views/layouts/app_public.blade.php index efe9a75..08e1172 100755 --- a/resources/views/layouts/app_public.blade.php +++ b/resources/views/layouts/app_public.blade.php @@ -40,56 +40,54 @@
- - - @php - if(!empty($cartProducts)) { - @endphp - {{count($cartProducts)}} - @php - } - @endphp + + {{!empty($cartProducts) ? count($cartProducts): 0}}
- @php - $sum = 0; - if(!empty($cartProducts)) { - $sum = 0; - @endphp -
-
- - -
- {{__('public_pages.subtotal')}} - {{$sum}} -
+
+ {{__('public_pages.payment')}}
- {{__('public_pages.payment')}}
+ @php + } + @endphp
- @php - } - @endphp @@ -229,7 +227,9 @@ @endif diff --git a/resources/views/publics/cartHtml.php b/resources/views/publics/cartHtml.php new file mode 100644 index 0000000..fc46ece --- /dev/null +++ b/resources/views/publics/cartHtml.php @@ -0,0 +1,31 @@ +
+
+ + +
+ + +
+
+ +
+
\ No newline at end of file diff --git a/routes/web.php b/routes/web.php index d77ed01..20469dd 100755 --- a/routes/web.php +++ b/routes/web.php @@ -39,6 +39,10 @@ // add product to cart from add button (ajax) Route::post('addProduct', 'Publics\\CartController@addProduct'); +// get products and cart html +Route::post('getGartProducts', 'Publics\\CartController@renderCartProductsWithHtml'); +// get products and cart html +Route::post('removeProductQuantity', 'Publics\\CartController@removeProductQuantity'); /* Administration Routes */