Still searching for a solution to this if anyone is willing to offer help/advice.
Dec 17, 2014
16
Level 51
Moltin/Cart package, adding more then 1 item
Hi all,
Using the moltin/cart package I have this add to cart method which works fine:
public function add() {
$input = Input::all();
$id = Input::get('pid');
$item = Cart::item($id);
// If the result if false then the items was not found
// in the cart and you need to create a new entry
if ($item === false)
{
$product = array(
'id' => $id,
'name' => $input['product_name'],
'price' => $input['price'],
'quantity' => $input['qty'],
'image' => $input['image'],
'path' => $input['path'],
'description' => $input['description'],
'stock' => $input['stock'],
'delivery' => $input['delivery'],
'colour' => $input['colour'],
'size' => $input['size']
);
Cart::insert($product);
}
else
{
// If it was found you just need to update the quantity
$item->quantity += (int) $input['qty'];
$product = $item;
}
return Redirect::to('cart');
}
But I then have a method to pass to Stripe to process the payment, and within this "messy" method i need it to save the item(s) to the database but it only ever saves one item even if there is more than one item in the cart.
Stripe Method:
public function stripeSuccess()
{
$price = str_replace('.', '', money_format("%i", Cart::total(true) + Config::get('moltincart.postage')));
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
if (isset(Auth::user()->email)) {
$email = Auth::user()->email;
} else {
$email = $_POST['stripeEmail'];
}
// Captcha User details
$rules = array(
'name' => 'required',
'phone' => 'required',
'address' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// Validation fails
if ($validator->fails()) {
return Redirect::back()->withErrors($validator)->withInput();
} else {
$name = Input::get('name');
$emaile = Input::get('email');
$phone = Input::get('phone');
$address = Input::get('address');
}
foreach(Cart::contents() as $items) {
$product = $items->name;
$colour = $items->colour;
$size = $items->size;
}
// Create a Customer
$customer = Stripe_Customer::create(array(
"card" => $token,
"description" => $product. $colour . $size ,
"email" => "Customer Name: " . $name . " Email: " . $email . ""
));
// Charge the Customer instead of the card
$charge = Stripe_Charge::create(array(
"amount" => $price,
"currency" => "gbp",
"customer" => $customer->id,
));
$uemail = Auth::id();
if (isset($uemail)) {
$user = User::find($uemail);
$user->stripe_active = 1;
$user->billing_id = $customer->id;
$user->stripe_id = $token;
$user->update();
}
// Grab cart contents
$items = Cart::contents();
foreach ($items as $item) {
// This is the foreach which I need to pass to email
$data = array(
'stripe' => $token,
'product' => Session::get('pName'),
'size' => $item->size,
'colour' => $item->colour,
'price' => $item->price,
'name' => $name,
'email' => $emaile,
'phone' => $phone,
'address' => $address
);
}
if (Auth::user()) {
$userid = Auth::id();
} else {
$userid = '0';
}
// Create new order within the database, this is where I need to store multiple items if they are within the cart to the database.
$order = new Orders;
$order->order_id = mt_rand(110001, 999999);
foreach(Cart::contents() as $items) {
$order->product = $items->name;
$order->size = $items->colour;
$order->colour = $items->size;
}
$order->price = $data['price'];
$order->name = $data['name'];
if (Auth::user()) {
$order->email = Auth::user()->email;
} else {
$order->email = $data['email'];
}
$order->phone = $data['phone'];
$order->address = $data['address'];
$order->date = date('Y-m-d');
$order->user_id = $userid;
$order->save();
// Destory cart, no longer required, redirect back to home page
Cart::destroy();
return Redirect::to('/')->with('flash_success', 'Thank you for your purchase, we will contact you in 24 hours to confirm your purchase.');
}
Can someone help determine what I've done wrong and how I can save within the database all items within the cart whether it's just one item or multiple.
Thanks in advance.
Level 16
How about something like.
$Order_id = mt_rand(110001, 999999);
foreach(Cart::contents() as $items) {
$order = new Orders;
$order->order_id = $Order_id;
$order->product = $items->name;
$order->size = $items->colour;
$order->colour = $items->size;
$order->price = $data['price'];
$order->name = $data['name'];
$order->email = Auth::user()?Auth::user()->email : $order->email = $data['email'];
$order->phone = $data['phone'];
$order->address = $data['address'];
$order->date = date('Y-m-d');
$order->user_id = $userid;
$order->save();
}
1 like
Please or to participate in this conversation.