public function hasProductStock($product_id, $options = array(), $qty = 0, $key = '', $isTotal = false, $type = "update", $original_qty = 0) { $params = JComponentHelper::getParams('com_k2store'); $status = true; $inventory = new K2StoreInventory(); //check if backorder is enabled. If yes, then return true if ($params->get('allow_backorder', 0)) { return $status; } //get the existing cart items $products = $this->getDataNew(); //get existing items in a CSV format $allOptions = $this->getAllAttributes($products); if ($type == 'add' || $type == 'update') { //this request either comes from adding a product to cart or update if (count($options)) { $options = $this->cleanAttributes($options, $product_id); } } $attributes_csv = ''; if (count($options)) { $attribs = $options; sort($attribs); $attributes_csv = implode(',', $attribs); } //echo $attributes_csv; //when we recheck options, we need to get the correct quantity if ($type == 'update') { $difference = $qty - $original_qty; if (count($options)) { if (isset($allOptions[$attributes_csv])) { $existing_qty = $allOptions[$attributes_csv]; $qty = $existing_qty + $difference; } } } // get the right quantity for checking if ($type == 'add') { if (count($options)) { if (isset($allOptions[$attributes_csv])) { $qty = $allOptions[$attributes_csv] + $qty; } } else { $product_total = 0; foreach ($products as $product_2) { if ($product_2['product_id'] == $product_id) { $product_total += $product_2['quantity']; } } $qty = $product_total + $qty; } } $availableQuantity = K2StoreInventory::getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->manage_stock && $qty > $availableQuantity->quantity) { $status = false; } else { $status = true; } return $status; }
public static function isAllowed($item, $qty = 1, $attributes_csv = '') { $params = JComponentHelper::getParams('com_k2store'); //set the result object $result = new JObject(); $result->backorder = false; //we always want to allow users to buy. so initialise to 1. $result->can_allow = 1; //if basic version return true if (K2STORE_PRO != 1) { $result->can_allow = 1; return $result; } //first check if global inventory is enabled. if (!$params->get('enable_inventory', 0)) { //if not enabled, allow adding and return here $result->can_allow = 1; return $result; } //global inventory enabled. Now check at the product level. if (is_object($item->product->stock)) { $stock = $item->product->stock; } else { $stock = K2StoreInventory::getStock($item->product_id); } if ((int) $stock->manage_stock < 1) { //Stock disabled. Return true $result->can_allow = 1; return $result; } if (empty($attributes_csv)) { $model = new K2StoreModelMyCart(); //get attributes $attributes = $model->getProductOptions($item->product_id); $list = array(); foreach ($attributes as $option) { if ($option['type'] == 'select' || $option['type'] == 'radio') { if (isset($option['manage_stock']) && $option['manage_stock'] == 1) { $option['product_option_id']; //get the default attributes foreach ($option['optionvalue'] as $optionvalue) { if ($optionvalue['product_optionvalue_default']) { //we have a default option $list[$option['product_option_id']] = $optionvalue['product_optionvalue_id']; } } if (empty($list[$option['product_option_id']])) { $list[$option['product_option_id']] = $option['optionvalue'][0]['product_optionvalue_id']; } } } } sort($list); $attributes_csv = implode(',', $list); } $availableQuantity = K2StoreInventory::getAvailableQuantity($item->product_id, $attributes_csv); if ($availableQuantity->manage_stock && $qty > $availableQuantity->quantity) { $result->can_allow = 0; } $quantity_min = 1; if ($stock->min_out_qty) { $quantity_min = $stock->min_out_qty; } if ($quantity_min >= $availableQuantity->quantity) { $result->can_allow = 0; } if ($availableQuantity->quantity <= 0) { $result->can_allow = 0; } //TODO:: is there a better way of doing this. We are checking the product's total stock if ($item->product_stock > 0) { $result->can_allow = 1; } else { $result->can_allow = 0; } if ($quantity_min >= $item->product_stock) { $result->can_allow = 0; } //if backorder is allowed, set it and override to allow adding if ($params->get('enable_inventory', 0) && $params->get('allow_backorder', 0)) { $result->backorder = true; } return $result; }