Skip to content

Instantly share code, notes, and snippets.

@phillipmadsen
Created October 11, 2016 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipmadsen/37d506a99a9120fb27be7763e5717bf6 to your computer and use it in GitHub Desktop.
Save phillipmadsen/37d506a99a9120fb27be7763e5717bf6 to your computer and use it in GitHub Desktop.
New Sentinel Cart Controller and helper file for the navigation cart
<?php
namespace Ecommerce;
use App\Models\Cart;
use Illuminate\Database\Eloquent\Collection;
use Sentinel;
use Session;
class helperFunctions
{
public static function getPageInfo(&$cart,&$total)
{
if (Sentinel::check()) {
$cart = Sentinel::getUser()->cart;
} else {
$cart = new Collection;
if (Session::has('cart')) {
foreach (Session::get('cart') as $item) {
$elem = new Cart;
$elem->product_id = $item['product_id'];
$elem->amount = $item['quantity'];
if (isset($item['options'])) {
$elem->options = $item['options'];
}
$cart->add($elem);
}
}
}
$total = 0;
foreach ($cart as $item) {
$total += $item->product->price*$item->amount;
}
}
}
<?php
namespace App\Http\Controllers;
use App;
use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\OptionValue;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\Payment;
use App\Models\Product;
use App\Models\Section;
use App\Models\User;
use App\Models\UserInfo;
use Illuminate\Http\Request;
use Sentinel;
use Session;
use \Ecommerce\helperFunctions;
use \Illuminate\Database\Eloquent\Collection;
class CartController extends Controller
{
public function __construct()
{
$this->middleware('sentinel.auth', ['except' => [
'index',
'add',
'remove',
'clear'
]]);
}
public function index()
{
if (Sentinel::check()) {
//$user = Sentinel::getUser();
$cart = $user->cart;
} else {
$cart = new Collection;
if (Session::has('cart')) {
foreach (Session::get('cart') as $item) {
$elem = new Cart;
$elem->product_id = $item['product_id'];
$elem->amount = $item['quantity'];
if (isset($item['options'])) {
$elem->options = $item['options'];
}
$cart->add($elem);
}
}
}
$total = 0;
$options = new Collection;
foreach ($cart as $item) {
$total += $item->product->price * $item->amount;
if ($item->options) {
$values = explode(',', $item->options);
foreach ($values as $value) {
$options->add(OptionValue::find($value));
}
}
}
return view('frontend.shop.cart', compact('total', 'cart', 'options'));
}
public function add($product_id, Request $request)
{
$pid = Product::findOrFail($product_id)->id;
// dd($pid);
if ((Product::find($pid)->quantity - $request->quantity) < 0) {
return Redirect()->back()->with([
'flash_message' => 'Out of Stock',
'flash-warning' => true
]);
}
/**
* Check If the user is a guest , if so store the cart in a session
*/
if (Sentinel::check()) {
$exists = 0;
/**
* Check if the product already exists in the cart, if so increment the quantity
*/
if (Session::has('cart')) {
// dd(Session::all());
foreach (Session::get('cart') as $key => $cart) {
if ($cart['product_id'] == $product_id) {
$cart['quantity'] += $request->quantity;
if ($cart['quantity'] <= 0) {
$cart['quantity'] = 1;
}
Session::forget('cart.' . $key);
if ($request->options) {
Session::push('cart', [
'product_id' => $product_id,
'quantity' => $cart['quantity'],
'options' => implode(',', $request->options)
]);
} else {
Session::push('cart', [
'product_id' => $product_id,
'quantity' => $cart['quantity']
]);
}
$exists = 1;
break;
}
}
}
/**
* If the product is not in the cart , add a new one
*/
if (!$exists) {
if ($request->options) {
Session::push('cart', [
'product_id' => $product_id,
'quantity' => $request->quantity,
'options' => implode(',', $request->options)
]);
} else {
Session::push('cart', [
'product_id' => $product_id,
'quantity' => $request->quantity
]);
}
}
}
/**
* If the user is logged in , store the cart in the database
*/
else {
// old // if (count($cart = Cart::whereProduct_idAndUser_id($product_id, Sentinel::getUser()->getUserId()->first())) {
if (count($cart = Cart::whereProduct_idAndUser_id($product_id, Sentinel::findById()))) {
$request->quantity ? $cart->amount += $request->quantity : $cart->amount += 1;
if ($cart->amount <= 0) {
$cart->amount = 1;
}
$cart->save();
} else {
$cart = new Cart;
$cart->user_id = Sentinel::findById();
$cart->product_id = $pid;
if ($request->options) {
$cart->options = implode(',', $request->options);
}
$request->quantity ? $cart->amount = $request->quantity : $cart->amount = 1;
if ($cart->amount <= 0) {
$cart->amount = 1;
}
$cart->save();
}
}
return \Redirect()->back()->with([
'flash_message' => 'Added to Cart !'
]);
}
public function remove($product_id)
{
if (Sentinel::check()) {
// Cart::whereProduct_idAndUser_id($product_id, $user_id = Sentinel::getUser()->getUserId()->delete());
Cart::whereProduct_idAndUser_id($product_id, $user_id = Sentinel::findById()->delete());
} else {
foreach (Session::get('cart') as $key => $item) {
if ($item['product_id'] == $product_id) {
Session::forget('cart.' . $key);
break;
}
}
}
return \Redirect()->back()->with([
'flash_message' => 'Product Removed From Cart !',
'flash-warning' => true
]);
}
public function clear()
{
if (Sentinel::check()) {
Sentinel::getUser()->cart()->delete();
} else {
Session::flush('cart');
}
return \Redirect()->back();
}
public function payment(Request $request)
{
$userCart = Sentinel::getUser()->cart;
$total = 0;
foreach ($userCart as $item) {
$total += ($item->product->price) * ($item->amount);
}
if (Session::has('coupon')) {
$total = $total - (($total * Session::get('coupon.discount')) / 100);
}
$billing = App::make('App\Ecommerce\Billing\BillingInterface');
$billing->charge([
'email' => Sentinel::getUser()->email,
'token' => $request->stripeToken,
'amount' => $total
]);
$order = Order::create([
'user_id' => Sentinel::findById(),
'amount' => $total,
'status' => 'Processing',
'firstname' => Session::get('shipping.firstname'),
'lastname' => Session::get('shipping.lastname'),
'shipping_address' => Session::get('shipping.address'),
'shipping_city' => Session::get('shipping.city'),
'shipping_zipcode' => Session::get('shipping.zipcode'),
'shipping_country' => Session::get('shipping.country'),
'payment_method' => 'Credit Card',
'phone' => Session::get('shipping.phone'),
'coupon_id' => Session::get('coupon.id')
]);
Session::forget('coupon');
foreach ($userCart as $item) {
OrderProduct::create([
'order_id' => $order->id,
'product_id' => $item->product_id,
'amount' => $item->amount,
'options' => $item->options
]);
$item->product->quantity -= $item->amount;
$item->product->save();
}
$this->clear();
return \Redirect('/dashboard')->with([
'alert-success' => 'Payment success'
]);
}
public function shipping()
{
if (!Sentinel::getUser()->cart->count()) {
return \Redirect()->back()->with([
'flash_message' => 'Your Cart is empty !',
'flash-warning' => true
]);
} else {
$user = Sentinel::getUser();
helperFunctions::getPageInfo($cart, $total);
return view('frontend.account.shipping', compact( 'total', 'cart', 'user'));
}
}
public function storeShippingInformation(Request $request)
{
$this->validate($request, [
'firstname' => 'required',
'lastname' => 'required',
'phone' => 'required',
'address' => 'required',
'city' => 'required',
'country' => 'required'
]);
$user = Sentinel::findUserById();
Session::put('shipping', $request->except('_token'));
$userInfo = userInfo::where('user_id', $user()->id);
$userInfo->update([
'firstname' => $request->firstname,
'lastname' => $request->lastname,
'address' => $request->address,
'city' => $request->city,
'country' => $request->country,
'zipcode' => $request->zipcode
]);
helperFunctions::getPageInfo($cart, $total);
$publishable_key = Payment::first()->stripe_publishable_key;
return view('frontend.ecom.payment', compact('total', 'cart', 'publishable_key'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment