public function index() { // TODO get the slug param. lookup the category. Check ACL against both category. // get paginated list of blog posts associated with this category // only posts that are published as of now $f3 = \Base::instance(); $slug = $this->inputfilter->clean($f3->get('PARAMS.slug'), 'cmd'); $products_model = $this->model('products'); try { $paginated = $products_model->populateState()->paginate(); } catch (\Exception $e) { // TODO Change to a normal 404 error \Dsc\System::instance()->addMessage("Invalid Items: " . $e->getMessage(), 'error'); $f3->reroute('/'); return; } $state = $products_model->getState(); \Base::instance()->set('state', $state); \Base::instance()->set('paginated', $paginated); $this->app->set('meta.title', 'Shop'); \Shop\Models\Activities::track('Viewed Shop Home', array('page_number' => $paginated->current_page)); $view = \Dsc\System::instance()->get('theme'); echo $view->render('Shop/Site/Views::home/index.php'); }
public function read() { $slug = $this->inputfilter->clean($this->app->get('PARAMS.slug'), 'cmd'); try { $model = $this->model('products')->setState('filter.slug', $slug); $preview = $this->input->get("preview", 0, 'int'); if ($preview) { $this->canPreview(false, "Shop\\Models\\Products"); } else { $model->setState('filter.published_today', true)->setState('filter.publication_status', 'published')->setState('filter.inventory_status', 'in_stock'); } $item = $model->getItem(); if (empty($item->id)) { throw new \Exception(); } } catch (\Exception $e) { $this->app->error('404', 'Invalid Product'); return; } // get the previous query_params for the products model from the state history // but only if this product is in the last state $model = $this->model('products'); if ($params = $this->session->lastState(get_class($model))) { $surrounding = $model->surrounding($item->id, $params); if (!empty($surrounding['found'])) { \Base::instance()->set('surrounding', $surrounding); } } \Base::instance()->set('item', $item); $this->app->set('meta.title', $item->seoTitle() . ' | Shop'); $this->app->set('meta.description', $item->seoDescription()); \Shop\Models\Activities::track('Viewed Potential Purchase', array('SKU' => $item->{'tracking.sku'}, 'Product Name' => $item->title, 'product_id' => (string) $item->id)); $this->session->set('shop.product_review.redirect', '/shop/product/' . $item->slug); $view = \Dsc\System::instance()->get('theme'); echo $view->render('Shop/Site/Views::product/detail.php'); }
/** * * @param unknown $variant_id * @param string $wishlist_id */ public function moveToCart($wishlistitem_hash, $cart) { $item = $this->fetchItemByHash($wishlistitem_hash); if (empty($item['id'])) { throw new \Exception('Invalid Wishlist Item'); } $variant_id = $item['variant_id']; $product = (new \Shop\Models\Variants())->getById($variant_id); if ($cart->addItem($variant_id, $product)) { // Track it if ($variant = $product->variant($variant_id)) { \Shop\Models\Activities::track('Moved product from wishlist to cart', array('SKU' => $product->{'tracking.sku'}, 'Variant Title' => !empty($variant['attribute_title']) ? $variant['attribute_title'] : $product->title, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } else { \Shop\Models\Activities::track('Moved product from wishlist to cart', array('SKU' => $product->{'tracking.sku'}, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } $this->removeItem($wishlistitem_hash); } return $this; }
/** * Add an item to a wishlist */ public function add() { $f3 = \Base::instance(); // ----------------------------------------------------- // Start: validation // ----------------------------------------------------- $variant_id = $this->input->get('variant_id'); try { $product = (new \Shop\Models\Variants())->getById($variant_id); } catch (\Exception $e) { if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage('Item not added to wishlist - Invalid product', 'error'); $f3->reroute('/shop/wishlist'); return; } } // ----------------------------------------------------- // End: validation // ----------------------------------------------------- // get the current user's wishlist, either based on session_id (visitor) or user_id (logged-in) $wishlist = \Shop\Models\Wishlists::fetch(); // add the item try { $wishlist->addItem($variant_id, $product, $f3->get('POST')); } catch (\Exception $e) { if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage('Item not added to wishlist', 'error'); \Dsc\System::addMessage($e->getMessage(), 'error'); $f3->reroute('/shop/wishlist'); return; } } // Track it if ($variant = $product->variant($variant_id)) { \Shop\Models\Activities::track('Added to Wishlist', array('SKU' => $product->{'tracking.sku'}, 'Variant Title' => !empty($variant['attribute_title']) ? $variant['attribute_title'] : $product->title, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } else { \Shop\Models\Activities::track('Added to Wishlist', array('SKU' => $product->{'tracking.sku'}, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => true))); } else { \Dsc\System::addMessage('Item added to wishlist'); $f3->reroute('/shop/wishlist'); } }
public function viewAll() { $slug = $this->inputfilter->clean($this->app->get('PARAMS.slug'), 'cmd'); $model = $this->getModel()->populateState(); try { $collection = (new \Shop\Models\Collections())->setState('filter.slug', $slug)->getItem(); if (empty($collection->id)) { throw new \Exception('Invalid Collection'); } $model->setState('filter.collection', $collection->id); $conditions = \Shop\Models\Collections::getProductQueryConditions($collection->id); if ($filter_tags = (array) $model->getState('filter.tags')) { if ($tags = array_filter(array_values($filter_tags))) { if (!empty($conditions['tags'])) { // Add this to an $and clause if (empty($conditions['$and'])) { $conditions['$and'] = array(); } $conditions['$and'][] = array('tags' => array('$in' => $tags)); } else { $conditions['tags'] = array('$in' => $tags); } } } switch ($model->getState('sort_by')) { // only use the collection's $collection->sort_by if the user hasn't set their own state, // which is populated in the model by populateState() case "collection-default": case "": switch ($collection->sort_by) { case "ordering-asc": $model->setState('list.sort', array(array('collections.' . $collection->id . '.ordering' => 1))); break; default: $model->handleSortBy($collection->sort_by); break; } break; } $model->setState('list.limit', 300); $paginated = $model->setParam('conditions', $conditions)->paginate(); } catch (\Exception $e) { \Dsc\System::instance()->addMessage($e->getMessage(), 'error'); $this->app->error('404'); return; } // push the current query_params into the state history $this->session->trackState(get_class($model), $model->getParam())->clearUrls()->trackUrl($collection->{'title'}); $state = $model->getState(); $this->app->set('state', $state); $this->app->set('paginated', $paginated); $this->app->set('collection', $collection); $this->app->set('meta.title', $collection->seoTitle() . ' | Shop'); $this->app->set('meta.description', $collection->seoDescription()); \Shop\Models\Activities::track('Viewed Collection', array('Collection Name' => $collection->seoTitle(), 'collection_id' => (string) $collection->id, 'page_number' => 'view_all')); $view = \Dsc\System::instance()->get('theme'); $view_file = 'all.php'; if ($collection->{'display.view'} && $view->findViewFile('Shop/Site/Views::collection/all/' . $collection->{'display.view'})) { $view_file = 'all/' . $collection->{'display.view'}; } echo $view->renderTheme('Shop/Site/Views::collection/' . $view_file); }
public function viewAll() { $param = $this->inputfilter->clean($this->app->get('PARAMS.1'), 'string'); $pieces = explode('?', $param); $path = $pieces[0]; $products_model = $this->model('products'); try { $category = $this->model('categories')->setState('filter.path', $path)->getItem(); if (empty($category->id)) { throw new \Exception(); } $paginated = $products_model->populateState()->setState('filter.category.id', $category->id); $preview = $this->input->get("preview", 0, 'int'); if ($preview) { $this->canPreview(false, "Shop\\Models\\Categories"); } else { $products_model->setState('filter.published_today', true)->setState('filter.inventory_status', 'in_stock')->setState('filter.publication_status', 'published'); } $products_model->setState('list.limit', 300); $paginated = $products_model->paginate(); } catch (\Exception $e) { \Dsc\System::instance()->addMessage('Invalid category', 'error'); \Dsc\System::instance()->addMessage($path, 'error'); $this->app->reroute('/shop'); // $f3->error('404'); return; } // push the current query_params into the state history $this->session->trackState(get_class($products_model), $products_model->getParam())->clearUrls(); foreach ($category->ancestors() as $ancestor) { $this->session->trackUrl($ancestor->title, '/shop/category' . $ancestor->path); } $this->session->trackUrl($category->{'title'}); $state = $products_model->getState(); $this->app->set('state', $state); $this->app->set('paginated', $paginated); $this->app->set('category', $category); $this->app->set('meta.title', $category->seoTitle() . ' | Shop'); $this->app->set('meta.description', $category->seoDescription()); \Shop\Models\Activities::track('Viewed Category', array('Category Name' => $category->seoTitle(), 'category_id' => (string) $category->id, 'page_number' => 'view_all')); $view = \Dsc\System::instance()->get('theme'); $view_file = 'all.php'; if ($category->{'display.view'} && $view->findViewFile('Shop/Site/Views::category/all/' . $category->{'display.view'})) { $view_file = 'all/' . $category->{'display.view'}; } echo $view->renderTheme('Shop/Site/Views::category/' . $view_file); }
/** * 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); }
/** * Add an item to a cart */ public function add() { $redirect = '/shop/cart'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('shop.add_to_cart.product.redirect')) { $redirect = $custom_redirect; } // ----------------------------------------------------- // Start: validation // ----------------------------------------------------- $variant_id = $this->input->get('variant_id'); // load the product try { $product = (new \Shop\Models\Variants())->getById($variant_id); } catch (\Exception $e) { if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage('Item not added to cart - Invalid product', 'error'); $this->app->reroute($redirect); return; } } // ----------------------------------------------------- // End: validation // ----------------------------------------------------- // get the current user's cart, either based on session_id (visitor) or user_id (logged-in) $cart = \Shop\Models\Carts::fetch(); // add the item try { $cart->addItem($variant_id, $product, $this->app->get('POST')); } catch (\Exception $e) { if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage('Item not added to cart', 'error'); \Dsc\System::addMessage($e->getMessage(), 'error'); $this->app->reroute($redirect); return; } } // Track it if ($variant = $product->variant($variant_id)) { \Shop\Models\Activities::track('Added to Cart', array('SKU' => $product->{'tracking.sku'}, 'Variant Title' => !empty($variant['attribute_title']) ? $variant['attribute_title'] : $product->title, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } else { \Shop\Models\Activities::track('Added to Cart', array('SKU' => $product->{'tracking.sku'}, 'Product Name' => $product->title, 'variant_id' => (string) $variant_id, 'product_id' => (string) $product->id)); } if ($this->app->get('AJAX')) { $this->app->set('cart', $cart); return $this->outputJson($this->getJsonResponse(array('result' => true, 'html' => $this->theme->renderView('Shop/Site/Views::cart/lightbox.php')))); } else { \Dsc\System::addMessage('Item added to cart. <a href="./shop/checkout"><b>Click here to checkout now!</b></a>'); $this->app->reroute($redirect); } }