/** * Displays an order confirmation page */ public function confirmation() { $just_completed_order_id = \Dsc\System::instance()->get('session')->get('shop.just_completed_order_id'); if (!empty($just_completed_order_id)) { try { $order = (new \Shop\Models\Orders())->load(array('_id' => new \MongoId((string) $just_completed_order_id))); if (empty($order->id)) { throw new \Exception(); } /** * Start Activity Tracking */ $properties = array('order_id' => (string) $order->id, 'order_number' => (string) $order->number, 'Grand Total' => (string) $order->grand_total, 'Credit Total' => (string) $order->credit_total, 'Products' => array(), 'Coupons' => \Dsc\ArrayHelper::getColumn((array) $order->coupons, 'code'), 'Auto Coupons' => \Dsc\ArrayHelper::getColumn((array) $order->auto_coupons, 'code')); $identity = $this->getIdentity(); if ($identity->guest) { $properties['guest'] = true; } foreach ($order->items as $item) { $product = array(); $product['Product Name'] = \Dsc\ArrayHelper::get($item, 'product.title'); if (\Dsc\ArrayHelper::get($item, 'attribute_title')) { $product['Variant'] = \Dsc\ArrayHelper::get($item, 'attribute_title'); } $properties['Products'][] = $product; } \Shop\Models\Activities::track('Completed Checkout', $properties); $abandoned_cart = \Dsc\System::instance()->get('session')->get('shop.notification_email'); if ($abandoned_cart == 1) { // checkedout abandoned cart \Shop\Models\Activities::track('Completed Checkout Abandoned Email'); \Dsc\System::instance()->get('session')->set('shop.notification_email', 0); } /** * END Activity Tracking */ // check coupons and discard used generated codes if (count($order->coupons)) { foreach ($order->coupons as $coupon) { if (!empty($coupon['generated_code'])) { \Shop\Models\Coupons::collection()->update(array('_id' => new \MongoId((string) $coupon['_id']), 'codes.list.code' => (string) $coupon['generated_code']), array('$set' => array('codes.list.$.used' => 1))); } } } if (count($order->auto_coupons)) { foreach ($order->auto_coupons as $coupon) { if (!empty($coupon['generated_code'])) { \Shop\Models\Coupons::collection()->update(array('_id' => new \MongoId((string) $coupon['_id']), 'codes.list.code' => (string) $coupon['generated_code']), array('$set' => array('codes.list.$.used' => 1))); } } } } catch (\Exception $e) { // TODO Handle when it's an invalid order } if (!empty($order->id)) { \Base::instance()->set('order', $order); } } $this->app->set('meta.title', 'Order Confirmation | Checkout'); $view = \Dsc\System::instance()->get('theme'); echo $view->render('Shop/Site/Views::confirmation/index.php'); \Dsc\System::instance()->get('session')->set('shop.just_completed_order', false); \Dsc\System::instance()->get('session')->set('shop.just_completed_order_id', null); }
public function countUsedCodes() { $result = \Shop\Models\Coupons::collection()->aggregate(array('$match' => array('_id' => new \MongoId((string) $this->id))), array('$unwind' => '$codes.list'), array('$group' => array('_id' => '$title', 'used_codes' => array('$sum' => '$codes.list.used')))); if (count($result['result'])) { return $result['result'][0]['used_codes']; } else { return 0; } }
public function deleteCode() { $id = $this->inputfilter->clean($this->app->get("PARAMS.id"), 'ALNUM'); $code = $this->inputfilter->clean($this->app->get('PARAMS.code'), 'CMD'); if (empty($id) || empty($code)) { \Dsc\System::addMessage('Missing coupon ID', 'error'); $this->app->reroute('/admin/shop/coupons'); return; } try { \Shop\Models\Coupons::collection()->update(array('_id' => new \MongoId((string) $id)), array('$pull' => array('codes.list' => array('code' => $code))), array("upset" => true)); \Dsc\System::addMessage('Codes was successfuly deleted.'); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'error'); } $this->app->reroute('/admin/shop/coupon/' . $id . '/codes'); }