/** * Get the cart button form for a specific product * * @param int $product_id The id of the product * @return html The add to cart form */ public static function getCartButton($product_id, $layout = 'product_buy', $values = array(), &$callback_js = '') { /* Get the application */ $app = JFactory::getApplication(); if (is_array($values) && !count($values)) { $values = $app->input->get('request'); //$values = JRequest::get( 'request' ); } $html = ''; $page = $app->input->get('page', 'product'); //$page = JRequest::getVar( 'page', 'product' ); $isPOS = $page == 'pos'; if ($isPOS) { JLoader::register("CitruscartViewPOS", JPATH_ADMINISTRATOR . "/components/com_citruscart/views/pos/view.html.php"); $view = new CitruscartViewPOS(); } else { JLoader::register("CitruscartViewProducts", JPATH_SITE . "/components/com_citruscart/views/products/view.html.php"); $view = new CitruscartViewProducts(); } $model = JModelLegacy::getInstance('Products', 'CitruscartModel'); $model->setId($product_id); $model->setState('task', 'product_buy'); Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper_product = CitruscartHelperBase::getInstance('Product'); Citruscart::load('CitruscartHelperUser', 'helpers.user'); $user_id = JFactory::getUser()->id; if ($isPOS) { $user_id = $app->input->getInt('user_id', $user_id); //$user_id = JRequest::getInt( 'user_id', $user_id ); } $filter_group = CitruscartHelperUser::getUserGroup($user_id, $product_id); $qty = isset($values['product_qty']) && !empty($values['product_qty']) ? $values['product_qty'] : 1; $model->setState('filter_group', $filter_group); $model->setState('product.qty', $qty); $model->setState('user.id', $user_id); $row = $model->getItem(false, true, false); if ($row->product_notforsale || Citruscart::getInstance()->get('shop_enabled') == '0') { return $html; } // This enable this helper method to be used outside of Citruscart if ($isPOS) { $view->set('_controller', 'pos'); $view->set('_view', 'pos'); } else { $view->addTemplatePath(JPATH_SITE . '/components/com_citruscart/views/products/tmpl'); $view->addTemplatePath(JPATH_SITE . '/templates/' . JFactory::getApplication('site')->getTemplate() . '/html/com_citruscart/products/'); // add extra templates $view->addTemplatePath(Citruscart::getPath('product_buy_templates')); $view->set('_controller', 'products'); $view->set('_view', 'products'); } $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->setLayout($layout); $view->product_id = $product_id; $view->values = $values; $filter_category = $model->getState('filter_category', $app->input->getInt('filter_category', 0)); //$filter_category = $model->getState( 'filter_category', JRequest::getInt( 'filter_category', ( int ) $values['filter_category'] ) ); $view->filter_category = $filter_category; if ($isPOS) { $view->validation = "index.php?option=com_citruscart&view=pos&task=validate&format=raw"; } else { $view->validation = "index.php?option=com_citruscart&view=products&task=validate&format=raw"; } $config = Citruscart::getInstance(); // TODO What about this?? $show_shipping = $config->get('display_prices_with_shipping'); if ($show_shipping) { $article_link = $config->get('article_shipping', ''); $shipping_cost_link = JRoute::_('index.php?option=com_content&view=article&id=' . $article_link); $view->shipping_cost_link = $shipping_cost_link; } $quantity_min = 1; if ($row->quantity_restriction) { $quantity_min = $row->quantity_min; } $invalidQuantity = '0'; $attributes = array(); $attr_orig = array(); if (empty($values)) { $product_qty = $quantity_min; // get the default set of attribute_csv if (!isset($row->default_attributes)) { $default_attributes = $helper_product->getDefaultAttributes($product_id); } else { $default_attributes = $row->default_attributes; } sort($default_attributes); $attributes_csv = implode(',', $default_attributes); $availableQuantity = $helper_product->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $invalidQuantity = '1'; } $attr_orig = $attributes = $default_attributes; } else { $product_qty = !empty($values['product_qty']) ? (int) $values['product_qty'] : $quantity_min; // TODO only display attributes available based on the first selected attribute? foreach ($values as $key => $value) { if (substr($key, 0, 10) == 'attribute_') { if (empty($value)) { $attributes[$key] = 0; } else { $attributes[$key] = $value; } } } if (!count($attributes)) { // no attributes are selected -> use default if (!isset($row->default_attributes)) { $attributes = $helper_product->getDefaultAttributes($product_id); } else { $attributes = $row->default_attributes; } } $attr_orig = $attributes; sort($attributes); // Add 0 to attributes to include all the root attributes //$attributes[] = 0;//remove this one. its causing the getAvailableQuantity to not get quantity because of wrong csv // For getting child opts $view->selected_opts = json_encode(array_merge($attributes, array('0'))); $attributes_csv = implode(',', $attributes); // Integrity checks on quantity being added if ($product_qty < 0) { $product_qty = '1'; } // using a helper file to determine the product's information related to inventory $availableQuantity = $helper_product->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $invalidQuantity = '1'; } } // adjust the displayed price based on the selected or default attributes CitruscartHelperProduct::calculateProductAttributeProperty($row, $attr_orig, 'price', 'product_weight'); $show_tax = $config->get('display_prices_with_tax'); $show_product = $config->get('display_category_cartbuttons'); $view->show_tax = $show_tax; $row->tax = '0'; $row->taxtotal = '0'; if ($show_tax) { // finish CitruscartHelperUser::getGeoZone -- that's why this isn't working Citruscart::load('CitruscartHelperUser', 'helpers.user'); $geozones_user = CitruscartHelperUser::getGeoZones($user_id); if (empty($geozones_user)) { $geozones = array(Citruscart::getInstance()->get('default_tax_geozone')); } else { $geozones = array(); foreach ($geozones_user as $value) { $geozones[] = $value->geozone_id; } } Citruscart::load('CitruscartHelperTax', 'helpers.tax'); $product = new stdClass(); $product->product_price = $row->price; $product->product_id = $product_id; $tax = CitruscartHelperTax::calculateGeozonesTax(array($product), 2, $geozones); $row->taxtotal = $tax->tax_total; $row->tax = $tax->tax_total; } $row->_product_quantity = $product_qty; if ($page == 'product' || $isPOS) { $display_cartbutton = Citruscart::getInstance()->get('display_product_cartbuttons', '1'); } else { $display_cartbutton = Citruscart::getInstance()->get('display_category_cartbuttons', '1'); } $view->page = $page; $view->display_cartbutton = $display_cartbutton; $view->availableQuantity = $availableQuantity; $view->invalidQuantity = $invalidQuantity; if ($isPOS) { $view->product = $row; } else { $view->item = $row; } $dispatcher = JDispatcher::getInstance(); ob_start(); JFactory::getApplication()->triggerEvent('onDisplayProductAttributeOptions', array($row->product_id)); $view->onDisplayProductAttributeOptions = ob_get_contents(); ob_end_clean(); $html = $view->loadTemplate(); if (isset($view->callback_js) && !empty($view->callback_js)) { $callback_js = $view->callback_js; } return $html; }