/** * 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 (!Cart_Helper::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 include_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php'; $warehouse = new StorefrontModelWarehouse(); $allSkuInfo = $warehouse->getSkusInfo(array($sId)); if (empty($allSkuInfo)) { throw new Exception(Lang::txt('COM_STOREFRONT_SKU_NOT_FOUND')); } $skuInfo = $allSkuInfo[$sId]['info']; $skuName = $skuInfo->pName; if (!empty($allSkuInfo[$sId]['options']) && count($allSkuInfo[$sId]['options'])) { foreach ($allSkuInfo[$sId]['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')); } // Check if there is this project already in the cart (different SKU) $allSkus = $warehouse->getProductSkus($skuInfo->pId); 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')); } } } } // 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); $auditorResponse = $auditor->audit(); if ($auditorResponse->status == 'error') { throw new Exception($skuInfo->pName . $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); $this->_db->query(); }
/** * Display product * * @param $pId * @return void */ public function displayTask() { $pId = $this->warehouse->productExists(Request::getVar('product', '')); if (!$pId) { App::abort(404, Lang::txt('COM_STOREFRONT_PRODUCT_NOT_FOUND')); } $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 require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'CurrentCart.php'; $cart = new CartModelCurrentCart(); // 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 $redirect_url = Route::url('index.php?option=' . 'com_cart'); App::redirect($redirect_url); } } // Get the product info $product = $this->warehouse->getProductInfo($pId); $this->view->product = $product; // Run the auditor require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'helpers' . DS . 'Audit.php'; $auditor = Audit::getAuditor($product, $cart->getCartInfo()->crtId); $auditorResponse = $auditor->audit(); //print_r($auditor); 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); if ($data) { //throw new Exception(Lang::txt('COM_STOREFRONT_PRODUCT_ERROR'), 404); $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 $sku = array_shift(array_values($data->skus)); // 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) > 1 || count($data->skus) > 1)) { $js = $this->getDisplayJs($data->options, $data->skus); Document::addScriptDeclaration($js); } // Get images (if any), gets all images from /site/storefront/products/$pId $allowedImgExt = array('jpg', 'gif', 'png'); $productImg = array(); $imgWebPath = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . $pId; $imgPath = PATH_APP . $imgWebPath; if (file_exists($imgPath)) { $files = scandir($imgPath); foreach ($files as $file) { if (in_array(pathinfo($file, PATHINFO_EXTENSION), $allowedImgExt)) { if (substr($file, 0, 7) == 'default') { // Let the default image to be the first one array_unshift($productImg, $imgWebPath . DS . $file); } else { $productImg[] = $imgWebPath . DS . $file; } } } } else { $productImg[] = DS . 'site' . DS . 'storefront' . DS . 'products' . DS . 'noimage.png'; } $this->view->productImg = $productImg; $this->view->productAvailable = $productAvailable; //build pathway $this->_buildPathway($product->pName); // Set notifications $this->view->notifications = $pageMessages; $this->view->display(); }