/** * Gets or creates the current order. * Based on the session ONLY! * IMPORTANT FUNCTION! * @todo - does this need to be public???? * @return void */ public function currentOrder($recurseCount = 0) { if (!$this->order) { $sessionVariableName = $this->sessionVariableName("OrderID"); $orderIDFromSession = intval(Session::get($sessionVariableName)); if ($orderIDFromSession > 0) { $this->order = Order::get()->byID($orderIDFromSession); } $member = Member::currentUser(); if ($this->order) { //first reason to set to null: it is already submitted if ($this->order->IsSubmitted()) { $this->order = null; } elseif (!$this->order->canView()) { $this->order = null; } elseif ($member && $member->exists()) { if ($this->order->MemberID != $member->ID) { $updateMember = false; if (!$this->order->MemberID) { $updateMember = true; } if (!$member->IsShopAdmin()) { $updateMember = true; } if ($updateMember) { $this->order->MemberID = $member->ID; $this->order->write(); } } //IF current order has nothing in it AND the member already has an order: use the old one first //first, lets check if the current order is worthwhile keeping if ($this->order->StatusID || $this->order->TotalItems()) { //do NOTHING! } else { $firstStep = OrderStep::get()->First(); //we assume the first step always exists. //TODO: what sort order? $count = 0; while ($firstStep && ($previousOrderFromMember = Order::get()->where("\r\n\t\t\t\t\t\t\t\t\t\"MemberID\" = " . $member->ID . "\r\n\t\t\t\t\t\t\t\t\tAND (\"StatusID\" = " . $firstStep->ID . " OR \"StatusID\" = 0)\r\n\t\t\t\t\t\t\t\t\tAND \"Order\".\"ID\" <> " . $this->order->ID)->First())) { //arbritary 12 attempts ... if ($count > 12) { break; } $count++; if ($previousOrderFromMember && $previousOrderFromMember->canView()) { if ($previousOrderFromMember->StatusID || $previousOrderFromMember->TotalItems()) { $this->order->delete(); $this->order = $previousOrderFromMember; break; } else { $previousOrderFromMember->delete(); } } } } } } if (!$this->order) { if ($member) { $firstStep = OrderStep::get()->First(); if ($firstStep) { $previousOrderFromMember = Order::get()->filter(array("MemberID" => $member->ID, "StatusID" => array($firstStep->ID, 0)))->First(); if ($previousOrderFromMember) { if ($previousOrderFromMember->canView()) { $this->order = $previousOrderFromMember; } } } } if ($this->order && !$this->order->exists()) { $this->order = null; } if (!$this->order) { //here we cleanup old orders, because they should be //cleaned at the same rate that they are created... if (EcommerceConfig::get("ShoppingCart", "cleanup_every_time")) { $cartCleanupTask = EcommerceTaskCartCleanup::create(); $cartCleanupTask->runSilently(); } //create new order $this->order = Order::create(); if ($member) { $this->order->MemberID = $member->ID; } $this->order->write(); } Session::set($sessionVariableName, intval($this->order->ID)); } elseif ($this->order->MemberID && !$member) { $this->clear(); $this->order = null; } if ($this->order && $this->order->exists()) { $this->order->calculateOrderAttributes($force = false); } if ($this->order && !$this->order->SessionID) { $this->order->SessionID = session_id(); $this->order->write(); } } //just in case ... if (!$this->order && $recurseCount < 3) { $recurseCount++; return $this->currentOrder(); } return $this->order; }
/** * run in verbose mode */ public static function run_on_demand() { $obj = new EcommerceTaskCartCleanup(); $obj->verbose = true; $obj->run(null); }