首页 > 解决方案 > 如何在 darryldecode/laravelshoppingcart 中添加自定义(不同尺寸)产品?

问题描述

我正在为我的购物车使用darryldecode/laravelshoppingcart,我有一些尺寸不同的产品(可订购的高度和宽度),价格会随着尺寸的变化而变化,但是当我尝试从购物车中的产品中添加两种不同尺寸时,数量增加,但我需要创建一个具有新尺寸的新产品行(例如 3×4 日光壁纸和 2×3 日光壁纸{在我的购物车中:是我添加的最后一个尺寸的 2 日光壁纸它})。另外,有正常的产品

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
                      )
                  ),
                ));
            }
        }

标签: laravelcartshopping-cartshop

解决方案


推荐阅读