Skip to content
2 changes: 2 additions & 0 deletions app/code/community/Algolia/Algoliasearch/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ public function getAttributesToRetrieve($groupId, $store)
foreach ($currencies as $currency) {
$attributes[] = $price.'.'.$currency.'.default';
$attributes[] = $price.'.'.$currency.'.default_formated';
$attributes[] = $price.'.'.$currency.'.default_original';
$attributes[] = $price.'.'.$currency.'.default_original_formated';
$attributes[] = $price.'.'.$currency.'.group_'.$groupId;
$attributes[] = $price.'.'.$currency.'.group_'.$groupId.'_formated';
$attributes[] = $price.'.'.$currency.'.group_'.$groupId.'_original_formated';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function getProductCollectionQuery($storeId, $productIds = null, $only_vi
}

$products = $products
->addAttributeToSelect('special_price')
->addAttributeToSelect('special_from_date')
->addAttributeToSelect('special_to_date')
->addAttributeToSelect('visibility')
Expand Down Expand Up @@ -496,14 +497,22 @@ protected function formatPrice($price, $includeContainer, $currency_code)

protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_products, &$customData)
{
/** @var Mage_Directory_Model_Currency $directoryCurrency */
$directoryCurrency = Mage::getModel('directory/currency');

/** @var Mage_Tax_Helper_Data $taxHelper */
$taxHelper = Mage::helper('tax');

/** @var Mage_Directory_Helper_Data $directoryHelper */
$directoryHelper = Mage::helper('directory');

$fields = $this->getFields($product->getStore());
$customer_groups_enabled = $this->config->isCustomerGroupsEnabled($product->getStoreId());
$store = $product->getStore();
$type = $this->config->getMappedProductType($product->getTypeId());

/** @var Mage_Directory_Model_Currency $directoryCurrency */
$directoryCurrency = Mage::getModel('directory/currency');
$currencies = $directoryCurrency->getConfigAllowCurrencies();
$baseCurrencyCode = $store->getBaseCurrencyCode();

if (Mage::helper('core')->isModuleEnabled('Mage_Weee') &&
Mage::helper('weee')->getPriceDisplayType($product->getStore()) == 0) {
Expand All @@ -512,102 +521,100 @@ protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_produc
$weeeTaxAmount = 0;
}

$baseCurrencyCode = $store->getBaseCurrencyCode();

$groups = array();

if ($customer_groups_enabled) {
$groups = Mage::getModel('customer/group')->getCollection();
} else {
$groups = array();
}
$special_price = $product->getSpecialPrice();
$special_from_date = $product->getSpecialFromDate();
$special_to_date = $product->getSpecialToDate();

// There is a special_price and either no to date or a to date in the future
// start date is irrelevant
if ($special_price && Mage::app()->getLocale()->isStoreDateInInterval($store, null, $special_to_date)) {
$canSpecialBeValid = true;
$special_price += $weeeTaxAmount;
}

/** @var Mage_Tax_Helper_Data $taxHelper */
$taxHelper = Mage::helper('tax');

/** @var Mage_Directory_Helper_Data $directoryHelper */
$directoryHelper = Mage::helper('directory');

foreach ($fields as $field => $with_tax) {
$customData[$field] = array();
$field_price = (double) $taxHelper->getPrice($product, $product->getPrice() + $weeeTaxAmount, $with_tax, null, null, null, $store, null);
if ($canSpecialBeValid) {
$field_special_price = (double) $taxHelper->getPrice($product, $special_price, $with_tax, null, null, null, $store, null);
}

foreach ($currencies as $currency_code) {
$customData[$field][$currency_code] = array();
$cdfc = array();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it really hard to read $customData[$field][$currency_code] everywhere, so I renamed this and added it at the end, I apologise if this breaks your code style

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this is painful to use in the rest of the function. On the other hand $cdfc isn't very meaningful and maybe too short. Could you please consider to change the variable name in something meaningful like $currencyData maybe ? Thx in advance :)


$price = (double) $taxHelper->getPrice($product, $product->getPrice(), $with_tax, null, null, null, $product->getStore(), null);
$price = $directoryHelper->currencyConvert($price, $baseCurrencyCode, $currency_code);
$price += $weeeTaxAmount;
$price = $directoryHelper->currencyConvert($field_price + $weeeTaxAmount, $baseCurrencyCode, $currency_code);

$customData[$field][$currency_code]['default'] = $price;
$customData[$field][$currency_code]['default_formated'] = $this->formatPrice($price, false, $currency_code);
$cdfc['default'] = $price;
$cdfc['default_formated'] = $this->formatPrice($price, false, $currency_code);

$special_price = (double) $taxHelper->getPrice($product, $product->getFinalPrice(), $with_tax, null, null, null, $product->getStore(), null);
$special_price = $directoryHelper->currencyConvert($special_price, $baseCurrencyCode, $currency_code);
$special_price += $weeeTaxAmount;
if ($canSpecialBeValid) {
$special_price = $directoryHelper->currencyConvert($field_special_price + $weeeTaxAmount, $baseCurrencyCode, $currency_code);

// this needs moving up a few levels
$cdfc['special_from_date'] = Mage::app()->getLocale()->storeDate($store, $special_from_date)->getTimestamp();
//if special_to_date is null this will default to today
$cdfc['special_to_date'] = Mage::app()->getLocale()->storeDate($store, $special_to_date)->getTimestamp();
}
if ($customer_groups_enabled) {
// If fetch special price for groups

foreach ($groups as $group) {
$group_id = (int) $group->getData('customer_group_id');
$group_str = 'group_' . $group_id;
$product->setCustomerGroupId($group_id);

$discounted_price = $product->getPriceModel()->getFinalPrice(1, $product);
$discounted_price = $directoryHelper->currencyConvert($discounted_price, $baseCurrencyCode, $currency_code);
$discounted_price += $weeeTaxAmount;

if ($discounted_price !== false) {
$customData[$field][$currency_code]['group_'.$group_id] = (double) $taxHelper->getPrice($product,
$discounted_price,
$with_tax, null,
null, null,
$product->getStore(),
null);
$customData[$field][$currency_code]['group_'.$group_id] = $directoryHelper->currencyConvert($customData[$field][$currency_code]['group_'.$group_id],
$baseCurrencyCode,
$currency_code);
$customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $store->formatPrice($customData[$field][$currency_code]['group_'.$group_id],
false, $currency_code);
$group_price = $product->getGroupPrice();
$group_price += $weeeTaxAmount;

if ($group_price !== false) {
$cdfc[$group_str] = $directoryHelper->currencyConvert(
(double) $taxHelper->getPrice($product,
$group_price,
$with_tax, null,
null, null,
$store,
null),
$baseCurrencyCode,
$currency_code);
} else {
$customData[$field][$currency_code]['group_'.$group_id] = $customData[$field][$currency_code]['default'];
$customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $customData[$field][$currency_code]['default_formated'];
$cdfc[$group_str] = $cdfc['default'];
}
}

$product->setCustomerGroupId(null);
}

$customData[$field][$currency_code]['special_from_date'] = strtotime($product->getSpecialFromDate());
$customData[$field][$currency_code]['special_to_date'] = strtotime($product->getSpecialToDate());

if ($customer_groups_enabled) {
foreach ($groups as $group) {
$group_id = (int) $group->getData('customer_group_id');

if ($special_price && $special_price < $customData[$field][$currency_code]['group_'.$group_id]) {
$customData[$field][$currency_code]['group_'.$group_id.'_original_formated'] =
$customData[$field][$currency_code]['default_formated'];

$customData[$field][$currency_code]['group_'.$group_id] = $special_price;
$customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $this->formatPrice(
$special_price,
if ($canSpecialBeValid && $special_price < $cdfc[$group_str]) {
$cdfc[$group_str .'_original_formated'] = $this->formatPrice(
$cdfc[$group_str],
false,
$currency_code
);
$cdfc[$group_str .'_original'] = $cdfc[$group_str];

$cdfc[$group_str] = $special_price;
}
$cdfc[$group_str .'_formated'] = $store->formatPrice($cdfc[$group_str], false, $currency_code);
}
$product->setCustomerGroupId(null);
}

if ($special_price && $special_price < $customData[$field][$currency_code]['default']) {
$customData[$field][$currency_code]['default_original_formated'] =
$customData[$field][$currency_code]['default_formated'];
if ($canSpecialBeValid && $special_price < $cdfc['default']) {
$cdfc['default_original_formated'] = $cdfc['default_formated'];
$cdfc['default_original'] = $cdfc['default'];

$customData[$field][$currency_code]['default'] = $special_price;
$customData[$field][$currency_code]['default_formated'] = $this->formatPrice(
$cdfc['default'] = $special_price;
$cdfc['default_formated'] = $this->formatPrice(
$special_price,
false,
$currency_code
);
}

// configurable appears to assume that there are no customer_group_prices
// specified on the attached simple products, but needs re-writing anyway
// to correctly calculate the option price, not the simple price
if ($type == 'grouped' || $type == 'bundle' || $type == 'configurable') {
$min = PHP_INT_MAX;
$max = 0;
Expand Down Expand Up @@ -645,55 +652,59 @@ protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_produc
$dashed_format = $this->formatPrice($min, false, $currency_code).' - '.$this->formatPrice($max,
false, $currency_code);

if (isset($customData[$field][$currency_code]['default_original_formated']) === false || $min <= $customData[$field][$currency_code]['default']) {
$customData[$field][$currency_code]['default_formated'] = $dashed_format;
if (isset($cdfc['default_original_formated']) === false || $min <= $cdfc['default']) {
$cdfc['default_formated'] = $dashed_format;

//// Do not keep special price that is already taken into account in min max
unset($customData['price']['special_from_date']);
unset($customData['price']['special_to_date']);
unset($customData['price']['default_original_formated']);
unset($cdfc['special_from_date']);
unset($cdfc['special_to_date']);
unset($cdfc['default_original']);
unset($cdfc['default_original_formated']);

$customData[$field][$currency_code]['default'] = 0; // will be reset just after
$cdfc['default'] = 0; // will be reset just after
}

if ($customer_groups_enabled) {
foreach ($groups as $group) {
$group_id = (int) $group->getData('customer_group_id');
$group_str = 'group_' . $group_id;

if ($min != $max && $min <= $customData[$field][$currency_code]['group_'.$group_id]) {
$customData[$field][$currency_code]['group_'.$group_id] = 0;
if ($min != $max && $min <= $cdfc[$group_str]) {
$cdfc[$group_str] = 0;
} else {
$customData[$field][$currency_code]['group_'.$group_id] = $customData[$field][$currency_code]['default'];
$cdfc[$group_str] = $cdfc['default'];
}

$customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $dashed_format;
$cdfc[$group_str .'_formated'] = $dashed_format;
}
}
}

if ($customData[$field][$currency_code]['default'] == 0) {
$customData[$field][$currency_code]['default'] = $min;
if ($cdfc['default'] == 0) {
$cdfc['default'] = $min;

if ($min === $max) {
$customData[$field][$currency_code]['default_formated'] = $this->formatPrice($min, false,
$cdfc['default_formated'] = $this->formatPrice($min, false,
$currency_code);
}
}

if ($customer_groups_enabled) {
foreach ($groups as $group) {
$group_id = (int) $group->getData('customer_group_id');
$group_str = 'group_' . $group_id;

if ($customData[$field][$currency_code]['group_'.$group_id] == 0) {
$customData[$field][$currency_code]['group_'.$group_id] = $min;
if ($cdfc[$group_str] == 0) {
$cdfc[$group_str] = $min;

if ($min === $max) {
$customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $customData[$field][$currency_code]['default_formated'];
$cdfc[$group_str .'_formated'] = $cdfc['default_formated'];
}
}
}
}
}
$customData[$field][$currency_code] = $cdfc;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $session = Mage::getSingleton('customer/session');
$customerGroupId = $session->getCustomerGroupId();

$priceKey = '.'.$currencyCode.'.default';
if ($config->isCustomerGroupsEnabled($storeId) && $customerGroupId !== 0) {
if ($config->isCustomerGroupsEnabled($storeId)) {
$priceKey = '.'.$currencyCode.'.group_'.$customerGroupId;
}

Expand Down
37 changes: 26 additions & 11 deletions js/algoliasearch/internals/frontend/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ var algolia = {
if (Array.isArray(currentData)) {
currentData = [currentData];
}

var allParameters = [].concat(currentData).concat(hookArguments);

return hook.apply(null, allParameters);
}, originalData);

Expand Down Expand Up @@ -143,17 +142,33 @@ document.addEventListener("DOMContentLoaded", function (e) {
hit.price = hit.price[0];
}

if (hit['price'] !== undefined && price_key !== '.' + algoliaConfig.currencyCode + '.default' && hit['price'][algoliaConfig.currencyCode][price_key.substr(1) + '_formated'] !== hit['price'][algoliaConfig.currencyCode]['default_formated']) {
hit['price'][algoliaConfig.currencyCode][price_key.substr(1) + '_original_formated'] = hit['price'][algoliaConfig.currencyCode]['default_formated'];
if (Array.isArray(hit.price_with_tax)) {
hit.price_with_tax = hit.price_with_tax[0];
}

if (hit['price'] !== undefined && hit['price'][algoliaConfig.currencyCode]['default_original_formated']
&& hit['price'][algoliaConfig.currencyCode]['special_to_date']) {

// price_key is in format .CURRENCY.KEY, fix that for array access
const price_key_as_key = price_key.replace('.' + algoliaConfig.currencyCode + '.', '');
const formated_original_key = price_key_as_key + '_original_formated';

// original_formatted means that there is possibly a special price
if (hit['price'] !== undefined && hit['price'][algoliaConfig.currencyCode][formated_original_key]) {
const formated_key = price_key_as_key + '_formated';

// Neither date should be blank, magento sets special_from_date
// Producthelper.php sets to_date to today if it's blank
var priceStart = hit['price'][algoliaConfig.currencyCode]['special_from_date'];
var priceExpiration = hit['price'][algoliaConfig.currencyCode]['special_to_date'];

if (algoliaConfig.now > priceExpiration) {
hit['price'][algoliaConfig.currencyCode]['default_formated'] = hit['price'][algoliaConfig.currencyCode]['default_original_formated'];
hit['price'][algoliaConfig.currencyCode]['default_original_formated'] = false;
// if .now is not inside the window between priceStart and priceExpiration
if (!(algoliaConfig.now >= priceStart && algoliaConfig.now <= priceExpiration )) {
// remove original_formatted_price, so client doesn't show special price
hit['price'][algoliaConfig.currencyCode][formated_key] = hit['price'][algoliaConfig.currencyCode][formated_original_key];
hit['price'][algoliaConfig.currencyCode][formated_original_key] = false;
if (hit.price_with_tax !== undefined) {
hit['price_with_tax'][algoliaConfig.currencyCode][formated_key] = hit['price_with_tax'][algoliaConfig.currencyCode][formated_original_key];
hit['price_with_tax'][algoliaConfig.currencyCode][formated_original_key] = false;

}
}
}

Expand Down Expand Up @@ -517,4 +532,4 @@ document.addEventListener("DOMContentLoaded", function (e) {
window.scrollTo(x, y);
};
});
});
});