简体   繁体   English

如何在laravel moltin购物车中添加多个同一个ID且具有不同尺寸的商品?

[英]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. 我正在尝试使用Moltin购物车添加不止一种具有相同ID但尺寸不同的产品。 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. 我用谷歌搜索了一个解决方案,但是我发现发生这种情况是因为将相同的ID传递给Cart::insert()方法。

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? 我该怎么做呢?

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. 我从未使用过Moltin购物车包,但是看它看起来像是使用id字段和options数组字段的组合来构建商品标识符的代码 Therefore, if the id is the same, but the options are different, it should insert two different items in your cart. 因此,如果id相同,但options不同,则应在购物车中插入两个不同的项目。

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'
    )
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM