Skip to content

Commit 58f05e2

Browse files
committed
Finish handling price correctly
1 parent 0c45fa9 commit 58f05e2

File tree

4 files changed

+107
-67
lines changed

4 files changed

+107
-67
lines changed

code/Helper/Config.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,18 @@ public function getSortingIndices($storeId = NULL)
207207
{
208208
$suffix_index_name = 'group_' . $group_id;
209209

210-
$attr['index_name'] = $product_helper->getIndexName($storeId) . '_' . $suffix_index_name . '_' .$attr['attribute'].'_'.$attr['sort'];
210+
$attr['index_name'] = $product_helper->getIndexName($storeId) . '_' . $attr['attribute'] . '_' .$suffix_index_name.'_'.$attr['sort'];
211211
}
212+
else
213+
$attr['index_name'] = $product_helper->getIndexName($storeId). '_' .$attr['attribute'] . '_'.$attr['sort'];
212214
}
213215
else
214-
$attr['index_name'] = $product_helper->getIndexName($storeId) . '_' . 'default' . '_' .$attr['attribute'].'_'.$attr['sort'];
216+
{
217+
if (strpos($attr['attribute'], 'price') !== false)
218+
$attr['index_name'] = $product_helper->getIndexName($storeId). '_' .$attr['attribute'].'_' . 'default' . '_'.$attr['sort'];
219+
else
220+
$attr['index_name'] = $product_helper->getIndexName($storeId). '_' .$attr['attribute'] . '_'.$attr['sort'];
221+
}
215222
}
216223

217224
if (is_array($attrs))

code/Helper/Entity/Producthelper.php

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,25 @@ public function setSettings($storeId)
120120
$facets = $this->config->getFacets();
121121

122122
foreach($facets as $facet)
123+
{
124+
if ($facet['attribute'] === 'price')
125+
{
126+
$facet['attribute'] = 'price.default';
127+
128+
if ($this->config->isCustomerGroupsEnabled($storeId))
129+
{
130+
foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group)
131+
{
132+
$group_id = (int)$group->getData('customer_group_id');
133+
134+
$attributesForFaceting[] = 'price.group_' . $group_id;
135+
}
136+
}
137+
}
138+
123139
$attributesForFaceting[] = $facet['attribute'];
140+
}
141+
124142

125143
foreach ($customRankings as $ranking)
126144
$customRankingsArr[] = $ranking['order'] . '(' . $ranking['attribute'] . ')';
@@ -156,21 +174,24 @@ public function setSettings($storeId)
156174
{
157175
if ($this->config->isCustomerGroupsEnabled($storeId))
158176
{
159-
if (strpos($values['attribute'], 'price') !== false)
177+
if ($values['attribute'] === 'price')
160178
{
161179
foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group)
162180
{
163181
$group_id = (int)$group->getData('customer_group_id');
164182

165183
$suffix_index_name = 'group_' . $group_id;
166184

167-
$slaves[] = $this->getIndexName($storeId) . '_' . $suffix_index_name . '_' .$values['attribute'].'_'.$values['sort'];
185+
$slaves[] = $this->getIndexName($storeId) . '_' .$values['attribute'].'_' . $suffix_index_name . '_' . $values['sort'];
168186
}
169187
}
170188
}
171189
else
172190
{
173-
$slaves[] = $this->getIndexName($storeId) . '_' . 'default' . '_' .$values['attribute'].'_'.$values['sort'];
191+
if ($values['attribute'] === 'price')
192+
$slaves[] = $this->getIndexName($storeId) . '_' .$values['attribute']. '_default_' . $values['sort'];
193+
else
194+
$slaves[] = $this->getIndexName($storeId) . '_' .$values['attribute']. '_' . $values['sort'];
174195
}
175196
}
176197

@@ -186,12 +207,13 @@ public function setSettings($storeId)
186207
{
187208
$group_id = (int)$group->getData('customer_group_id');
188209

189-
$suffix_index_name = '_group_' . $group_id;
210+
$suffix_index_name = 'group_' . $group_id;
211+
212+
$sort_attribute = strpos($values['attribute'], 'price') !== false ? $values['attribute'].'.'.$suffix_index_name : $values['attribute'];
190213

191-
$sort_attribute = strpos($values['attribute'], 'price') !== false ? $values['attribute'].'.'.$group_id : $values['attribute'];
192214
$mergeSettings['ranking'] = array($values['sort'].'('.$sort_attribute.')', 'typo', 'geo', 'words', 'proximity', 'attribute', 'exact', 'custom');
193215

194-
$this->algolia_helper->setSettings($this->getIndexName($storeId).$suffix_index_name.'_'.$values['attribute'].'_'.$values['sort'], $mergeSettings);
216+
$this->algolia_helper->setSettings($this->getIndexName($storeId).'_'.$values['attribute'].'_'. $suffix_index_name .'_'.$values['sort'], $mergeSettings);
195217
}
196218
}
197219
}
@@ -201,56 +223,81 @@ public function setSettings($storeId)
201223

202224
$mergeSettings['ranking'] = array($values['sort'].'('.$sort_attribute.')', 'typo', 'geo', 'words', 'proximity', 'attribute', 'exact', 'custom');
203225

204-
$this->algolia_helper->setSettings($this->getIndexName($storeId) . '_' . 'default' . '_' .$values['attribute'].'_'.$values['sort'], $mergeSettings);
226+
$this->algolia_helper->setSettings($this->getIndexName($storeId) . '_' .$values['attribute'].'_' . 'default' . '_'.$values['sort'], $mergeSettings);
205227
}
206228
}
207229
}
208230
}
209231

210-
/**
211-
* @param $product
212-
* @param $sub_products
213-
* @param $additionalAttributes
214-
* @param $customData
215-
* @param array $groups
216-
* @param null $group_id
217-
*/
218-
private function handlePrice(&$product, $sub_products, $additionalAttributes, &$customData, $groups = array(), $group_id = null)
232+
private function handlePrice(&$product, $sub_products, &$customData, $groups = array(), $current_group_id = null)
219233
{
220-
$key = $group_id === null ? 'default' : $group_id;
221-
222234
$customData['price'] = array();
223235

224-
$customData['price'][$key] = (double) Mage::helper('tax')->getPrice($product, $product->getPrice(), null, null, null, null, $product->getStore(), null);
225-
$customData['price'][$key.'_formated'] = $product->getStore()->formatPrice($customData['price'][$key], false);
236+
$customData['price']['default'] = (double) Mage::helper('tax')->getPrice($product, $product->getPrice(), null, null, null, null, $product->getStore(), null);
237+
$customData['price']['default_formated'] = $product->getStore()->formatPrice($customData['price']['default'], false);
238+
239+
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
240+
{
241+
foreach ($groups as $group)
242+
{
243+
$group_id = (int)$group->getData('customer_group_id');
244+
245+
$customData['price']['group_' . $group_id] = $customData['price']['default'];
246+
$customData['price']['group_' . $group_id . '_formated'] = $customData['price']['default_formated'];
247+
}
248+
}
226249

227250
$special_price = null;
228251

229-
if ($group_id !== null) // If fetch special price for groups
252+
if ($current_group_id !== null) // If fetch special price for groups
230253
{
231254
$discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice(
232255
Mage::app()->getLocale()->storeTimeStamp($product->getStoreId()),
233256
Mage::app()->getStore($product->getStoreId())->getWebsiteId(),
234-
$group_id,
257+
$current_group_id,
235258
$product->getId()
236259
);
237260

238261
if ($discounted_price !== false)
239262
$special_price = $discounted_price;
263+
264+
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
265+
{
266+
foreach ($groups as $group)
267+
{
268+
$group_id = (int)$group->getData('customer_group_id');
269+
270+
$discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice(
271+
Mage::app()->getLocale()->storeTimeStamp($product->getStoreId()),
272+
Mage::app()->getStore($product->getStoreId())->getWebsiteId(),
273+
$group_id,
274+
$product->getId()
275+
);
276+
277+
if ($discounted_price !== false)
278+
{
279+
$customData['price']['group_' . $group_id] = (double) Mage::helper('tax')->getPrice($product, $discounted_price, null, null, null, null, $product->getStore(), null);
280+
$customData['price']['group_' . $group_id . '_formated'] = $product->getStore()->formatPrice($customData['price']['group_' . $group_id], false);
281+
}
282+
}
283+
}
284+
240285
}
241286
else // If fetch default special price
242287
{
243288
$special_price = (double) $product->getFinalPrice();
244289
}
245290

246-
if ($special_price && $special_price !== $customData['price'][$key])
291+
if ($special_price && $special_price !== $customData['price']['default'])
247292
{
248293
$customData['price']['special_from_date'] = strtotime($product->getSpecialFromDate());
249294
$customData['price']['special_to_date'] = strtotime($product->getSpecialToDate());
250295

296+
$customData['price']['default_old_formated'] = $customData['price']['default'.'_formated'];
297+
251298
$special_price = (double) Mage::helper('tax')->getPrice($product, $special_price, null, null, null, null, $product->getStore(), null);
252-
$customData['price'][$key.'_special'] = $special_price;
253-
$customData['price'][$key.'_special_formated'] = $product->getStore()->formatPrice($special_price, false);
299+
$customData['price']['default'] = $special_price;
300+
$customData['price']['default_formated'] = $product->getStore()->formatPrice($special_price, false);
254301
}
255302

256303
if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped' || $product->getTypeId() == 'bundle')
@@ -262,9 +309,7 @@ private function handlePrice(&$product, $sub_products, $additionalAttributes, &$
262309
{
263310
$_priceModel = $product->getPriceModel();
264311

265-
list($min, $max) = $_priceModel->getTotalPrices($product, null, null, false);
266-
list($min_with_tax, $max_with_tax) = $_priceModel->getTotalPrices($product, null, true, false);
267-
312+
list($min, $max) = $_priceModel->getTotalPrices($product, null, null, true);
268313
}
269314

270315
if ($product->getTypeId() == 'grouped' || $product->getTypeId() == 'configurable')
@@ -288,17 +333,15 @@ private function handlePrice(&$product, $sub_products, $additionalAttributes, &$
288333
{
289334
$group_id = (int)$group->getData('customer_group_id');
290335

291-
$customData['price']['group_' . $group_id.'_formated'] = $product->getStore()->formatPrice($min, false) . ' ' . $product->getStore()->formatPrice($max, false);
292-
293-
$customData['price']['default'] = 0; // will be reset just after
336+
$customData['price']['group_' . $group_id] = 0;
337+
$customData['price']['group_' . $group_id . '_formated'] = $product->getStore()->formatPrice($min, false) . ' - ' . $product->getStore()->formatPrice($max, false);
294338
}
295339
}
296340

297341
//// Do not keep special price that is already taken into account in min max
298342
unset($customData['price']['special_from_date']);
299343
unset($customData['price']['special_to_date']);
300-
unset($customData['price']['default_special']);
301-
unset($customData['price']['default_special_formated']);
344+
unset($customData['price']['default_old_formated']);
302345

303346
$customData['price']['default'] = 0; // will be reset just after
304347
}
@@ -312,11 +355,7 @@ private function handlePrice(&$product, $sub_products, $additionalAttributes, &$
312355
foreach ($groups as $group)
313356
{
314357
$group_id = (int)$group->getData('customer_group_id');
315-
316-
if ($customData['price']['group_' . $group_id] == 0)
317-
{
318-
$customData['price']['group_' . $group_id] = $min;
319-
}
358+
$customData['price']['group_' . $group_id] = $min;
320359
}
321360
}
322361
}
@@ -557,7 +596,7 @@ public function getObject(Mage_Catalog_Model_Product $product)
557596
}
558597
}
559598

560-
$this->handlePrice($product, $sub_products, $additionalAttributes, $customData);
599+
$this->handlePrice($product, $sub_products, $customData);
561600

562601
if ($this->config->isCustomerGroupsEnabled())
563602
{
@@ -566,7 +605,7 @@ public function getObject(Mage_Catalog_Model_Product $product)
566605
foreach ($groups as $group)
567606
{
568607
$group_id = (int)$group->getData('customer_group_id');
569-
$this->handlePrice($product, $sub_products, $additionalAttributes, $customData, $groups, $group_id);
608+
$this->handlePrice($product, $sub_products, $customData, $groups, $group_id);
570609
}
571610
}
572611

code/etc/config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@
183183
<instant>
184184
<replace_categories>1</replace_categories>
185185
<instant_selector>.main</instant_selector>
186-
<facets>a:2:{s:18:"_1432907948596_596";a:4:{s:9:"attribute";s:14:"price_with_tax";s:4:"type";s:6:"slider";s:10:"other_type";s:0:"";s:5:"label";s:5:"Price";}s:18:"_1432907963376_376";a:4:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:10:"other_type";s:0:"";s:5:"label";s:10:"Categories";}}</facets>
186+
<facets>a:2:{s:18:"_1432907948596_596";a:4:{s:9:"attribute";s:5:"price";s:4:"type";s:6:"slider";s:10:"other_type";s:0:"";s:5:"label";s:5:"Price";}s:18:"_1432907963376_376";a:4:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:10:"other_type";s:0:"";s:5:"label";s:10:"Categories";}}</facets>
187187
<max_values_per_facet>10</max_values_per_facet>
188-
<sorts>a:3:{s:18:"_1432908018844_844";a:3:{s:9:"attribute";s:14:"price_with_tax";s:4:"sort";s:3:"asc";s:5:"label";s:12:"Lowest price";}s:18:"_1432908022539_539";a:3:{s:9:"attribute";s:14:"price_with_tax";s:4:"sort";s:4:"desc";s:5:"label";s:13:"Highest price";}s:18:"_1433768597454_454";a:3:{s:9:"attribute";s:10:"created_at";s:4:"sort";s:4:"desc";s:5:"label";s:12:"Newest first";}}</sorts>
188+
<sorts>a:3:{s:18:"_1432908018844_844";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:3:"asc";s:5:"label";s:12:"Lowest price";}s:18:"_1432908022539_539";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:4:"desc";s:5:"label";s:13:"Highest price";}s:18:"_1433768597454_454";a:3:{s:9:"attribute";s:10:"created_at";s:4:"sort";s:4:"desc";s:5:"label";s:12:"Newest first";}}</sorts>
189189
<add_to_cart_enable>1</add_to_cart_enable>
190190
</instant>
191191
<autocomplete>

design/frontend/template/topsearch.phtml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $price_key = '.default';
1515

1616
if ($config->isCustomerGroupsEnabled(Mage::app()->getStore()->getStoreId()))
1717
{
18-
$price_key = '.'.$group_id;
18+
$price_key = '.group_'.$group_id;
1919
}
2020

2121
$title = '';
@@ -171,20 +171,17 @@ $class = $isSearchPage ? 'search-page' : '';
171171
{{/thumbnail_url}}
172172

173173
<div class="algoliasearch-autocomplete-price">
174-
{{#price<?php echo $price_key; ?>_special}}
175-
<span class="after_special">
176-
{{price<?php echo $price_key; ?>_special_formated}}
177-
</span>
178-
{{/price<?php echo $price_key; ?>_special}}
179-
180-
{{#price<?php echo $price_key; ?>_special}}<span class="before_special">{{/price<?php echo $price_key; ?>_special}}
181-
182-
{{price<?php echo $price_key; ?>_formated}}
174+
<span class="after_special">
175+
{{price<?php echo $price_key; ?>_formated}}
176+
</span>
183177

184-
{{#price<?php echo $price_key; ?>_special}}</span>{{/price<?php echo $price_key; ?>_special}}
178+
{{#price<?php echo $price_key; ?>_old_formated}}
179+
<span class="before_special">
180+
{{price<?php echo $price_key; ?>_old_formated}}
181+
</span>
182+
{{/price<?php echo $price_key; ?>_old_formated}}
185183
</div>
186184

187-
188185
<div class="info">
189186
{{{_highlightResult.name.value}}}
190187

@@ -376,17 +373,15 @@ $class = $isSearchPage ? 'search-page' : '';
376373
<div class="price">
377374
<div class="algoliasearch-autocomplete-price">
378375
<div>
379-
{{#price<?php echo $price_key; ?>_special}}
380-
<span class="after_special">
381-
{{price<?php echo $price_key; ?>_special_formated}}
382-
</span>
383-
{{/price<?php echo $price_key; ?>_special}}
384-
385-
{{#price<?php echo $price_key; ?>_special}}<span class="before_special">{{/price<?php echo $price_key; ?>_special}}
386-
387-
{{price<?php echo $price_key; ?>_formated}}
376+
<span class="after_special">
377+
{{price<?php echo $price_key; ?>_formated}}
378+
</span>
388379

389-
{{#price<?php echo $price_key; ?>_special}}</span>{{/price<?php echo $price_key; ?>_special}}
380+
{{#price<?php echo $price_key; ?>_old_formated}}
381+
<span class="before_special">
382+
{{price<?php echo $price_key; ?>_old_formated}}
383+
</span>
384+
{{/price<?php echo $price_key; ?>_old_formated}}
390385
</div>
391386
</div>
392387
</div>
@@ -632,7 +627,6 @@ $class = $isSearchPage ? 'search-page' : '';
632627

633628
if (algoliaConfig.facets[i].type == "hierarchical")
634629
{
635-
636630
var hierarchical_levels = [];
637631

638632
for (var l = 0; l < 10; l++)

0 commit comments

Comments
 (0)