phpmysqllaravellaravel-4moltin

How to Add more than one products of same id with different sizes in laravel moltin cart?


I am trying to add more than one product having same ID but different sizes with Moltin cart. Problem here is, if I am trying to add the same product with different sizes into the Cart, it just increments the quantity instead of appending it. I googled for a solution but I found this happens because of passing the same ID into the Cart::insert() method.

Cart insert Method is:

Cart::insert(array(
    'id' => $product->id,
    'name' => $product->title,
    'price' => $product->price,
    'dimension'=>null,
    'unit'=>$product->unit,
    'quantity' => $quantity,
    'image' => $product->image,
    'tax' =>$product->taxvalue,
    'taxtype'=>$product->tax,
    'pincode' =>$pincode,
    'shippingfee'=>Session::get('shippingfee'),
    'retailerId' =>$retailerIdfromProductId
));  

I want to append a new product if the dimension is not null. How do I do this?


Solution

  • I've never used the Moltin cart package, but looking at the code it looks like it builds the item identifier using a combination of the id field and an options array field. Therefore, if the id is the same, but the options are different, it should insert two different items in your cart.

    Can you do something like this:

    // first item with no dimension
    Cart::insert(array(
        'id' => $product->id,
        'name' => $product->title,
        'price' => $product->price,
        'unit' => $product->unit,
        'quantity' => $quantity,
        'image' => $product->image,
        'tax' => $product->taxvalue,
        'taxtype' => $product->tax,
        'pincode' => $pincode,
        'shippingfee' => Session::get('shippingfee'),
        'retailerId' => $retailerIdfromProductId,
        'options' => array(
            'dimension' => null
        )
    ));
    
    // second item with 'M' dimension
    Cart::insert(array(
        'id' => $product->id,
        'name' => $product->title,
        'price' => $product->price,
        'unit' => $product->unit,
        'quantity' => $quantity,
        'image' => $product->image,
        'tax' => $product->taxvalue,
        'taxtype' => $product->tax,
        'pincode' => $pincode,
        'shippingfee' => Session::get('shippingfee'),
        'retailerId' => $retailerIdfromProductId,
        'options' => array(
            'dimension' => 'M'
        )
    ));