/** * Finds or creates a current order. * @todo split this into two functions: initcart, and currentcart...so that templates can return null for Cart */ public static function current_order() { if (self::$order) { return self::$order; } //we only want to hit the database once //find order by id saved to session (allows logging out and retaining cart contents) $cartid = Session::get(self::$cartid_session_name); //TODO: make clear cart on logout optional if ($cartid && ($o = DataObject::get_one('Order', "\"Status\" = 'Cart' AND \"ID\" = {$cartid}"))) { $order = $o; } else { $order = new Order(); $order->SessionID = session_id(); if (EcommerceRole::get_associate_to_current_order()) { $order->MemberID = Member::currentUserID(); } // Set the Member relation to this order $order->write(); Session::set(self::$cartid_session_name, $order->ID); //init modifiers the first time the order is created // (currently assumes modifiers won't change) } self::$order = $order; //temp caching $order->initModifiers(); //init /re-init modifiers $order->write(); // Write the order return $order; }