public function getProduct($id) { if ($id) { $query = 'SELECT p.price, p.id_tax_rules_group, l.name, l.description FROM ' . _DB_PREFIX_ . 'product p LEFT JOIN ' . _DB_PREFIX_ . 'product_lang l ON p.id_product = l.id_product where p.id_product = ' . $id . ' and l.id_lang = 6;'; $prod = Db::getInstance()->ExecuteS($query); // check provision $this->provision = htmlentities(Configuration::get('JB_APROVISION'), ENT_QUOTES, 'UTF-8'); // TODO $id_currency = 4; $specific_price = null; $price = Product::priceCalculation(1, $id, null, 14, 0, 0, $id_currency, _PS_DEFAULT_CUSTOMER_GROUP_, 1, true, 6, false, true, true, $specific_price, true); if ($this->provision && is_numeric($this->provision)) { $old_price = $price; $price = $price * (1 + $this->provision / 100); } if ($prod[0]['description'] == '') { $desc = $prod[0]['name']; } else { $desc = $prod[0]['description']; } $data = array('name' => $prod[0]['name'], 'price' => round($price, 2), 'desc' => '<h1>' . $prod[0]['name'] . '</h1><br />' . $desc); if ($this->provision && is_numeric($this->provision)) { $data['old_price'] = $old_price; } return $data; } }
public function specificPriceCalculation($parameters) { $arr_return = array(); foreach ($parameters as $name => $value) { $id_shop = (int) Context::getContext()->shop->id; $id_country = (int) (isset($value['country']) ? $value['country'] : Configuration::get('PS_COUNTRY_DEFAULT')); $id_state = (int) (isset($value['state']) ? $value['state'] : 0); $id_currency = (int) (isset($value['currency']) ? $value['currency'] : Configuration::get('PS_CURRENCY_DEFAULT')); $id_group = (int) (isset($value['group']) ? $value['group'] : (int) Configuration::get('PS_CUSTOMER_GROUP')); $quantity = (int) (isset($value['quantity']) ? $value['quantity'] : 1); $use_tax = (int) (isset($value['use_tax']) ? $value['use_tax'] : Configuration::get('PS_TAX')); $decimals = (int) (isset($value['decimals']) ? $value['decimals'] : Configuration::get('PS_PRICE_ROUND_MODE')); $id_product_attribute = (int) (isset($value['product_attribute']) ? $value['product_attribute'] : null); $id_county = (int) (isset($value['county']) ? $value['county'] : null); $only_reduc = (int) (isset($value['only_reduction']) ? $value['only_reduction'] : false); $use_reduc = (int) (isset($value['use_reduction']) ? $value['use_reduction'] : true); $use_ecotax = (int) (isset($value['use_ecotax']) ? $value['use_ecotax'] : Configuration::get('PS_USE_ECOTAX')); $specific_price_output = null; $id_county = isset($value['county']) ? $value['county'] : 0; $return_value = Product::priceCalculation($id_shop, $value['object_id'], $id_product_attribute, $id_country, $id_state, $id_county, $id_currency, $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $use_ecotax, $specific_price_output, null); $arr_return[$name] = array('sqlId' => strtolower($name), 'value' => $return_value); } return $arr_return; }
public static function indexProductPrices($id_product, $smart = true) { static $groups = null; if (is_null($groups)) { $groups = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_group FROM `' . _DB_PREFIX_ . 'group_reduction`'); if (!$groups) { $groups = array(); } } $shop_list = Shop::getShops(false, null, true); foreach ($shop_list as $id_shop) { static $currency_list = null; if (is_null($currency_list)) { $currency_list = Currency::getCurrencies(false, 1, new Shop($id_shop)); } $min_price = array(); $max_price = array(); if ($smart) { Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'layered_price_index` WHERE `id_product` = ' . (int) $id_product . ' AND `id_shop` = ' . (int) $id_shop); } if (Configuration::get('PS_LAYERED_FILTER_PRICE_USETAX')) { $max_tax_rate = Db::getInstance()->getValue(' SELECT max(t.rate) max_rate FROM `' . _DB_PREFIX_ . 'product_shop` p LEFT JOIN `' . _DB_PREFIX_ . 'tax_rules_group` trg ON (trg.id_tax_rules_group = p.id_tax_rules_group AND p.id_shop = ' . (int) $shop_list . ') LEFT JOIN `' . _DB_PREFIX_ . 'tax_rule` tr ON (tr.id_tax_rules_group = trg.id_tax_rules_group) LEFT JOIN `' . _DB_PREFIX_ . 'tax` t ON (t.id_tax = tr.id_tax AND t.active = 1) WHERE id_product = ' . (int) $id_product . ' GROUP BY id_product'); } else { $max_tax_rate = 0; } $product_min_prices = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT id_shop, id_currency, id_country, id_group, from_quantity FROM `' . _DB_PREFIX_ . 'specific_price` WHERE id_product = ' . (int) $id_product); // Get min price foreach ($currency_list as $currency) { $price = Product::priceCalculation($id_shop, (int) $id_product, null, null, null, null, $currency['id_currency'], null, null, false, 6, false, true, true, $specific_price_output, true); if (!isset($max_price[$currency['id_currency']])) { $max_price[$currency['id_currency']] = 0; } if (!isset($min_price[$currency['id_currency']])) { $min_price[$currency['id_currency']] = null; } if ($price > $max_price[$currency['id_currency']]) { $max_price[$currency['id_currency']] = $price; } if ($price == 0) { continue; } if (is_null($min_price[$currency['id_currency']]) || $price < $min_price[$currency['id_currency']]) { $min_price[$currency['id_currency']] = $price; } } foreach ($product_min_prices as $specific_price) { foreach ($currency_list as $currency) { if ($specific_price['id_currency'] && $specific_price['id_currency'] != $currency['id_currency']) { continue; } $price = Product::priceCalculation($specific_price['id_shop'] == 0 ? null : (int) $specific_price['id_shop'], (int) $id_product, null, $specific_price['id_country'] == 0 ? null : $specific_price['id_country'], null, null, $currency['id_currency'], $specific_price['id_group'] == 0 ? null : $specific_price['id_group'], $specific_price['from_quantity'], false, 6, false, true, true, $specific_price_output, true); if (!isset($max_price[$currency['id_currency']])) { $max_price[$currency['id_currency']] = 0; } if (!isset($min_price[$currency['id_currency']])) { $min_price[$currency['id_currency']] = null; } if ($price > $max_price[$currency['id_currency']]) { $max_price[$currency['id_currency']] = $price; } if ($price == 0) { continue; } if (is_null($min_price[$currency['id_currency']]) || $price < $min_price[$currency['id_currency']]) { $min_price[$currency['id_currency']] = $price; } } } foreach ($groups as $group) { foreach ($currency_list as $currency) { $price = Product::priceCalculation(null, (int) $id_product, null, null, null, null, (int) $currency['id_currency'], (int) $group['id_group'], null, false, 6, false, true, true, $specific_price_output, true); if (!isset($max_price[$currency['id_currency']])) { $max_price[$currency['id_currency']] = 0; } if (!isset($min_price[$currency['id_currency']])) { $min_price[$currency['id_currency']] = null; } if ($price > $max_price[$currency['id_currency']]) { $max_price[$currency['id_currency']] = $price; } if ($price == 0) { continue; } if (is_null($min_price[$currency['id_currency']]) || $price < $min_price[$currency['id_currency']]) { $min_price[$currency['id_currency']] = $price; } } } $values = array(); foreach ($currency_list as $currency) { $values[] = '(' . (int) $id_product . ', ' . (int) $currency['id_currency'] . ', ' . $id_shop . ', ' . (int) $min_price[$currency['id_currency']] . ', ' . (int) Tools::ps_round($max_price[$currency['id_currency']] * (100 + $max_tax_rate) / 100, 0) . ')'; } Db::getInstance()->execute(' INSERT INTO `' . _DB_PREFIX_ . 'layered_price_index` (id_product, id_currency, id_shop, price_min, price_max) VALUES ' . implode(',', $values) . ' ON DUPLICATE KEY UPDATE id_product = id_product # avoid duplicate keys'); } }
public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, $use_customer_price = true) { if (!$context) { $context = Context::getContext(); } $cur_cart = $context->cart; if ($divisor !== null) { Tools::displayParameterAsDeprecated('divisor'); } if (!Validate::isBool($usetax) || !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } $id_group = null; if ($id_customer) { $id_group = Customer::getDefaultGroupId((int) $id_customer); } if (!$id_group) { $id_group = (int) Group::getCurrent()->id; } if (!is_object($cur_cart) || Validate::isUnsignedInt($id_cart) && $id_cart && $cur_cart->id != $id_cart) { /* * When a user (e.g., guest, customer, Google...) is on PrestaShop, 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 PrestaShop, he does not have already it BUT knows the cart ID * When called from the back office, cart ID can be inexistant */ if (!$id_cart && !isset($context->employee)) { die(Tools::displayError()); } $cur_cart = new Cart($id_cart); if (!Validate::isLoadedObject($context->cart)) { $context->cart = $cur_cart; } } $qty = $quantity; if (is_array($quantity)) { $quantity = PP::resolveQty($qty[0], $qty[1]); } $cart_quantity = 0; if ((int) $id_cart) { $cache_id = 'Product::getPriceStatic_' . (int) $id_product . '-' . (int) $id_cart; if (!Cache::isStored($cache_id) || ($cart_quantity = Cache::retrieve($cache_id) != (double) $quantity)) { $sql = 'SELECT SUM(' . PP::sqlQty('quantity') . ') FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_product` = ' . (int) $id_product . ' AND `id_cart` = ' . (int) $id_cart; $cart_quantity = (double) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); Cache::store($cache_id, $cart_quantity); } $cart_quantity = Cache::retrieve($cache_id); } $id_currency = (int) Validate::isLoadedObject($context->currency) ? $context->currency->id : Configuration::get('PS_CURRENCY_DEFAULT'); $id_country = (int) $context->country->id; $id_state = 0; $zipcode = 0; if (!$id_address && Validate::isLoadedObject($cur_cart)) { $id_address = $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}; } if ($id_address) { $address_infos = Address::getCountryAndState($id_address); if ($address_infos['id_country']) { $id_country = (int) $address_infos['id_country']; $id_state = (int) $address_infos['id_state']; $zipcode = $address_infos['postcode']; } } elseif (isset($context->customer->geoloc_id_country)) { $id_country = (int) $context->customer->geoloc_id_country; $id_state = (int) $context->customer->id_state; $zipcode = $context->customer->postcode; } if (Tax::excludeTaxeOption()) { $usetax = false; } if ($usetax != false && !empty($address_infos['vat_number']) && $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') && Configuration::get('VATNUMBER_MANAGEMENT')) { $usetax = false; } if (is_null($id_customer) && Validate::isLoadedObject($context->customer)) { $id_customer = $context->customer->id; } return Product::priceCalculation($context->shop->id, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency, $id_group, $qty, $usetax, $decimals, $only_reduc, $usereduc, $with_ecotax, $specific_price_output, $use_group_reduction, $id_customer, $use_customer_price, $id_cart, $cart_quantity); }
/** * Get product price * * @param integer $id_product Product id * @param boolean $usetax With taxes or not (optional) * @param integer $id_product_attribute 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 integer $divisor Useful when paying many time without fees (optional) * @param boolean $only_reduc Returns only the reduction amount * @param boolean $usereduc Set if the returned amount will include reduction * @param integer $quantity Required for quantity discount application (default value: 1) * @param boolean $forceAssociatedTax DEPRECATED - NOT USED Force to apply the associated tax. Only works when the parameter $usetax is true * @param integer $id_customer Customer ID (for customer group reduction) * @param integer $id_cart Cart ID. Required when the cookie is not accessible (e.g., inside a payment module, a cron task...) * @param integer $id_address Customer address ID. Required for price (tax included) calculation regarding the guest localization * @param variable_reference $specificPriceOutput. If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @param boolean $with_ecotax insert ecotax in price output. * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address = NULL, &$specificPriceOutput = NULL, $with_ecotax = true, $use_groupReduction = true) { global $cookie, $cart; $cur_cart = $cart; if (isset($divisor)) { Tools::displayParameterAsDeprecated('divisor'); } if (!Validate::isBool($usetax) or !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } // Initializations if (!$id_customer) { $id_customer = (Validate::isCookie($cookie) and isset($cookie->id_customer) and $cookie->id_customer) ? (int) $cookie->id_customer : NULL; } $id_group = $id_customer ? (int) Customer::getDefaultGroupId($id_customer) : _PS_DEFAULT_CUSTOMER_GROUP_; if (!is_object($cur_cart) or Validate::isUnsignedInt($id_cart) and $id_cart) { /* * When a user (e.g., guest, customer, Google...) is on PrestaShop, 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 PrestaShop, he does not have already it BUT knows the cart ID */ if (!$id_cart and !Validate::isCookie($cookie)) { die(Tools::displayError()); } $cur_cart = $id_cart ? new Cart((int) $id_cart) : new Cart((int) $cookie->id_cart); } $cart_quantity = 0; if ((int) $id_cart) { $condition = ''; $cache_name = (int) $id_cart . '_' . (int) $id_product; if (Configuration::get('PS_QTY_DISCOUNT_ON_COMBINATION')) { $cache_name = (int) $id_cart . '_' . (int) $id_product . '_' . (int) $id_product_attribute; $condition = ' AND `id_product_attribute` = ' . (int) $id_product_attribute; } if (!isset(self::$_cart_quantity[$cache_name]) or self::$_cart_quantity[$cache_name] != (int) $quantity) { self::$_cart_quantity[$cache_name] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT SUM(`quantity`) FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_product` = ' . (int) $id_product . ' AND `id_cart` = ' . (int) $id_cart . ' ' . $condition); $cart_quantity = self::$_cart_quantity[$cache_name]; } } $quantity = ($id_cart and $cart_quantity) ? $cart_quantity : $quantity; $id_currency = (int) (Validate::isLoadedObject($cur_cart) ? $cur_cart->id_currency : ((isset($cookie->id_currency) and (int) $cookie->id_currency) ? $cookie->id_currency : Configuration::get('PS_CURRENCY_DEFAULT'))); // retrieve address informations $id_country = (int) Country::getDefaultCountryId(); $id_state = 0; $id_county = 0; if (!$id_address) { $id_address = $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}; } if ($id_address) { $address_infos = Address::getCountryAndState($id_address); if ($address_infos['id_country']) { $id_country = (int) $address_infos['id_country']; $id_state = (int) $address_infos['id_state']; $postcode = (int) $address_infos['postcode']; $id_county = (int) County::getIdCountyByZipCode($id_state, $postcode); } } elseif (isset($cookie->id_country)) { // fetch address from cookie $id_country = (int) $cookie->id_country; $id_state = (int) $cookie->id_state; $postcode = (int) $cookie->postcode; $id_county = (int) County::getIdCountyByZipCode($id_state, $postcode); } if (Tax::excludeTaxeOption()) { $usetax = false; } if ($usetax != false and !empty($address_infos['vat_number']) and $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') and Configuration::get('VATNUMBER_MANAGEMENT')) { $usetax = false; } $id_shop = (int) Shop::getCurrentShop(); return Product::priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $id_county, $id_currency, $id_group, $quantity, $usetax, $decimals, $only_reduc, $usereduc, $with_ecotax, $specificPriceOutput, $use_groupReduction); }
/** * Get product price * * @param integer $id_product Product id * @param boolean $usetax With taxes or not (optional) * @param integer $id_product_attribute 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 integer $divisor Useful when paying many time without fees (optional) * @param boolean $only_reduc Returns only the reduction amount * @param boolean $usereduc Set if the returned amount will include reduction * @param integer $quantity Required for quantity discount application (default value: 1) * @param boolean $forceAssociatedTax DEPRECATED - NOT USED Force to apply the associated tax. Only works when the parameter $usetax is true * @param integer $id_customer Customer ID (for customer group reduction) * @param integer $id_cart Cart ID. Required when the cookie is not accessible (e.g., inside a payment module, a cron task...) * @param integer $id_address Customer address ID. Required for price (tax included) calculation regarding the guest localization * @param variable_reference $specificPriceOutput. * If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @param boolean $with_ecotax insert ecotax in price output. * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, $use_customer_price = true) { if (!$context) { $context = Context::getContext(); } $cur_cart = $context->cart; if ($divisor !== null) { Tools::displayParameterAsDeprecated('divisor'); } if (!Validate::isBool($usetax) || !Validate::isUnsignedId($id_product)) { die(Tools::displayError()); } // Initializations $id_group = isset($context->customer) ? $context->customer->id_default_group : _PS_DEFAULT_CUSTOMER_GROUP_; // If there is cart in context or if the specified id_cart is different from the context cart id if (!is_object($cur_cart) || Validate::isUnsignedInt($id_cart) && $id_cart && $cur_cart->id != $id_cart) { /* * When a user (e.g., guest, customer, Google...) is on PrestaShop, 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 PrestaShop, he does not have already it BUT knows the cart ID * When called from the back office, cart ID can be inexistant */ if (!$id_cart && !isset($context->employee)) { die(Tools::displayError()); } $cur_cart = new Cart($id_cart); // Store cart in context to avoid multiple instantiations in BO if (!Validate::isLoadedObject($context->cart)) { $context->cart = $cur_cart; } } $cart_quantity = 0; if ((int) $id_cart) { $condition = ''; $cache_name = (int) $id_cart . '_' . (int) $id_product; if (!isset(self::$_cart_quantity[$cache_name]) || self::$_cart_quantity[$cache_name] != (int) $quantity) { self::$_cart_quantity[$cache_name] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT SUM(`quantity`) FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_product` = ' . (int) $id_product . ' AND `id_cart` = ' . (int) $id_cart); } $cart_quantity = self::$_cart_quantity[$cache_name]; } $id_currency = (int) Validate::isLoadedObject($context->currency) ? $context->currency->id : Configuration::get('PS_CURRENCY_DEFAULT'); // retrieve address informations $id_country = (int) $context->country->id; $id_state = 0; $zipcode = 0; if (!$id_address) { $id_address = $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}; } if ($id_address) { $address_infos = Address::getCountryAndState($id_address); if ($address_infos['id_country']) { $id_country = (int) $address_infos['id_country']; $id_state = (int) $address_infos['id_state']; $zipcode = $address_infos['postcode']; } } else { if (isset($context->customer->geoloc_id_country)) { $id_country = (int) $context->customer->geoloc_id_country; $id_state = (int) $context->customer->id_state; $zipcode = (int) $context->customer->postcode; } } if (Tax::excludeTaxeOption()) { $usetax = false; } if ($usetax != false && !empty($address_infos['vat_number']) && $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') && Configuration::get('VATNUMBER_MANAGEMENT')) { $usetax = false; } if (is_null($id_customer) && Validate::isLoadedObject($context->customer)) { $id_customer = $context->customer->id; } return Product::priceCalculation($context->shop->id, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency, $id_group, $cart_quantity, $usetax, $decimals, $only_reduc, $usereduc, $with_ecotax, $specific_price_output, $use_group_reduction, $id_customer, $use_customer_price, $id_cart, $quantity); }
public function getPrice($id_shop = null, $id_product, $id_product_attribute = null, $id_country = null, $id_state = null, $county = null, $id_currency = null, $id_group = null, $quantity = null, $use_tax = null, $decimals = null, $only_reduc = null, $use_reduc = null, $with_ecotax = null, $specific_price_output = null, $divisor = null) { $id_shop = isset($id_shop) ? $id_shop : (int) Shop::getCurrentShop(); // TO CHECK $id_product_attribute = isset($id_product_attribute) ? $id_product_attribute : Product::getDefaultAttribute($id_product); // FIXME $id_country = isset($id_country) ? $id_country : (int) Configuration::get('PS_COUNTRY_DEFAULT'); $id_state = isset($id_state) ? $id_state : 0; $id_currency = isset($id_currency) ? $id_currency : Configuration::get('PS_CURRENCY_DEFAULT'); $id_group = isset($id_group) ? $id_group : Configuration::get('_PS_DEFAULT_CUSTOMER_GROUP_'); $quantity = isset($quantity) ? $quantity : 1; $use_tax = isset($use_tax) ? $use_tax : Configuration::get('PS_TAX'); $decimals = isset($decimals) ? $decimals : 6; $only_reduc = isset($only_reduc) ? $only_reduc : false; $use_reduc = isset($use_reduc) ? $use_reduc : true; $use_ecotax = isset($use_ecotax) ? $use_ecotax : true; $specific_price_output = null; $county = isset($county) ? $county : 0; // UNUSED $divisor = null; $price = Product::priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $county, $id_currency, $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $use_ecotax, $specific_price_output, $divisor); return Tools::ps_round($price, 2); }
/** * Get product price * * @param integer $id_product Product id * @param boolean $usetax With taxes or not (optional) * @param integer $id_product_attribute 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 integer $divisor Useful when paying many time without fees (optional) * @param boolean $only_reduc Returns only the reduction amount * @param boolean $usereduc Set if the returned amount will include reduction * @param integer $quantity Required for quantity discount application (default value: 1) * @param boolean $forceAssociatedTax DEPRECATED - NOT USED Force to apply the associated tax. Only works when the parameter $usetax is true * @param integer $id_customer Customer ID (for customer group reduction) * @param integer $id_cart Cart ID. Required when the cookie is not accessible (e.g., inside a payment module, a cron task...) * @param integer $id_address Customer address ID. Required for price (tax included) calculation regarding the guest localization * @param variable_reference $specificPriceOutput. If a specific price applies regarding the previous parameters, this variable is filled with the corresponding SpecificPrice object * @param boolean $with_ecotax insert ecotax in price output. * @return float Product price */ public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specificPriceOutput = null, $with_ecotax = true, $use_groupReduction = true) { global $cookie, $cart; $cur_cart = $cart; if (isset($divisor)) { Tools::displayParameterAsDeprecated('divisor'); } if ((int) $id_product <= 0) { die(Tools::displayError()); } // Initializations if (!$id_customer) { $id_customer = Validate::isCookie($cookie) && isset($cookie->id_customer) && $cookie->id_customer ? (int) $cookie->id_customer : null; } $id_group = $id_customer ? (int) Customer::getDefaultGroupId($id_customer) : _PS_DEFAULT_CUSTOMER_GROUP_; if (!is_object($cur_cart) && !$id_cart && !Validate::isCookie($cookie)) { die(Tools::displayError()); } /* * When a user (e.g., guest, customer, Google...) is on PrestaShop, 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 PrestaShop, he does not have already it BUT knows the cart ID */ if (!is_object($cur_cart) && ((int) $id_cart > 0 || Validate::isCookie($cookie) && $cookie->id_cart)) { $cur_cart = $id_cart ? new Cart((int) $id_cart) : new Cart((int) $cookie->id_cart); } $cart_quantity = 0; if ((int) $id_cart) { $calc_qty_discounts_with_combinations = (int) Configuration::get('PS_QTY_DISCOUNT_ON_COMBINATION'); $cache_name = (int) $id_cart . '_' . (int) $id_product . ($calc_qty_discounts_with_combinations ? '_' . (int) $id_product_attribute : ''); if (!isset(self::$_cart_quantity) || !count(self::$_cart_quantity) || !isset(self::$_cart_quantity[$cache_name])) { $quantities = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT id_product, ' . ($calc_qty_discounts_with_combinations ? 'id_product_attribute, quantity' : 'SUM(`quantity`) quantity') . ' FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_cart` = ' . (int) $id_cart . ' ' . ($calc_qty_discounts_with_combinations ? ', id_product_attribute' : ' GROUP BY id_product'), false); while ($row = DB::getInstance()->nextRow($quantities)) { self::$_cart_quantity[(int) $id_cart . '_' . (int) $row['id_product'] . ($calc_qty_discounts_with_combinations ? '_' . (int) $row['id_product_attribute'] : '')] = $row['quantity']; } } $cart_quantity = self::$_cart_quantity[$cache_name]; } $quantity = $id_cart && $cart_quantity ? $cart_quantity : $quantity; $id_currency = (int) (Validate::isLoadedObject($cur_cart) ? $cur_cart->id_currency : (isset($cookie->id_currency) && (int) $cookie->id_currency ? $cookie->id_currency : _PS_CURRENCY_DEFAULT_)); // retrieve address informations $id_state = 0; $id_county = 0; if (!$id_address && Validate::isLoadedObject($cur_cart)) { $id_address = $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}; } if ($id_address) { $address_infos = Address::getCountryAndState((int) $id_address); if ($address_infos['id_country']) { $id_country = (int) $address_infos['id_country']; $id_state = (int) $address_infos['id_state']; $postcode = (int) $address_infos['postcode']; $id_county = (int) County::getIdCountyByZipCode((int) $id_state, $postcode); } } elseif (isset($cookie->id_country)) { // fetch address from cookie $id_country = (int) $cookie->id_country; $id_state = (int) $cookie->id_state; $postcode = (int) $cookie->postcode; $id_county = (int) County::getIdCountyByZipCode((int) $id_state, $postcode); } if (!isset($id_country)) { $id_country = (int) Country::getDefaultCountryId(); } if (!_PS_TAX_ || $usetax != false && !empty($address_infos['vat_number']) && $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') && Configuration::get('VATNUMBER_MANAGEMENT')) { $usetax = false; } return Product::priceCalculation(0, $id_product, $id_product_attribute, $id_country, $id_state, $id_county, $id_currency, $id_group, $quantity, $usetax, $decimals, $only_reduc, $usereduc, $with_ecotax, $specificPriceOutput, $use_groupReduction); }
/** * calculates the price for a specific customer group * * @param int $shopId * @param int $itemId * @param int $variantId * @param int $groupId * @param int $qty */ protected function calculateCustomerGroupPrice($shopId, $itemId, $variantId, $groupId, $qty) { /* * class Product method: priceCalculation is available Since 1.4.0.17 */ $specific_price = ''; // This needs to be passed by reference return Product::priceCalculation($shopId, $itemId, $variantId, (int) Country::getDefaultCountryId(), 0, 0, (int) (Validate::isLoadedObject($this->getPlugin()->getContext()->currency) ? $this->getPlugin()->getContext()->currency->id : Configuration::get('PS_CURRENCY_DEFAULT')), $groupId, (int) $qty, $this->getUseTax(), 6, false, true, true, $specific_price, true); }