Ejemplo n.º 1
0
 /**
  * Display product
  *
  * @param		$pId
  * @return     	void
  */
 public function displayTask()
 {
     $productIdentifier = Request::getVar('product', '');
     $pInfo = $this->warehouse->checkProduct($productIdentifier);
     if (!$pInfo->status) {
         App::abort($pInfo->errorCode, Lang::txt($pInfo->message));
     }
     $pId = $pInfo->pId;
     $this->view->pId = $pId;
     $this->view->css();
     $this->view->js('product_display.js');
     // A flag whether the item is available for purchase (for any reason, used by the auditors)
     $productAvailable = true;
     $pageMessages = array();
     // Get the cart
     $cart = new CurrentCart();
     // POST add to cart request
     $addToCartRequest = Request::getVar('addToCart', false, 'post');
     $options = Request::getVar('og', false, 'post');
     $qty = Request::getInt('qty', 1, 'post');
     if ($addToCartRequest) {
         // Initialize errors array
         $errors = array();
         // Check if passed options/productID map to a SKU
         try {
             $sku = $this->warehouse->mapSku($pId, $options);
             $cart->add($sku, $qty);
         } catch (\Exception $e) {
             $errors[] = $e->getMessage();
             $pageMessages[] = array($e->getMessage(), 'error');
         }
         if (!empty($errors)) {
             $this->view->setError($errors);
         } else {
             // prevent resubmitting by refresh
             // If not an ajax call, redirect to cart
             App::redirect(Route::url('index.php?option=com_cart'));
         }
     }
     // Get the product info
     $product = $this->warehouse->getProductInfo($pId);
     $this->view->product = $product;
     // Run the auditor
     $auditor = Audit::getAuditor($product, $cart->getCartInfo()->crtId);
     $auditorResponse = $auditor->audit();
     //print_r($auditorResponse); die;
     if (!empty($auditorResponse) && $auditorResponse->status != 'ok') {
         if ($auditorResponse->status == 'error') {
             // Product is not available for purchase
             $productAvailable = false;
             foreach ($auditorResponse->notices as $notice) {
                 $pageMessages[] = array($notice, 'warning');
             }
         }
     }
     // Get option groups with options and SKUs
     $data = $this->warehouse->getProductOptions($pId);
     //print_r($data); die;
     if ($data) {
         //App::abort(404 , Lang::txt('COM_STOREFRONT_PRODUCT_ERROR'));
         $this->view->options = $data->options;
     }
     //print_r($data); die;
     // Find a price range for the product
     $priceRange = array('high' => 0, 'low' => false);
     /*
     	Find if there is a need to display a product quantity dropdown on the initial view load. It will be only displayed for single SKU that allows multiple items.
     	For multiple SKUs it will be generated by JS (no drop-down for non-JS users, sorry)
     */
     $qtyDropDownMaxVal = 0;
     $inStock = true;
     if (!$data || !count($data->skus)) {
         $inStock = false;
     }
     $this->view->inStock = $inStock;
     if ($data && count($data->skus) == 1) {
         // Set the max value for the dropdown QTY
         // TODO: add it to the SKU table to set on the per SKU level
         $qtyDropDownMaxValLimit = 20;
         // Get the first and the only value
         $arrayValues = array_values($data->skus);
         $sku = array_shift($arrayValues);
         // If no inventory tracking, there is no limit on how many can be purchased
         $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         if ($sku['info']->sTrackInventory) {
             $qtyDropDownMaxVal = $sku['info']->sInventory;
         }
         if ($qtyDropDownMaxVal < 1) {
             $qtyDropDownMaxVal = 1;
         } elseif ($qtyDropDownMaxVal > $qtyDropDownMaxValLimit) {
             $qtyDropDownMaxVal = $qtyDropDownMaxValLimit;
         }
         // If the SKU doesn't allow multiple items, set the dropdown to 1
         if (!$sku['info']->sAllowMultiple) {
             $qtyDropDownMaxVal = 1;
         }
     }
     $this->view->qtyDropDown = $qtyDropDownMaxVal;
     if ($data) {
         foreach ($data->skus as $sId => $info) {
             $info = $info['info'];
             if ($info->sPrice > $priceRange['high']) {
                 $priceRange['high'] = $info->sPrice;
             }
             if (!$priceRange['low'] || $priceRange['low'] > $info->sPrice) {
                 $priceRange['low'] = $info->sPrice;
             }
         }
     }
     $this->view->price = $priceRange;
     // Add custom page JS
     if ($data && (count($data->options) > 0 || count($data->skus) > 1)) {
         $js = $this->getDisplayJs($data->options, $data->skus, $productIdentifier);
         Document::addScriptDeclaration($js);
     }
     $this->view->config = $this->config;
     $this->view->productAvailable = $productAvailable;
     //build pathway
     $this->_buildPathway($product->pName);
     // Set notifications
     $this->view->notifications = $pageMessages;
     $this->view->display();
 }