Rounding off price on product listing page magento 2

Hi,
We have integrated algolia with magento 2
I am new to algolia. I want to round off price on product listing page.
Is there any reference in order to achieve this?

Thank you

Hi @sachin ,

Could you tell me what you’re trying to achieve exactly ?
You want to change prices from XX.XX€ to XX€ ignoring the decimals even if it’s not 00 ?
Do you have any screenshot to show me what it is display on your product page and on your native product listings ?

Thanks in advance.

Best regards.

Damien

Hi @damien.couchez

Thank you for your reply.

Yes you are right.
For exam, if price of product is AED 1,469.00 then I need to show round off as AED 1,469
if price of product is AED 1,101.75 then I need to show round off as AED 1,102

I am adding screen shot of both examples here
algolia plp price

I think the best way to do it is to use one of our Magento backend events “algolia_after_create_product_object” :

You will be able to get the $custom_data variable with all the data ready to be indexed in the product indices.
What is usually displayed on the product listing is the “default_formatted” value of the currency price object (in your case, it should be price.AED.default_formatted).

So you have to change this value, you can use the price.AED.default value which should be a float and format it as you want and replace the value in price.AED.default_formatted with it.

Hope this would help.

Best regards.

Damien

@damien.couchez

Thank you for reply and thank you for suggestion.

I will follow this and get back to you with results. Thank you again.

@damien.couchez

I have a question, how to replace the value in price.AED.default_formatted.

For exam : I have USD currency for store and I am using below code
$custom_data = $observer->getData(‘custom_data’); //I get data here which is being sent for indexing
$price = $custom_data[‘price’][‘USD’][‘default_formated’]; //I get formatted price of USD store
$priceForRoundOff = preg_replace("/[^0-9.]/", “”, $price); //getting only integer value here
$roundedPrice = round($priceForRoundOff); //rounding off Integer value
$finalRoundedPrice = number_format($roundedPrice); //formating it
custom_data['price']['USD']['default_formated'] = ''.$finalRoundedPrice; //Here I am trying to replace value of price.USD.default_formatted with $finalRoundedPrice. Is this right

custom_data['price']['USD']['default_formated'] = ''.$finalRoundedPrice; After this, I have run re-indexing and lost all products from front end. My question is that "Is this correct way to set//replace value

Hi @sachin ,

Seems like a good start.
I think it would be “cleaner” to get the “default” value at the beginning rather than “default_formatted”, and use a native magento helper to format the value before replacing $custom_data[‘price’][‘USD’][‘default_formated’] after rounding the price (it should be a way to format it without the decimals) .

To test the observer, you don’t need to directly index all your products, just choose one and save it in the Magento Back-office, it will trigger a reindex only for this particular product.

Hope this would help.

Best.

@damien.couchez
I got default price value and got it formated using magento in a variable called $formattedPrice

I am stuck at replacing the value of $custom_data[‘price’][‘USD’][‘default_formated’] with $formattedPrice.

Can you help out how to replace this value.

Did you try just to set the value and update the observer ?

Something like

$custom_data[‘price’][‘USD’][‘default_formated’] = $formattedPrice;
$observer->getData(‘custom_data’)->setData($custom_data);

I did only this $custom_data[‘price’][‘USD’][‘default_formated’] = $formattedPrice;
but didn’t set into observer

Hi @damien.couchez,
I tried using this
$custom_data[‘price’][‘USD’][‘default_formated’] = $formattedPrice;
$observer->getData(‘custom_data’)->setData($custom_data);

But it is not setting new value and also there are no indices created on algolia dashboard

Is there anything else I can try with? Any other suggestions?