darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.35k stars 452 forks source link

Cart conditions won't save to table #125

Open robertnicjoo opened 6 years ago

robertnicjoo commented 6 years ago

I have products with and without conditions in my cart, when I add my cart data to orders table all my cart data will save except my products conditions.

dd of my cart data:

CartCollection {#699 ▼
  #items: array:1 [▼
    2 => ItemCollection {#670 ▼
      #config: array:6 [▶]
      #items: array:6 [▼
        "id" => 2
        "name" => "product two"
        "price" => 50000.0
        "quantity" => 2
        "attributes" => ItemAttributeCollection {#671 ▼
          #items: array:1 [▼
            "attr" => array:2 [▼
              "name" => "weight"
              "value" => "2"
            ]
          ]
        }
        "conditions" => array:1 [▼
          0 => CartCondition {#672 ▼
            -args: array:4 [▼
              "name" => "12 inch"
              "value" => "25000"
              "type" => "additional"
              "target" => "item"
            ]
            -parsedRawValue: 25000.0
          }
        ]
      ]
    }
  ]
}

dd of same cart when I try to save in orders table:

Order {#680 ▼
  #fillable: array:16 [▶]
  #events: array:1 [▶]
  #casts: array:1 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:15 [▶]
  #original: array:15 [▼
    "product_data" => "{"2":{"id":2,"name":"product two","price":50000,"quantity":2,"attributes":{"attr":{"name":"weight","value":"2"}},"conditions":[{}]}}"
    "user_id" => 1
    "ordernu" => 9358964473
    "address_id" => "1"
    "orderstatus_id" => 1
    "quantity" => 2
    "payment_id" => null
    "buyer_name" => "John Doe"
    "note" => null
    "buyer_email" => "admin@admin.com"
    "phone" => "006281200000000"
    "price" => null
    "updated_at" => "2018-02-18 13:03:41"
    "created_at" => "2018-02-18 13:03:41"
    "id" => 10
  ]

As you see my conditions "conditions":[{}]} is empty.

Here is my save function:

//save cart to orders table and remove items from it
    public function checkout(Request $request)
    {
      $cartItems = Cart::getContent();

      try {
        $order = new Order();
        $status  = Orderstatus::where('title', 'Witing Payment')->value('id');
        $qty = Cart::getTotalQuantity();
        $order->product_data = $cartItems; //save cart data as json
        $order->user_id = Auth::user()->id;
        $order->ordernu = mt_rand(1000000000, 9999999999);
        $order->address_id = $request->input('address_id');
        $order->orderstatus_id = $status;
        $order->quantity = $qty;
        $order->payment_id = $request->input('payment_id');
        $order->buyer_name = $request->input('buyer_name');
        $order->note = $request->input('note');
        $order->buyer_email = $request->input('buyer_email');
        $order->phone = $request->input('phone');
        $order->price = $request->input('totalPriceInTotal');
        dd(Auth::user()->orders()->save($order)); // dd results
        Auth::user()->orders()->save($order);

        foreach ($cartItems as $item) {
            $product = Product::find($item->id);
            $product->decrement('stock', $item->quantity);
        }

      $user = $order->buyer_email;
      event(new UserOrdered($order));

      //Cart::clear(); //clear cart info
      Session::flash('success', 'Thank you. Your order has been received.');
      return redirect()->route('ordersindex');
      }catch (Exception $e) {
         return response($e->getMessage(), 400);
      }
    }

any idea?

robertnicjoo commented 6 years ago

anyone there can help with that?

@darryldecode

avigato commented 6 years ago

same problem for me...have you found a solution @robertnicjoo?