Skip to content

Commit 0c45fa9

Browse files
committed
Fix price issues
1 parent 50283f1 commit 0c45fa9

File tree

3 files changed

+132
-164
lines changed

3 files changed

+132
-164
lines changed

code/Helper/Entity/Producthelper.php

Lines changed: 109 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getAllAttributes($add_empty_row = false)
2222

2323
$allAttributes = $config->getEntityAttributeCodes('catalog_product');
2424

25-
$productAttributes = array_merge(array('name', 'path', 'categories', 'categories_without_path', 'description', 'ordered_qty', 'stock_qty', 'price_with_tax', 'rating_summary', 'media_gallery'), $allAttributes);
25+
$productAttributes = array_merge(array('name', 'path', 'categories', 'categories_without_path', 'description', 'ordered_qty', 'stock_qty', 'price', 'rating_summary', 'media_gallery'), $allAttributes);
2626

2727
$excludedAttributes = array(
2828
'all_children', 'available_sort_by', 'children', 'children_count', 'custom_apply_to_products',
@@ -53,7 +53,7 @@ public function getAllAttributes($add_empty_row = false)
5353
protected function isAttributeEnabled($additionalAttributes, $attr_name)
5454
{
5555
foreach ($additionalAttributes as $attr)
56-
if ($attr['attribute'] == $attr_name)
56+
if ($attr['attribute'] === $attr_name)
5757
return true;
5858

5959
return false;
@@ -207,12 +207,22 @@ public function setSettings($storeId)
207207
}
208208
}
209209

210-
private function handlePrice(&$product, &$customData, $group_id = null)
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)
211219
{
212220
$key = $group_id === null ? 'default' : $group_id;
213221

214-
$customData['price'][$key] = (double) $product->getPrice();
215-
$customData['price_with_tax'][$key] = (double) Mage::helper('tax')->getPrice($product, $product->getPrice(), true, null, null, null, null, false);
222+
$customData['price'] = array();
223+
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);
216226

217227
$special_price = null;
218228

@@ -222,36 +232,95 @@ private function handlePrice(&$product, &$customData, $group_id = null)
222232
Mage::app()->getLocale()->storeTimeStamp($product->getStoreId()),
223233
Mage::app()->getStore($product->getStoreId())->getWebsiteId(),
224234
$group_id,
225-
$product->getId());
235+
$product->getId()
236+
);
226237

227238
if ($discounted_price !== false)
228239
$special_price = $discounted_price;
229240
}
230241
else // If fetch default special price
242+
{
231243
$special_price = (double) $product->getFinalPrice();
244+
}
232245

233246
if ($special_price && $special_price !== $customData['price'][$key])
234247
{
235-
$customData['special_price_from_date'][$key] = strtotime($product->getSpecialFromDate());
236-
$customData['special_price_to_date'][$key] = strtotime($product->getSpecialToDate());
248+
$customData['price']['special_from_date'] = strtotime($product->getSpecialFromDate());
249+
$customData['price']['special_to_date'] = strtotime($product->getSpecialToDate());
237250

238-
$customData['special_price'][$key] = (double) $special_price;
239-
$customData['special_price_with_tax'][$key] = (double) Mage::helper('tax')->getPrice($product, $special_price, true, null, null, null, null, false);
240-
241-
$customData['special_price_formated'][$key] = $product->getStore()->formatPrice($customData['special_price'][$key], false);
242-
$customData['special_price_with_tax_formated'][$key] = $product->getStore()->formatPrice($customData['special_price_with_tax'][$key], false);
251+
$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);
243254
}
244-
else
255+
256+
if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped' || $product->getTypeId() == 'bundle')
245257
{
246-
/**
247-
* In case of partial updates set back to empty so that it get updated
248-
*/
249-
$customData['special_price'][$key] = '';
250-
$customData['special_price_with_tax'][$key] = '';
251-
}
258+
$min = PHP_INT_MAX;
259+
$max = 0;
260+
261+
if ($product->getTypeId() == 'bundle')
262+
{
263+
$_priceModel = $product->getPriceModel();
264+
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+
268+
}
269+
270+
if ($product->getTypeId() == 'grouped' || $product->getTypeId() == 'configurable')
271+
{
272+
foreach ($sub_products as $sub_product)
273+
{
274+
$price = (double) Mage::helper('tax')->getPrice($sub_product, $sub_product->getFinalPrice(), null, null, null, null, $product->getStore(), null);
275+
276+
$min = min($min, $price);
277+
$max = max($max, $price);
278+
}
279+
}
280+
281+
if ($min != $max)
282+
{
283+
$customData['price']['default_formated'] = $product->getStore()->formatPrice($min, false) . ' - ' . $product->getStore()->formatPrice($max, false);
284+
285+
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
286+
{
287+
foreach ($groups as $group)
288+
{
289+
$group_id = (int)$group->getData('customer_group_id');
290+
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
294+
}
295+
}
296+
297+
//// Do not keep special price that is already taken into account in min max
298+
unset($customData['price']['special_from_date']);
299+
unset($customData['price']['special_to_date']);
300+
unset($customData['price']['default_special']);
301+
unset($customData['price']['default_special_formated']);
302+
303+
$customData['price']['default'] = 0; // will be reset just after
304+
}
305+
306+
if ($customData['price']['default'] == 0)
307+
{
308+
$customData['price']['default'] = $min;
309+
310+
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
311+
{
312+
foreach ($groups as $group)
313+
{
314+
$group_id = (int)$group->getData('customer_group_id');
252315

253-
$customData['price_formated'][$key] = $product->getStore()->formatPrice($customData['price'][$key], false);
254-
$customData['price_with_tax_formated'][$key] = $product->getStore()->formatPrice($customData['price_with_tax'][$key], false);
316+
if ($customData['price']['group_' . $group_id] == 0)
317+
{
318+
$customData['price']['group_' . $group_id] = $min;
319+
}
320+
}
321+
}
322+
}
323+
}
255324
}
256325

257326
public function getObject(Mage_Catalog_Model_Product $product)
@@ -278,25 +347,6 @@ public function getObject(Mage_Catalog_Model_Product $product)
278347
if ($this->isAttributeEnabled($additionalAttributes, 'description'))
279348
$customData['description'] = $product->getDescription();
280349

281-
282-
foreach (array('price', 'price_with_tax', 'special_price_from_date', 'special_price_to_date', 'special_price'
283-
,'special_price_with_tax', 'special_price_formated', 'special_price_with_tax_formated'
284-
,'price_formated', 'price_with_tax_formated') as $price)
285-
$customData[$price] = array();
286-
287-
$this->handlePrice($product, $customData);
288-
289-
if ($this->config->isCustomerGroupsEnabled())
290-
{
291-
$groups = Mage::getModel('customer/group')->getCollection();
292-
293-
foreach ($groups as $group)
294-
{
295-
$group_id = (int)$group->getData('customer_group_id');
296-
$this->handlePrice($product, $customData, $group_id);
297-
}
298-
}
299-
300350
$categories = array();
301351
$categories_with_path = array();
302352

@@ -401,100 +451,25 @@ public function getObject(Mage_Catalog_Model_Product $product)
401451

402452
if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped' || $product->getTypeId() == 'bundle')
403453
{
404-
$min = PHP_INT_MAX;
405-
$max = 0;
406-
407-
$min_with_tax = PHP_INT_MAX;
408-
$max_with_tax = 0;
409-
410454
if ($product->getTypeId() == 'bundle')
411455
{
412-
$_priceModel = $product->getPriceModel();
413-
414-
list($min, $max) = $_priceModel->getTotalPrices($product, null, null, false);
415-
list($min_with_tax, $max_with_tax) = $_priceModel->getTotalPrices($product, null, true, false);
416-
417456
$ids = array();
418457

419458
$selection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product);
420459

421-
foreach($selection as $option)
460+
foreach ($selection as $option)
422461
$ids[] = $option->product_id;
423462
}
424463

425464
if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped')
426465
$ids = $product->getTypeInstance(true)->getChildrenIds($product->getId());
427466

428-
if (count($ids)) {
429-
$sub_products = $this->getProductCollectionQuery($product->getStoreId(), $ids, false, false)->load();
430-
}
431-
else {
432-
$sub_products = array();
433-
}
434-
435-
if ($product->getTypeId() == 'grouped' || $product->getTypeId() == 'configurable')
467+
if (count($ids))
436468
{
437-
foreach ($sub_products as $sub_product)
438-
{
439-
$price = $sub_product->getPrice();
440-
$price_with_tax = Mage::helper('tax')->getPrice($sub_product, $price, true, null, null, null, null, false);
441-
442-
$min = min($min, $price);
443-
$max = max($max, $price);
444-
445-
$min_with_tax = min($min_with_tax, $price_with_tax);
446-
$max_with_tax = max($max_with_tax, $price_with_tax);
447-
}
448-
}
449-
450-
if ($min != $max)
451-
{
452-
$customData['min_formated'] = array();
453-
$customData['max_formated'] = array();
454-
$customData['min_with_tax_formated'] = array();
455-
$customData['max_with_tax_formated'] = array();
456-
457-
$customData['min_formated']['default'] = $product->getStore()->formatPrice($min, false);
458-
$customData['max_formated']['default'] = $product->getStore()->formatPrice($max, false);
459-
$customData['min_with_tax_formated']['default'] = $product->getStore()->formatPrice($min_with_tax, false);
460-
$customData['max_with_tax_formated']['default'] = $product->getStore()->formatPrice($max_with_tax, false);
461-
462-
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
463-
{
464-
foreach ($groups as $group)
465-
{
466-
$group_id = (int)$group->getData('customer_group_id');
467-
468-
$customData['min_formated']['group_' . $group_id] = $product->getStore()->formatPrice($min, false);
469-
$customData['max_formated']['group_' . $group_id] = $product->getStore()->formatPrice($max, false);
470-
$customData['min_with_tax_formated']['group_' . $group_id] = $product->getStore()->formatPrice($min_with_tax, false);
471-
$customData['max_with_tax_formated']['group_' . $group_id] = $product->getStore()->formatPrice($max_with_tax, false);
472-
}
473-
}
474-
}
475-
476-
if ($customData['price']['default'] == 0)
469+
$sub_products = $this->getProductCollectionQuery($product->getStoreId(), $ids, false, false)->load();
470+
} else
477471
{
478-
$customData['price']['default'] = $min;
479-
$customData['price_with_tax']['default'] = $min_with_tax;
480-
$customData['price_formated']['default'] = $product->getStore()->formatPrice($min, false);
481-
$customData['price_with_tax_formated']['default'] = $product->getStore()->formatPrice($min_with_tax, false);
482-
483-
if ($this->config->isCustomerGroupsEnabled($product->getStoreId()))
484-
{
485-
foreach ($groups as $group)
486-
{
487-
$group_id = (int)$group->getData('customer_group_id');
488-
489-
if ($customData['price']['group_' . $group_id] == 0)
490-
{
491-
$customData['price']['group_' . $group_id] = $min;
492-
$customData['price_with_tax']['group_' . $group_id] = $min_with_tax;
493-
$customData['price_formated']['group_' . $group_id] = $product->getStore()->formatPrice($min, false);
494-
$customData['price_with_tax_formated']['group_' . $group_id] = $product->getStore()->formatPrice($min_with_tax, false);
495-
}
496-
}
497-
}
472+
$sub_products = array();
498473
}
499474
}
500475

@@ -582,6 +557,19 @@ public function getObject(Mage_Catalog_Model_Product $product)
582557
}
583558
}
584559

560+
$this->handlePrice($product, $sub_products, $additionalAttributes, $customData);
561+
562+
if ($this->config->isCustomerGroupsEnabled())
563+
{
564+
$groups = Mage::getModel('customer/group')->getCollection();
565+
566+
foreach ($groups as $group)
567+
{
568+
$group_id = (int)$group->getData('customer_group_id');
569+
$this->handlePrice($product, $sub_products, $additionalAttributes, $customData, $groups, $group_id);
570+
}
571+
}
572+
585573
$transport = new Varien_Object($customData);
586574
Mage::dispatchEvent('algolia_subproducts_index', array('custom_data' => $transport, 'sub_products' => $sub_products));
587575
$customData = $transport->getData();

code/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
<number_product_suggestions>3</number_product_suggestions>
177177
<number_product_results>9</number_product_results>
178178
<number_product_results_backend>9</number_product_results_backend>
179-
<product_additional_attributes>a:13:{s:18:"_1427959997377_377";a:4:{s:9:"attribute";s:4:"name";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427960012597_597";a:4:{s:9:"attribute";s:4:"path";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427960251221_221";a:4:{s:9:"attribute";s:17:"short_description";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:9:"unordered";}s:18:"_1427961262735_735";a:4:{s:9:"attribute";s:10:"categories";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961263735_735";a:4:{s:9:"attribute";s:10:"meta_title";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961264377_377";a:4:{s:9:"attribute";s:12:"meta_keyword";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961289908_908";a:4:{s:9:"attribute";s:16:"meta_description";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:9:"unordered";}s:18:"_1427961324936_936";a:4:{s:9:"attribute";s:3:"sku";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427962021621_621";a:4:{s:9:"attribute";s:14:"price_with_tax";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427977839554_554";a:4:{s:9:"attribute";s:11:"ordered_qty";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}s:18:"_1428566173508_508";a:4:{s:9:"attribute";s:9:"stock_qty";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}s:17:"_1433929490023_23";a:4:{s:9:"attribute";s:14:"rating_summary";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1436178594492_492";a:4:{s:9:"attribute";s:10:"created_at";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}}</product_additional_attributes>
179+
<product_additional_attributes>a:13:{s:18:"_1427959997377_377";a:4:{s:9:"attribute";s:4:"name";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427960012597_597";a:4:{s:9:"attribute";s:4:"path";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427960251221_221";a:4:{s:9:"attribute";s:17:"short_description";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:9:"unordered";}s:18:"_1427961262735_735";a:4:{s:9:"attribute";s:10:"categories";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961263735_735";a:4:{s:9:"attribute";s:10:"meta_title";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961264377_377";a:4:{s:9:"attribute";s:12:"meta_keyword";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427961289908_908";a:4:{s:9:"attribute";s:16:"meta_description";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:9:"unordered";}s:18:"_1427961324936_936";a:4:{s:9:"attribute";s:3:"sku";s:10:"searchable";s:1:"1";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427962021621_621";a:4:{s:9:"attribute";s:5:"price";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1427977839554_554";a:4:{s:9:"attribute";s:11:"ordered_qty";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}s:18:"_1428566173508_508";a:4:{s:9:"attribute";s:9:"stock_qty";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}s:17:"_1433929490023_23";a:4:{s:9:"attribute";s:14:"rating_summary";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"1";s:5:"order";s:7:"ordered";}s:18:"_1436178594492_492";a:4:{s:9:"attribute";s:10:"created_at";s:10:"searchable";s:1:"0";s:11:"retrievable";s:1:"0";s:5:"order";s:7:"ordered";}}</product_additional_attributes>
180180
<custom_ranking_product_attributes>a:1:{s:18:"_1427960305274_274";a:2:{s:9:"attribute";s:11:"ordered_qty";s:5:"order";s:4:"desc";}}</custom_ranking_product_attributes>
181181
<results_limit>1000</results_limit>
182182
</products>

0 commit comments

Comments
 (0)