diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Config.php b/app/code/community/Algolia/Algoliasearch/Helper/Config.php index 6e10c6c4..4051945d 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Config.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Config.php @@ -503,6 +503,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'; diff --git a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php index 02b0c260..fd5fd2fe 100644 --- a/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php +++ b/app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php @@ -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') @@ -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) { @@ -512,102 +521,104 @@ 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; + } else { + $canSpecialBeValid = false; } - - /** @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(); + $currencyData = array(); - $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); + $currencyData['default'] = $price; + $currencyData['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 + $currencyData['special_from_date'] = Mage::app()->getLocale()->storeDate($store, $special_from_date)->getTimestamp(); + //if special_to_date is null leave this blank or it will end the special_price! + if ($special_to_date) { + $currencyData['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) { + $currencyData[$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']; + $currencyData[$group_str] = $currencyData['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 < $currencyData[$group_str]) { + $currencyData[$group_str .'_original_formated'] = $this->formatPrice( + $currencyData[$group_str], false, $currency_code ); + $currencyData[$group_str .'_original'] = $currencyData[$group_str]; + + $currencyData[$group_str] = $special_price; } + $currencyData[$group_str .'_formated'] = $store->formatPrice($currencyData[$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 < $currencyData['default']) { + $currencyData['default_original_formated'] = $currencyData['default_formated']; + $currencyData['default_original'] = $currencyData['default']; - $customData[$field][$currency_code]['default'] = $special_price; - $customData[$field][$currency_code]['default_formated'] = $this->formatPrice( + $currencyData['default'] = $special_price; + $currencyData['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; @@ -645,37 +656,39 @@ 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($currencyData['default_original_formated']) === false || $min <= $currencyData['default']) { + $currencyData['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($currencyData['special_from_date']); + unset($currencyData['special_to_date']); + unset($currencyData['default_original']); + unset($currencyData['default_original_formated']); - $customData[$field][$currency_code]['default'] = 0; // will be reset just after + $currencyData['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 <= $currencyData[$group_str]) { + $currencyData[$group_str] = 0; } else { - $customData[$field][$currency_code]['group_'.$group_id] = $customData[$field][$currency_code]['default']; + $currencyData[$group_str] = $currencyData['default']; } - $customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $dashed_format; + $currencyData[$group_str .'_formated'] = $dashed_format; } } } - if ($customData[$field][$currency_code]['default'] == 0) { - $customData[$field][$currency_code]['default'] = $min; + if ($currencyData['default'] == 0) { + $currencyData['default'] = $min; if ($min === $max) { - $customData[$field][$currency_code]['default_formated'] = $this->formatPrice($min, false, + $currencyData['default_formated'] = $this->formatPrice($min, false, $currency_code); } } @@ -683,17 +696,19 @@ protected function handlePrice(Mage_Catalog_Model_Product &$product, $sub_produc 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 ($currencyData[$group_str] == 0) { + $currencyData[$group_str] = $min; if ($min === $max) { - $customData[$field][$currency_code]['group_'.$group_id.'_formated'] = $customData[$field][$currency_code]['default_formated']; + $currencyData[$group_str .'_formated'] = $currencyData['default_formated']; } } } } } + $customData[$field][$currency_code] = $currencyData; } } } diff --git a/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml b/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml index 9f8dbc14..653a3fb6 100644 --- a/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml +++ b/app/design/frontend/base/default/template/algoliasearch/internals/configuration.phtml @@ -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; } diff --git a/js/algoliasearch/internals/frontend/common.js b/js/algoliasearch/internals/frontend/common.js index 7cc5acf0..cd47d9c7 100644 --- a/js/algoliasearch/internals/frontend/common.js +++ b/js/algoliasearch/internals/frontend/common.js @@ -39,9 +39,8 @@ var algolia = { if (Array.isArray(currentData)) { currentData = [currentData]; } - var allParameters = [].concat(currentData).concat(hookArguments); - + return hook.apply(null, allParameters); }, originalData); @@ -143,17 +142,35 @@ 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'; + + // special_from_date should not be blank Producthelper.php sets it to today if blank + var priceStart = hit['price'][algoliaConfig.currencyCode]['special_from_date']; + // special_to_date may be blank, maybe the special price doesn't end + // if it is blank, just set it to the future so the comparison works var priceExpiration = hit['price'][algoliaConfig.currencyCode]['special_to_date']; + if (!priceExpiration) priceExpiration = algoliaConfig.now+1; - 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; + + } } } @@ -539,4 +556,4 @@ document.addEventListener("DOMContentLoaded", function (e) { window.scrollTo(x, y); }; }); -}); \ No newline at end of file +});