public static function getCurrentCountry($customer_id, JeproshopCartModelCart $cart = null) { if (!$cart) { $cart = JeproshopContext::getContext()->cart; } if (!$cart || !$cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}) { $db = JFactory::getDBO(); $query = "SELECT " . $db->quoteName('address_id') . " FROM " . $db->quoteName('#__jeproshop_address') . " WHERE " . $db->quoteName('customer_id') . " = " . (int) $customer_id . " AND " . $db->quoteName('deleted') . " = 0 ORDER BY " . $db->quoteName('address_id'); $db->setQuery($query); $address_id = (int) $db->loadResult(); } else { $address_id = $cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}; } $address = JeproshopAddressModelAddress::getCountryAndState($address_id); return (int) $address->country_id ? $address->country_id : JeproshopSettingModelSetting::getValue('default_country'); }
/** * Get product price * * @param integer $product_id Product id * @param boolean $use_tax With taxes or not (optional) * @param integer $product_attribute_id Product attribute id (optional). * If set to false, do not apply the combination price impact. NULL does apply the default combination price impact. * @param integer $decimals Number of decimals (optional) * @param boolean $only_reduction Returns only the reduction amount * @param boolean $use_reduction Set if the returned amount will include reduction * @param integer $quantity Required for quantity discount application (default value: 1) * @param integer $customer_id Customer ID (for customer group reduction) * @param integer $cart_id Cart ID. Required when the cookie is not accessible (e.g., inside a payment module, a cron task...) * @param integer $address_id Customer address ID. Required for price (tax included) calculation regarding the guest localization * @param null $specific_price_output * @param boolean $with_ecotax insert ecotax in price output. * @param bool $use_group_reduction * @param JeproshopContext $context * @param bool $use_customer_price * @internal param int $divisor Useful when paying many time without fees (optional) * @internal param \variable_reference $specificPriceOutput . * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @return float Product price */ public static function getStaticPrice($product_id, $use_tax = true, $product_attribute_id = null, $decimals = 6, $only_reduction = false, $use_reduction = true, $quantity = 1, $customer_id = null, $cart_id = null, $address_id = null, $specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, JeproshopContext $context = null, $use_customer_price = true) { if (!$context) { $context = JeproshopContext::getContext(); } $cur_cart = $context->cart; if (!JeproshopTools::isBool($use_tax) || !JeproshopTools::isUnsignedInt($product_id)) { //die(Tools::displayError()); } // Initializations $group_id = (int) JeproshopGroupModelGroup::getCurrent()->group_id; // If there is cart in context or if the specified id_cart is different from the context cart id if (!is_object($cur_cart) || JeproshopTools::isUnsignedInt($cart_id) && $cart_id && $cur_cart->cart_id != $cart_id) { /* * When a user (e.g., guest, customer, Google...) is on Jeproshop, he has already its cart as the global (see /init.php) * When a non-user calls directly this method (e.g., payment module...) is on JeproShop, he does not have already it BUT knows the cart ID * When called from the back office, cart ID can be inexistant */ if (!$cart_id && !isset($context->employee)) { JError::raiseError(500, __FILE__ . ' ' . __LINE__); } $cur_cart = new JeproshopCartModelCart($cart_id); // Store cart in context to avoid multiple instantiations in BO if (!JeproshopTools::isLoadedObject($context->cart, 'cart_id')) { $context->cart = $cur_cart; } } $db = JFactory::getDBO(); $cart_quantity = 0; if ((int) $cart_id) { $cache_id = 'jeproshop_product_model_get_price_static_' . (int) $product_id . '_' . (int) $cart_id; $cart_qty = JeproshopCache::retrieve($cache_id); if (!JeproshopCache::isStored($cache_id) || $cart_qty != (int) $quantity) { $query = "SELECT SUM(" . $db->quoteName('quantity') . ") FROM " . $db->quoteName('#__jeproshop_cart_product'); $query .= " WHERE " . $db->quoteName('product_id') . " = " . (int) $product_id . " AND " . $db->quoteName('cart_id'); $query .= " = " . (int) $cart_id; $db->setQuery($query); $cart_quantity = (int) $db->loadResult(); JeproshopCache::store($cache_id, $cart_quantity); } $cart_quantity = JeproshopCache::retrieve($cache_id); } $currency_id = (int) JeproshopTools::isLoadedObject($context->currency, 'currency_id') ? $context->currency->currency_id : JeproshopSettingModelSetting::getValue('default_currency'); // retrieve address information $country_id = (int) $context->country->country_id; $state_id = 0; $zipcode = 0; if (!$address_id && JeproshopTools::isLoadedObject($cur_cart, 'cart_id')) { $address_id = $cur_cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}; } if ($address_id) { $address_info = JeproshopAddressModelAddress::getCountryAndState($address_id); if ($address_info->country_id) { $country_id = (int) $address_info->country_id; $state_id = (int) $address_info->state_id; $zipcode = $address_info->postcode; } } else { if (isset($context->customer->geoloc_country_id)) { $country_id = (int) $context->customer->geoloc_country_id; $state_id = (int) $context->customer->state_id; $zipcode = (int) $context->customer->postcode; } } if (JeproshopTaxModelTax::taxExcludedOption()) { $use_tax = false; } if ($use_tax != false && !empty($address_info->vat_number) && $address_info->country_id != JeproshopSettingModelSetting::getValue('vat_number_country') && JeproshopSettingModelSetting::getValue('vat_number_management')) { $use_tax = false; } if (is_null($customer_id) && JeproshopTools::isLoadedObject($context->customer, 'customer_id')) { $customer_id = $context->customer->customer_id; } return JeproshopProductModelProduct::priceCalculation($context->shop->shop_id, $product_id, $product_attribute_id, $country_id, $state_id, $zipcode, $currency_id, $group_id, $quantity, $use_tax, $decimals, $only_reduction, $use_reduction, $with_ecotax, $specific_price_output, $use_group_reduction, $customer_id, $use_customer_price, $cart_id, $cart_quantity); }
public function initialize() { if (self::$initialized) { return; } self::$initialized = true; $app = JFactory::getApplication(); $context = JeproshopContext::getContext(); $view = $app->input->get('view', 'default'); $viewClass = $this->getView($view, JFactory::getDocument()->getType()); if (JeproshopTools::usingSecureMode()) { $this->ssl_enabled = true; } if (isset($context->cookie->account_created)) { $accountCreated = true; $viewClass->assignRef('account_created', $accountCreated); $context->cookie->account_created = 0; } JeproshopTools::setCookieLanguage($context->cookie); $cart_id = (int) $this->recoverCart(); if ($cart_id) { $context->cookie->cart_id = $cart_id; } if ($this->authenticated && !$context->customer->isLogged($this->guest_allowed)) { $app->redirect('index.php?option=com_jeproshop&view=authentication'); // todo add retun option } if (JeproshopSettingModelSetting::getValue('enable_geolocation')) { $defaultCountry = $this->geolocationManagement($context->country); if ($defaultCountry && JeproshopTools::isLoadedObject($defaultCountry, 'country_id')) { $context->country = $defaultCountry; } } $currency = JeproshopTools::setCurrency($context->cookie); $logout = $app->input->get('logout'); $myLogout = $app->input->get('mylogout'); if (isset($logout) || $context->customer->logged && JeproshopCustomerModelCustomer::isBanned($context->cutomer->customer_id)) { $context->customer->logout(); //$app->input->get('') } elseif (isset($myLogout)) { $context->customer->mylogout(); } if ((int) $context->cookie->cart_id) { $cart = new JeproshopCartModelCart($context->cookie->cart_id); if ($cart->orderExists()) { $context->cookie->cart_id = null; $context->cookie->check_selling_condition = false; } elseif ((int) JeproshopSettingModelSetting::getValue('enable_geolocation') && !in_array(strtoupper($context->cookie->iso_code_country), explode(';', JeproshopSettingModelSetting::getValue('allowed_countries'))) && $cart->numberOfProducts() && (int) JeproshopSettingModelSetting::getValue('geolocation_behavior') != -1 && !self::isInWhiteListForGeolocation() && !in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1'))) { $context->cookie->cart_id = null; $cart = null; } elseif ($context->cookie->customer_id != $cart->customer_id || $context->cookie->lang_id != $cart->lang_id || $currency->currency_id != $cart->currency_id) { if ($context->cookie->customer_id) { $cart->customer_id = (int) $context->cookie->customer_id; } $cart->lang_id = (int) $context->cookie->lang_id; $cart->currency_id = (int) $currency->currency_id; $cart->update(); } if (isset($cart) && (!isset($cart->address_delivery_id) || $cart->address_delivery_id == 0 || !isset($cart->address_invoice_id) || $cart->address_invoice_id) && $context->cookie->customer_id) { $toUpdate = false; if (!isset($cart->address_delivery_id) || $cart->address_delivery_id == 0) { $toUpdate = true; $cart->address_delivery_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id); } if (!isset($cart->address_invoice_id) || $cart->address_invoice_id == 0) { $toUpdate = true; $cart->address_invoice_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id); } if ($toUpdate) { $cart->update(); } } } if (!isset($cart) || $cart->cart_id) { $cart = new JeproshopCartModelCart(); $cart->lang_id = (int) $context->cookie->lang_id; $cart->currency_id = (int) $context->cookie->currency_id; $cart->guest_id = (int) $context->cookie->guest_id; $cart->shop_group_id = (int) $context->shop->shop_group_id; $cart->shop_id = $context->shop->shop_id; if ($context->cookie->customer_id) { $cart->customer_id = (int) $context->cookie->id_customer; $cart->address_delivery_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id); $cart->address_invoice_id = $cart->address_delivery_id; } else { $cart->address_delivery_id = 0; $cart->address_invoice_id = 0; } // Needed if the merchant want to give a free product to every visitors $context->cart = $cart; JeproshopCartRuleModelCartRule::autoAddToCart($context); } else { $context->cart = $cart; } JeproshopProductModelProduct::initPricesComputation(); $display_tax_label = $context->country->display_tax_label; if (isset($cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}) && $cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}) { $info = JeproshopAddressModelAddress::getCountryAndState($cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}); $country = new JeproshopCountryModelCountry((int) $info->country_id); $context->country = $country; if (JeproshopTools::isLoadedObject($country, 'country_id')) { $display_tax_label = $country->display_tax_label; } } $languages = JeproshopLanguageModelLanguage::getLanguages(true); $meta_language = array(); foreach ($languages as $lang) { $meta_language[] = $lang->iso_code; } $compared_products = array(); $comparatorMaxItem = JeproshopSettingModelSetting::getValue('comparator_max_item'); if ($comparatorMaxItem && isset($context->cookie->compare_id)) { $compared_products = JeproshopProductComparedModelProductCompared::getComparedProducts($context->cookie->compare_id); } $mobileDevice = $context->getMobileDevice(); $viewClass->assignRef('mobile_device', $mobileDevice); $viewClass->assignRef('cart', $cart); $viewClass->assignRef('currency', $currency); $viewClass->assignRef('display_tax_label', $display_tax_label); $isLogged = (bool) $context->customer->isLogged(); $viewClass->assignRef('is_logged', $isLogged); $isGuest = (bool) $context->customer->isGuest(); $viewClass->assignRef('is_guest', $isGuest); $priceRoundMode = JeproshopSettingModelSetting::getValue('price_round_mode'); $viewClass->assignRef('price_round_mode', $priceRoundMode); $useTax = JeproshopSettingModelSetting::getValue('use_tax'); $viewClass->assignRef('use_taxes', $useTax); $showTax = (int) JeproshopSettingModelSetting::getValue('display_tax') == 1 && JeproshopSettingModelSetting::getValue('use_tax'); $viewClass->assignRef('show_tax', $showTax); $catalogMode = (bool) JeproshopSettingModelSetting::getValue('catalog_mode') || !JeproshopGroupModelGroup::getCurrent()->show_prices; $viewClass->assignRef('catalog_mode', $catalogMode); $enableB2bMode = (bool) JeproshopSettingModelSetting::getValue('enable_b2b_mode'); $viewClass->assignRef('enable_b2b_mode', $enableB2bMode); $stockManagement = JeproshopSettingModelSetting::getValue('stock_management'); $viewClass->assignRef('stock_management', $stockManagement); $metaLanguages = implode(',', $meta_language); $viewClass->assignRef('meta_languages', $metaLanguages); $viewClass->assignRef('languages', $languages); $numberOfProducts = $cart->numberOfProducts(); $viewClass->assignRef('cart_quantities', $numberOfProducts); $currencies = JeproshopCurrencyModelCurrency::getCurrencies(); $viewClass->assignRef('currencies', $currencies); $comparatorMaxItem = JeproshopSettingModelSetting::getValue('comparator_max_item'); $viewClass->assignRef('comparator_max_item', $comparatorMaxItem); $quickView = (bool) JeproshopSettingModelSetting::getValue('quick_view'); $viewClass->assignRef('quick_view', $quickView); $restrictedCountryMode = false; $viewClass->assignRef('restricted_country_mode', $restrictedCountryMode); $displayPrice = JeproshopProductModelProduct::getTaxCalculationMethod((int) $context->cookie->customer_id); $viewClass->assignRef('display_price', $displayPrice); /*$viewClass->assignRef(''); $viewClass->assignRef(''); $viewClass->assignRef('');*/ $viewClass->assignRef('compared_products', $compared_products); /*$viewClass->assignRef('comparator_max_item', $comparatorMaxItem); */ }