Make orders (and products) only save 2 decimals on the database

  • Posts: 634
  • Thank you received: 16
6 years 6 months ago #292371

-- HikaShop version -- : 3.4.0
-- Joomla version -- : 3.8.1

Hi,

For default, the numerical fields on orders, products, shippings and payments come this this database struct: decimal 17,5.

Is there some easy way to make it only save 2 digits?

For example:

If I create an order that have a total incoming of 17.99650 (without taxes), I want to save it in the database like "18.00".



Thanks in advance.

--
Victor

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
6 years 6 months ago #292387

Hi,

There are several ways:
- you could turn on the "round prices during calculations" setting. That way, the rounding would be done during the calculations and not only during the display and you would get a rounded total for the order.
- you could also modify the code in the createFromCart function of the file administrator/components/com_hikashop/classes/order.php to round the total when the order is being created:

$order->order_full_price = $cart->full_total->prices[0]->price_value_with_tax;
- you could also create a plugin of the group hikashop, implementing the onBeforeOrderCreate trigger in order to do the rounding there.

The following user(s) said Thank You: PeterChain

Please Log in or Create an account to join the conversation.

  • Posts: 634
  • Thank you received: 16
6 years 6 months ago #292442

Hi,

Thanks for the answer.

It works fine, but the problem is that I really need to do it in all prices.

My customer only works in 2 decimals and using 5 of them it's a problem.



If I change the desired fields in the database to DECIMAL(17,2) it's going to work?



There will be some issue with that?

Thanks in advance.

--
Victor

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
6 years 6 months ago #292449

Hi,

Ah yes, I didn't thought about that.
That's indeed a very good idea. If you change the type of the field in the database, then MySQL will round the values automatically for you:
dev.mysql.com/doc/refman/8.0/en/precision-math-rounding.html
Note that as explained on that link, it's possible, based on your server, that the rounding is done differently than HikaShop, so for some payment plugins, it's possible that in some cases the rounding is done differently and could potentially be problematic.

The following user(s) said Thank You: PeterChain

Please Log in or Create an account to join the conversation.

  • Posts: 634
  • Thank you received: 16
6 years 6 months ago #292740

Hi,

First of all, sorry for the delay.

Which rounding system uses Hikashop?

Thanks in advance.

--
Victor

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
6 years 6 months ago #292751

Hi,

HikaShop rounds up from 5 to 9 and down from 0 to 4 (the default rounding of PHP) and what is usually done in real life.

The following user(s) said Thank You: PeterChain

Please Log in or Create an account to join the conversation.

Time to create page: 0.063 seconds
Powered by Kunena Forum