public function getList($refresh = false, $getEav = true, $options = array()) { static $pa, $pao; if (empty($pa)) { $pa = array(); } if (empty($pao)) { $pao = array(); } Citruscart::load("CitruscartHelperUser", 'helpers.user'); Citruscart::load('CitruscartHelperSubscription', 'helpers.subscription'); $user_helper = CitruscartHelperBase::getInstance('User'); $product_helper = CitruscartHelperBase::getInstance('Product'); if (empty($this->_list)) { JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables'); $items = parent::getList($refresh); // If no item in the list, return an array() if (empty($items)) { return array(); } foreach ($items as $item) { $filter_group = $user_helper->getUserGroup(JFactory::getUser()->id, $item->product_id); // at this point, ->product_price holds the default price for the product, // but the user may qualify for a discount based on volume or date, so let's get that price override $item->product_price_override = $product_helper->getPrice($item->product_id, $item->product_qty, $filter_group, JFactory::getDate()->toSql()); //checking if we do price override isset($item->product_price_override->override) ? $item->product_price_override = true : ""; if (!empty($item->product_price_override)) { $item->product_price = isset($item->product_price_override->product_price) ? $item->product_price_override->product_price : ""; } if ($item->product_recurs) { $item->recurring_price = $item->product_price; if ($item->subscription_prorated) { $result = CitruscartHelperSubscription::calculateProRatedTrial($item->subscription_prorated_date, $item->subscription_prorated_term, $item->recurring_period_unit, $item->recurring_trial_price, $item->subscription_prorated_charge); $item->product_price = $result['price']; $item->recurring_trial_price = $result['price']; $item->recurring_trial_period_interval = $result['interval']; $item->recurring_trial_period_unit = $result['unit']; $item->recurring_trial = $result['trial']; } else { if ($item->recurring_trial) { $item->product_price = $item->recurring_trial_price; } } } $item->product_parameters = new DSCParameter($item->product_params); $item->orderitem_attributes_price = '0.00000'; $item->orderitem_attributes_weight = '0.00000'; $attributes_names = array(); if (!empty($item->product_attributes)) { $item->attributes = array(); // array of each selected attribute's object $attibutes_array = explode(',', $item->product_attributes); foreach ($attibutes_array as $attrib_id) { if (empty($pao[$attrib_id])) { // load the attrib's object $pao[$attrib_id] = JTable::getInstance('ProductAttributeOptions', 'CitruscartTable'); $pao[$attrib_id]->load($attrib_id); } $table = $pao[$attrib_id]; // update the price // + or - if ($table->productattributeoption_prefix != '=') { $item->product_price = $item->product_price + floatval("{$table->productattributeoption_prefix}" . "{$table->productattributeoption_price}"); // store the attribute's price impact $item->orderitem_attributes_price = $item->orderitem_attributes_price + floatval("{$table->productattributeoption_prefix}" . "{$table->productattributeoption_price}"); //$item->product_price_override->override = true; $item->product_price_override = true; } else { // assign the product attribute price as the product price //then set the orderitem_attributes_price to 0.0000 $item->product_price = $table->productattributeoption_price; // // store the attribute's price impact $item->orderitem_attributes_price = "0.00000"; $item->product_price_override->override = false; } // update the weight // + or - if ($table->productattributeoption_prefix_weight != '=') { $item->product_weight = $item->product_weight + floatval("{$table->productattributeoption_prefix_weight}" . "{$table->productattributeoption_weight}"); // store the attribute's price impact $item->orderitem_attributes_weight = $item->orderitem_attributes_weight + floatval("{$table->productattributeoption_prefix_weight}" . "{$table->productattributeoption_weight}"); } else { // assign the product attribute price as the product price //then set the orderitem_attributes_price to 0.0000 $item->product_weight = $table->productattributeoption_weight; // // store the attribute's price impact $item->orderitem_attributes_weight = "0.00000"; } // update product values based on the selected option $m_values = DSCModel::getInstance('ProductAttributeOptionValues', 'CitruscartModel'); $m_values->setState('filter_option', $attrib_id); $list_values = $m_values->getList(); if (count($list_values)) { foreach ($list_values as $val) { switch ($val->productattributeoptionvalue_field) { case 'product_model': case 'product_sku': $field = $val->productattributeoptionvalue_field; switch ($val->productattributeoptionvalue_operator) { case 'prepend': $item->{$field} = $val->productattributeoptionvalue_value . $item->{$field}; break; case 'append': $item->{$field} = $item->{$field} . $val->productattributeoptionvalue_value; break; case 'replace': $item->{$field} = $val->productattributeoptionvalue_value; break; } break; } } } $item->orderitem_attributes_price = number_format($item->orderitem_attributes_price, '5', '.', ''); $item->orderitem_attributes_weight = number_format($item->orderitem_attributes_weight, '5', '.', ''); // store a csv of the attrib names, built by Attribute name + Attribute option name if (empty($pa[$table->productattribute_id])) { $pa[$table->productattribute_id] = JTable::getInstance('ProductAttributes', 'CitruscartTable'); $pa[$table->productattribute_id]->load($table->productattribute_id); } $atable = $pa[$table->productattribute_id]; if (!empty($atable->productattribute_id)) { $name = JText::_($atable->productattribute_name) . ': ' . JText::_($table->productattributeoption_name); $attributes_names[] = $name; } else { $attributes_names[] = JText::_($table->productattributeoption_name); } } // Generate Product Sku based upon attributes $item->product_sku = CitruscartHelperProduct::getProductSKU($item, $attibutes_array); // Could someone explain to me why this is necessary? if ($item->orderitem_attributes_price >= 0) { // formatted for storage in the DB $item->orderitem_attributes_price = "+{$item->orderitem_attributes_price}"; } } $item->attributes_names = implode(', ', $attributes_names); $this->prepareItem($item, 0, $refresh); } $this->_list = $items; } return $this->_list; }
/** * Gets a product's add to cart section * formatted for display * * @param int $address_id * @return string html */ function getAddToCart($product_id, $values = array()) { $input = JFactory::getApplication()->input; $html = ''; $view = $this->getView('products', 'html'); //$model = $this->getModel( $this->get('suffix') ); $model = JModelLegacy::getInstance('Products', 'CitruscartModel'); $model->setId($product_id); Citruscart::load('CitruscartHelperUser', 'helpers.user'); $user_id = JFactory::getUser()->id; $filter_group = CitruscartHelperUser::getUserGroup($user_id); $model->setState('filter_group', $filter_group); //$model->_item = ''; $row = $model->getItem(false); if ($row->product_notforsale || Citruscart::getInstance()->get('shop_enabled') == '0') { return $html; } $view->set('_controller', 'products'); $view->set('_view', 'products'); $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->setLayout('product_buy'); $view->assign('item', $row); $view->assign('product_id', $product_id); $view->assign('values', $values); $filter_category = $model->getState('filter_category', $input->getInt('filter_category', (int) @$values['filter_category'])); $view->assign('filter_category', $filter_category); $view->assign('validation', "index.php?option=com_citruscart&view=products&task=validate&format=raw"); $config = Citruscart::getInstance(); $show_tax = $config->get('display_prices_with_tax'); $view->assign('show_tax', $show_tax); $view->assign('tax', 0); $view->assign('taxtotal', ''); $view->assign('shipping_cost_link', ''); $row->tax = '0'; if ($show_tax) { // finish CitruscartHelperUser::getGeoZone -- that's why this isn't working Citruscart::load('CitruscartHelperUser', 'helpers.user'); $geozones = CitruscartHelperUser::getGeoZones(JFactory::getUser()->id); if (empty($geozones)) { // use the default $table = JTable::getInstance('Geozones', 'CitruscartTable'); $table->load(array('geozone_id' => Citruscart::getInstance()->get('default_tax_geozone'))); $geozones = array($table); } $taxtotal = CitruscartHelperProduct::getTaxTotal($product_id, $geozones); $tax = $taxtotal->tax_total; $row->taxtotal = $taxtotal; $row->tax = $tax; $view->assign('taxtotal', $taxtotal); $view->assign('tax', $tax); } // 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->assign('shipping_cost_link', $shipping_cost_link); } $invalidQuantity = '0'; if (empty($values)) { $product_qty = '1'; // get the default set of attribute_csv $default_attributes = CitruscartHelperProduct::getDefaultAttributes($product_id); sort($default_attributes); $attributes_csv = implode(',', $default_attributes); $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $invalidQuantity = '1'; } } if (!empty($values)) { $product_id = !empty($values['product_id']) ? (int) $values['product_id'] : $input->getInt('product_id'); $product_qty = !empty($values['product_qty']) ? (int) $values['product_qty'] : '1'; // TODO only display attributes available based on the first selected attribute? $attributes = array(); foreach ($values as $key => $value) { if (substr($key, 0, 10) == 'attribute_') { $attributes[] = $value; } } sort($attributes); $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 = Citruscart::getClass('CitruscartHelperProduct', 'helpers.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 attributes $table = JTable::getInstance('ProductAttributeOptions', 'CitruscartTable'); $attrs = array(); foreach ($attributes as $attrib_id) { // load the attrib's object $table->load($attrib_id); // update the price //$row->price = $row->price + floatval( "$table->productattributeoption_prefix"."$table->productattributeoption_price"); // is not + or - if ($table->productattributeoption_prefix == '=') { $row->price = floatval($table->productattributeoption_price); } else { $row->price = $row->price + floatval("{$table->productattributeoption_prefix}" . "{$table->productattributeoption_price}"); } $attrs[] = $table->productattributeoption_id; } $row->sku = CitruscartHelperProduct::getProductSKU($row, $attrs); $view->assign('item', $row); } $view->assign('availableQuantity', $availableQuantity); $view->assign('invalidQuantity', $invalidQuantity); ob_start(); JFactory::getApplication()->triggerEvent('onDisplayProductAttributeOptions', array($row->product_id)); $view->assign('onDisplayProductAttributeOptions', ob_get_contents()); ob_end_clean(); ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); return $html; }