Eshop-Laravel/app/Http/Controllers/ShopController.php
2019-10-18 12:25:37 +03:00

111 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Categorie;
use App\Product;
use resources;
use Cart;
use Session;
use App\Order;
use App\User;
use App\sales_role;
class ShopController extends MainController
{
public function categories(){
self::$data['title'] .= 'Shop categories';
self::$data['categories'] = Categorie::all()->toArray();
return view('shop.categories' ,self::$data);
}
public function product($cat_url){
// dd($cat_url);
$products = Product::getProducts($cat_url , self::$data);
return view('shop.products', self::$data);
}
public function item($cat_url,$prd_url){
if($product = Product::where('url','=',$prd_url)->first()){
self::$data['product'] = $product->toArray();
self::$data['title'] .= $product['title'];
// dd(self::$data['product']);
return view('shop.item' , self::$data);
}else{
abort(404);
}
}
public function addToCart(Request $request){
Product::addToCart($request);
}
public function checkOut(){
$cart = Cart::getContent() -> toArray();
self::$data['title'] .= 'Check-out';
self::$data['cart'] = $cart;
return view('shop.checkout' , self::$data);
}
public function clearCart(){
Session::flash('sm' ,'Cart Cleared');
Cart::clear();
return redirect('shop/checkout');
}
public function removeItem($id){
Cart::remove($id);
return redirect('shop/checkout');
}
// public function prePlaceOrder(){
// self::$data['title'] .= 'Wait!';
// self::$data['cart'] = Cart::getContent()->toArray();
// if (self::$data['user'] = User::getUserInfo() ) {
// return view('shop.pre-place-order' , self::$data);
// }else{
// return redirect('user/login');
// }
// // ;
// // dd(self::$data);
// }
static public function prePlaceOrder(){
if( Cart::isEmpty() ){
return redirect('shop');
}else{
if( ! Session::has('user_id') ){
return redirect('user/login?rt=shop/checkout');
}else{
// Order::saveNew();
self::$data['title'] .= 'Wait!';
self::$data['cart'] = Cart::getContent()->toArray();
self::$data['user'] = User::getUserInfo();
return view('shop.pre-place-order' , self::$data);
}
}
}
public function placeOrder(){
if (Cart::isEmpty() ) {
return redirect('shop');
}else {
if (!Session::has('user_id') ) {
return redirect('user/login?rt=shop/checkout');
}else {
Order::save_new();
return redirect('shop');
}
}
}
}