Beispiel #1
0
 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;
 }
Beispiel #2
0
 function hasMaximumQuantity($product, $qty = 1, $isTotal = false)
 {
     //first get the minimum restriction
     $stock = K2StoreInventory::getStock($product->product_id);
     //if not set return true
     if (!isset($stock->max_sale_qty) || $stock->max_sale_qty < 1) {
         return true;
     }
     $product_total = 0;
     $product_total = 0;
     $items = $this->getDataNew();
     foreach ($items as $product_2) {
         if ($product_2['product_id'] == $product->product_id) {
             $product_total += $product_2['quantity'];
         }
     }
     if ($isTotal) {
         //are we checking total qty from update? then override product total
         $new_product_total = $qty;
     } else {
         $new_product_total = $product_total + $qty;
     }
     if ($new_product_total > $stock->max_sale_qty) {
         throw new Exception(JText::sprintf('K2STORE_MAXIMUM_QUANTITY_WARNING', $product->product_name, (int) $stock->max_sale_qty, $product_total));
         return false;
     }
     return true;
 }
Beispiel #3
0
 public static function getAjaxCart($item)
 {
     require_once JPATH_ADMINISTRATOR . '/components/com_k2store/helpers/strapper.php';
     K2StoreStrapper::addJS();
     K2StoreStrapper::addCSS();
     $app = JFactory::getApplication();
     $html = '';
     JLoader::register("K2StoreViewMyCart", JPATH_SITE . "/components/com_k2store/views/mycart/view.html.php");
     $layout = 'addtocart';
     $view = new K2StoreViewMyCart();
     //$view->_basePath = JPATH_ROOT.DS.'components'.DS.'com_k2store';
     $view->addTemplatePath(JPATH_SITE . '/components/com_k2store/views/mycart/tmpl');
     $view->addTemplatePath(JPATH_SITE . '/templates/' . $app->getTemplate() . '/html/com_k2store/mycart');
     require_once JPATH_SITE . '/components/com_k2store/models/mycart.php';
     $model = new K2StoreModelMyCart();
     $product_id = $item->product_id = $item->id;
     $view->assign('_basePath', JPATH_SITE . DS . 'components' . DS . 'com_k2store');
     $view->set('_controller', 'mycart');
     $view->set('_view', 'mycart');
     $view->set('_doTask', true);
     $view->set('hidemenu', true);
     $view->setModel($model, true);
     $view->setLayout($layout);
     $view->assign('product_id', $product_id);
     $config = JComponentHelper::getParams('com_k2store');
     $show_tax = $config->get('show_tax_total', '1');
     //	$show_attributes = $config->get( 'show_product_attributes', 1);
     $view->assign('show_tax', $show_tax);
     $view->assign('params', $config);
     //$view->assign( 'show_attributes', $show_attributes );
     $item->product = $product = self::getItemInfo($product_id);
     $stock = $product->stock;
     if (isset($stock->min_sale_qty) && $stock->min_sale_qty > 1) {
         $item->product_quantity = (int) $stock->min_sale_qty;
         $item->item_minimum_notice = JText::sprintf('K2STORE_MINIMUM_QUANTITY_NOTIFICATION', $item->title, (int) $stock->min_sale_qty);
     } else {
         $item->product_quantity = 1;
     }
     //get attributes
     $attributes = $model->getProductOptions($product_id);
     if (count($attributes) && $product->stock->manage_stock == 1 && K2STORE_PRO == 1 && !$config->get('allow_backorder', 0)) {
         //get unavailable attribute options
         $attributes = $model->processAttributeOptions($attributes, $product);
         //print_r($attributes );
     }
     $item->attributes = $attributes;
     //get k2store product item
     $product = self::getItemInfo($product_id);
     $stock = K2StoreInventory::getStock($product_id);
     //quantity
     //get prices
     $item->prices = K2StorePrices::getPrice($product_id, $item->product_quantity);
     //base price
     $item->price = $item->prices->product_baseprice;
     //tax
     $t = new K2StoreTax();
     //assign tax class to the view. so that we dont have to call it everytime.
     $view->assign('tax_class', $t);
     $tax = $t->getProductTax($item->price, $item->product_id);
     $item->tax = isset($tax) ? $tax : 0;
     //special price
     $item->special_price = isset($item->prices->product_specialprice) ? (double) $item->prices->product_specialprice : null;
     $sp_tax = $t->getProductTax($item->special_price, $item->product_id);
     $item->sp_tax = isset($sp_tax) ? $sp_tax : 0;
     //now get the total stock
     if (K2STORE_PRO == 1) {
         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_k2store/models');
         $qtyModel = JModelLegacy::getInstance('ProductQuantities', 'K2StoreModel');
         $qtyModel->setState('filter_product', $item->product_id);
         $item->product_stock = $qtyModel->getQuantityTotal();
     } else {
         $item->product_stock = '';
     }
     $view->assign('attributes', $attributes);
     $view->assign('product', $product);
     $view->assign('params', $config);
     //trigger onBeforeAddtocart plugin event
     if (!is_object($item->event)) {
         $item->event = new stdClass();
     }
     $item->event->K2StoreBeforeCartDisplay = '';
     $dispatcher = JDispatcher::getInstance();
     JPluginHelper::importPlugin('k2store');
     $results = $dispatcher->trigger("onK2StoreBeforeCartDisplay", array($item));
     $item->event->K2StoreBeforeCartDisplay = trim(implode("\n", $results));
     $view->assign('item', $item);
     ob_start();
     $view->display();
     $html = ob_get_contents();
     ob_end_clean();
     return $html;
 }