laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

How to add a custom (different size) product

Open Misagh90 opened this issue 5 years ago • 8 comments

I have some products which have a different size (orderable height & width) and the price will change by the size change but when I try to add two different sizes from a product in the cart, the quantity increase but I need to create a new row of that product with new size (for example a 3×4 sunlight wallpaper and a 2×3 sunlight wallpaper {in my cart: is 2 sunlight wallpaper with the last size that I add it}). in addition, a have normal products

public function Add($id){
            $product = Product::find($id);
            $id = $product->id;
            $name = $product->barcode;
            $order = $product->orderable;
            $price = $product->price;
            $image = $product->FImage;
            $code = $product->barcode;
            if (Auth::check()){
                $userId = Auth::id();
                Cart::session($userId)->add(array(
                  array(
                      'id' => $id,
                      'name' => $name,
                      'price' => $price,
                      'quantity' => 1,
                      'attributes' => array(
                        'image' => $image,
                        'width' => null,
                        'height' => null,
                        'code' => $code
                      )
                  ),
                ));
            }else{
                Cart::add(array(
                  array(
                      'id' => $id,
                      'name' => $name,
                      'price' => $price,
                      'quantity' => 1,
                      'attributes' => array(
                        'image' => $image,
                        'width' => null,
                        'height' => null,
                        'code' => $code
                      )
                  ),
                ));             
            }
            return redirect()->route('Cart');
        }

        public function AddOrderable(Request $request,$id){
            $p = Product::find($id);
            $id = $p->id;
            $name = $p->barcode;
            $priceperunit = $p->priceperunit;

            $width = $request->width;
            $height = $request->height;
            $count = $request->count;
            $side = $request->side;
            $mounting = $request->mounting;
            $baseprice = 50;


            if ($width >= 1 && $height >=1) {
                if ($mounting == 'wall') {
                    $price = (($width * $height * $count) * $priceperunit) + ($count *$baseprice);
                }else{
                    $price = (($width * $height * $count) * $priceperunit);
                }
            }elseif ($width>= 1 && $height<1) {
                if ($mounting == 'wall') {
                    $price = (($width * $count)*$priceperunit) + ($count *$baseprice);
                }else{
                    $price = (($width * $count)*$priceperunit);
                }
            }elseif ($width< 1 && $height>=1) {
                if ($mounting == 'wall') {
                    $price = (($height * $count)*$priceperunit) + ($count *$baseprice);
                }else{
                    $price = (($height * $count)*$priceperunit);
                }
            }elseif ($width < 1 && $height<1) {
                if ($mounting == 'wall') {
                    $price = ($count*$priceperunit) + ($count *$baseprice);
                }else{
                    $price = ($count*$priceperunit);
                }
            }

            $image = $p->FImage;
            $code = $p->barcode;
            if (Auth::check()){
                $userId = Auth::id();
                Cart::session($userId)->add(array(
                  array(
                      'id' => $id,
                      'name' => $name,
                      'price' => $price,
                      'quantity' => $count,
                      'attributes' => array(
                        'order' => true,
                        'image' => $image,
                        'width' => $width,
                        'height' => $height,
                        'side' => $side,
                        'mounting' =>$mounting,
                        'code' => $code
                      )
                  ),
                ));
            }
        }

Misagh90 avatar Dec 10 '19 12:12 Misagh90

Hi you should not add product with same ID. If same product but totally different attribute and price, you should add it as new product with new unique ID. the purpose of id field is not the product model ID, but for a unique cart item ID and will be use when updating carts. You can put your model id on attributes field instead if need it for full flexibility.

darryldecode avatar Jan 08 '20 16:01 darryldecode

I have the same issue here i have an item with different sizes when i select one item with one size then again the same item with another size it replace the first one . can someone please help me

how to add unique id for every item i have thanks in advance

maliknaqibullah avatar Jan 30 '20 08:01 maliknaqibullah

Hi. you should not use the Product ID as the ID when you add an item on the cart. Generate a unique ID when you add item on the cart so you can update them independently. image

darryldecode avatar Feb 04 '20 03:02 darryldecode

Thanks alot for replying How to generate unique id thenSent from my Huawei phone-------- Original message --------From: darryl fernandez [email protected]Date: Tue., 4 Feb. 2020, 7:32 amTo: darryldecode/laravelshoppingcart [email protected]Cc: Naqibullah [email protected], Comment [email protected]Subject: Re: [darryldecode/laravelshoppingcart] How to add a custom (different size) product (#215)Hi. you should not use the Product ID as the ID when you add an item on the cart. Generate a unique ID when you add item on the cart so you can update them independently.

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or unsubscribe.

maliknaqibullah avatar Feb 04 '20 04:02 maliknaqibullah

Hi. That is totally up to you. You can use any random string or better use a library to generate a Uuid for your row ID.

darryldecode avatar Feb 04 '20 05:02 darryldecode

Thank you so much for your help

On Tue., 4 Feb. 2020, 9:09 am darryl fernandez, [email protected] wrote:

Hi. That is totally up to you. You can use any random string or better use a library to generate a Uuid for your row ID.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/darryldecode/laravelshoppingcart/issues/215?email_source=notifications&email_token=AH6WCLEKAPGUETH2CRJLC4TRBD2ALA5CNFSM4JY5O522YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKWLXYY#issuecomment-581745635, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH6WCLFNNEYGWABPRFOLTKDRBD2ALANCNFSM4JY5O52Q .

maliknaqibullah avatar Feb 04 '20 06:02 maliknaqibullah

yes but than all items will be added to cart separately, even if identical, is there a way around this?

michalakis avatar Mar 17 '20 11:03 michalakis

oui mais que tous les articles seront ajoutés au panier séparément, même s'ils sont identiques, y a-t-il un moyen de contourner cela?

i have the same problem

Wolpdg avatar Apr 12 '20 16:04 Wolpdg