/** * 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(); }
/** * Update/add SKU/quantity to cart, update the price in the cart, save old price and inventory level (if requested) * * @param int SKU ID * @param string Update Method: add - adds to the existing quantity, * set - ignores existing quantity and sets a new value, * sync - simply checks/updates inventory and pricing * @param int Quantity * @param bool Flag determining whether the old qty should be saved (only when it goes down); * price get saved in either case * @return void */ protected function doItem($sId, $mode = 'add', $qty = 1, $retainOldValue = false) { // Check quantity: must be a positive integer or zero if (!CartHelper::isNonNegativeInt($qty)) { throw new \Exception(Lang::txt('COM_CART_INCORRECT_QTY')); } elseif ($qty == 0 && !$retainOldValue) { // Delete if quantity is set to zero if ($mode == 'set') { $this->deleteItem($sId); return; } else { throw new \Exception(Lang::txt('COM_CART_INCORRECT_QTY')); } } // Check if there is enough inventory (if tracking inventory) taking into account current quantity in the cart // Get the quantity already in the cart (if appending or simply syncing) if ($mode == 'add' || $mode == 'sync') { $skuCartInfo = $this->getCartItem($sId); } else { $skuCartInfo = new \stdClass(); $skuCartInfo->crtiQty = 0; } // Get SKU pricing and inventory level & policies as well as permissions to access products $warehouse = $this->warehouse; try { $allSkuInfo = $warehouse->getSkuInfo($sId, false); } catch (\Exception $e) { throw new \Exception(Lang::txt($e->getMessage())); } if (empty($allSkuInfo)) { throw new \Exception(Lang::txt('COM_STOREFRONT_SKU_NOT_FOUND')); } $skuInfo = $allSkuInfo['info']; $skuName = $skuInfo->pName; if (!empty($allSkuInfo['options']) && count($allSkuInfo['options'])) { foreach ($allSkuInfo['options'] as $oName) { $skuName .= ', ' . $oName; } } // Check inventory rules (sync mode doesn't check inventory level, just pricing) if ($mode != 'sync') { // Don't allow purchasing multiple products (same & different SKUs) for those that are not allowed if (!$skuInfo->pAllowMultiple) { // Check this SKU qty to make sure no multiple SKUs are there if (!empty($skuCartInfo->crtiQty) && $skuCartInfo->crtiQty > 0 || $qty > 1) { //throw new \Exception($skuInfo->pName . Lang::txt('COM_CART_NO_MULTIPLE_ITEMS')); throw new \Exception($skuInfo->pName . " is already in the cart and cannot be added multiple times"); } // Check if there is this product already in the cart (different SKU) $allSkus = $warehouse->getProductSkus($skuInfo->pId); if (is_array($allSkus) || is_object($allSkus)) { foreach ($allSkus as $skuId) { // Skip the current SKU, look only at other SKUs if ($skuId != $sId) { $otherSkuInfo = $this->getCartItem($skuId); // Error if there is already another SKU of the same product in the cart if (!empty($otherSkuInfo->crtiQty) && $otherSkuInfo->crtiQty > 0) { //throw new \Exception($skuInfo->pName . Lang::txt('COM_CART_NO_MULTIPLE_ITEMS')); throw new \Exception($skuInfo->pName . " is already in the cart and cannot be added multiple times"); } } } } } // Don't allow purchasing multiple SKUs for those that are not allowed if (!$skuInfo->sAllowMultiple && (!empty($skuCartInfo->crtiQty) && $skuCartInfo->crtiQty > 0 || $qty > 1)) { throw new \Exception($skuName . Lang::txt('COM_CART_NO_MULTIPLE_ITEMS')); } // Make sure there is enough inventory if ($skuInfo->sTrackInventory) { // See if qty can be added if ($qty > $skuInfo->sInventory) { throw new \Exception(Lang::txt('COM_CART_NOT_ENOUGH_INVENTORY')); } elseif (!empty($skuCartInfo->crtiQty) && $qty + $skuCartInfo->crtiQty > $skuInfo->sInventory) { // This is how much they can add: $skuInfo->sInventory - $skuCartInfo->crtiQty throw new \Exception(Lang::txt('COM_CART_ADD_TOO_MANY_CART')); } } } // Run the auditor if ($mode != 'sync') { //require_once(JPATH_BASE . DS . 'components' . DS . 'com_cart' . DS . 'helpers' . DS . 'Audit.php'); $auditor = Audit::getAuditor($skuInfo, $this->crtId); $auditor->setSku($skuInfo->sId); //print_r($auditor); die; $auditorResponse = $auditor->audit(); if ($auditorResponse->status == 'error') { throw new \Exception($skuInfo->pName . ', ' . $skuInfo->sSku . $auditor->getResponseError()); } } // Insert new values, if exists save the previous price (for possible price changes messaging) // and old inventory level (if needed) $sql = "INSERT INTO `#__cart_cart_items`\n\t\t\t\t(`crtId`, `sId`, `crtiQty`, `crtiOldQty`, `crtiPrice`, `crtiOldPrice`, `crtiName`)\n\t\t\t\tVALUES\n\t\t\t\t({$this->crtId}, '{$sId}', {$qty}, NULL, {$skuInfo->sPrice}, NULL, " . $this->_db->quote($skuName) . ")\n\t\t\t\tON DUPLICATE KEY UPDATE `crtiOldPrice` = `crtiPrice`, `crtiPrice` = {$skuInfo->sPrice}, `crtiName` = " . $this->_db->quote($skuName); // Check if old value has to be retained if ($retainOldValue) { $sql .= ", `crtiOldQty` = `crtiQty`"; } else { $sql .= ", `crtiOldQty` = NULL"; } // add to the existing qty value if ($mode == 'add') { $sql .= ", `crtiQty` = `crtiQty` + {$qty}"; } elseif ($mode == 'set') { $sql .= ", `crtiQty` = {$qty}"; } // keep the qty value if syncing $this->_db->setQuery($sql); //print_r($this->_db->replacePrefix($this->_db->getQuery())); die; $this->_db->query(); }