/** * @return Cart */ private function getCartOrCreateNew() { $cart = merx_current_cart(); if (!$cart) { $cart = Cart::create(); session()->put("merx_cart_id", $cart->id); } return $cart; }
/** * Assign cart_id and client_id to the new order */ public static function boot() { parent::boot(); Order::creating(function ($order) { $clientId = merx_current_client_id(); if (!$clientId) { throw new NoCurrentClientException(); } $cart = $order->cart; if (!$cart) { $cart = merx_current_cart(); $order->cart()->associate($cart); } static::checkCartIsValid($cart); if (!$order->ref) { // Have to generate a unique ref $order->ref = static::generateRef(); } elseif (Order::where("ref", $order->ref)->count()) { throw new OrderWithThisRefAlreadyExist(); } $order->client_id = $clientId; $order->state = "draft"; }); }
/** * @param integer|null $cartId * @return Cart|null */ private function existingCart($cartId = null) { return $cartId ? Cart::find($cartId) : merx_current_cart(); }