Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

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.

0 likes
16 replies
theUnforgiven's avatar

Still searching for a solution to this if anyone is willing to offer help/advice.

fraserk's avatar

Trying to figure out what you trying to do. So basically each Cart item will belongs to an Order?

theUnforgiven's avatar

If i had 3 items in one visit/session I can;t store these 3 items in my database it only stores one.

fraserk's avatar
fraserk
Best Answer
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
theUnforgiven's avatar

So putting new Orders; inside the foreach is that what your saying?

fraserk's avatar

Yes, It will create a new record for each item.

theUnforgiven's avatar

Yes but I need it to have the same generate order number with this line: $order->order_id = mt_rand(110001, 999999);

So my client know that order is the same order.

fraserk's avatar

It should, that's why I left it out the foreach loop.

stickman373's avatar

I think you need an additional table....something like order_items that would store the items associated with the order. Right now you are looping through the contents, but overwriting the previous products information because you are storing everything in the Orders table. A better approach is a secondary table with a relationship.

2 likes
theUnforgiven's avatar

@stickman373 I agree but in this case it's not quite possible, this solution does work just need the generated order id to be the same no matter how many items are ordered per visit.

fraserk's avatar

What line are you getting that error on?

theUnforgiven's avatar

Sorry my bad! didn't assign to a variable left it has $order->order_id my bad!

theUnforgiven's avatar

Kinda works but is overwriting the price and inputting the price for each item as the same even though they aren't

1 like
fraserk's avatar

try $items->price; to get the price from the cart.

1 like

Please or to participate in this conversation.