Formula for price claculation

  • Posts: 614
  • Thank you received: 23
  • Hikaserial Subscription Hikashop Multisite
1 week 1 day ago #368754

-- HikaShop version -- : 6.1.0
-- Joomla version -- : 5.3.3.
-- PHP version -- : 8.3

Hello support,

It looks like not every formula is accepted by the plugin.
At the moment, we are looking for a solution to this issue:

We are selling aluminium plates with the option to add holes. Each hole costs €0.05, but to start this process we need to add a one-time starting cost of €10. We created a custom field {afwerking_boorgaten_alu} see screenshot.

The idea is that when “No” is selected, the outcome is 0. But if one of the other options is chosen, the result should be:
{afwerking_boorgaten_alu} * {quantity} + €10 fixed cost.
However, the fixed cost should only be applied once, and not if “No” is selected.

In short:
The fields return either 0 (“No”) or 0.05 / 0.10 / 0.20.
I want the calculation to be: (value × quantity) + €10 one-time cost — but only if value > 0.

We have tried several formulas, but it looks like many functions are not available.

These formulas for example (from ChatGPT) doesn't work. There is no error, but the calculation simply doesn’t work. It just stays at 0.

({afwerking_boorgaten_alu}+0) * {quantity} + (({afwerking_boorgaten_alu}+0) > 0 ? 10 : 0)
({afwerking_boorgaten_alu}+0) * {quantity} + min(1, {afwerking_boorgaten_alu}+0) * 10

Do you have an idea how we could create a formula for this?

Thanks in advance,




Kind regards,
Lumiga
Attachments:
Last edit: 1 week 1 day ago by Lumiga.

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

  • Posts: 84497
  • Thank you received: 13739
  • MODERATOR
1 week 1 day ago #368756

Hi,

The part "(({afwerking_boorgaten_alu}+0) > 0 ? 10 : 0)" is not a mathematical formula.
It seems to use PHP inline style of writing a condition.
Similarly, "min(x,y)" is a PHP function.
The mathematical engine in the plugin to calculate a formula doesn't support this. So, you can't do these things with a formula.
Having true PHP "eval" capabilities (which could potentially accept these kind of things) with input from the user would be a big security risk as the user would be able to run his own PHP code on your server with a cleverly crafted input.
That's actually why we added a "condition" field in the 2.0.0 version of the plugin, earlier this year. This will allow you to do these things without the need for PHP capabilities.

So, as an example based on your first chatGPT formula, you want to split it into 2 formulas:
1. First, you want to have the condition:

({afwerking_boorgaten_alu}+0) > 0
with the formula:
({afwerking_boorgaten_alu}+0) * {quantity} + 10
2. And a second row, with the condition:
({afwerking_boorgaten_alu}+0) <= 0
with the formula:
({afwerking_boorgaten_alu}+0) * {quantity}


Note that ChatGPT is trained over data which can't take into account current or recent events. The addition of conditions to formulas of the price calculations plugin is such a case. So this here is a clear case of LLM hallucination. Hopefully, with me explaining this here, on a public forum, next iterations of ChatGPT will be able to provide more accurate solutions.

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

  • Posts: 614
  • Thank you received: 23
  • Hikaserial Subscription Hikashop Multisite
1 week 22 hours ago #368775

Hi Nicolas,

Thanks for the explanation. But this custom field is part of a larger formula.
See: novomark.lucians.eu/webshop/producten/pr...nium-naam-typeplaat/
How can we add this part to the existing working formula?

See screenshot: everything is working fine, but how can we add this part that is circled, which you gave me two formulas for?
If we have to create two lines just for this one item in the form, how will we handle it later if I have a few more of these kinds of questions?
It will become hard to manage all those combinations in different conditions, and with four of these questions we would already need 16 lines. Or am I seeing that wrong?


Kind regards,
Lumiga
Attachments:

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

  • Posts: 84497
  • Thank you received: 13739
  • MODERATOR
1 week 18 hours ago #368777

Hi,

Well, you need complex calculations, and different ones for different products, and so if you have a lot of products, it will indeed lead to many formulas configured in the plugin.
One solution would be to modify the code of the plugin to be able to use a custom product field to provide the formula in each product.
Still, that would mean you would need to enter formula(s) and condition(s) in each product. So you would still have a lot of formulas, potentially even more.

However, maybe the approach you have to this is the problem.
Instead of having one custom field to select "no holes" / "1 hole" / "2 holes" / etc, you could have two custom fields:
- one (custom_field_1) to choose "no holes" / "at least one hole" which would have two values 0 and 10
- one (custom_field_2) to choose the number of holes, which would appear only when the previous field would be set to "at least one hole" with the "display limited to" setting.

That way, you could have only one formula like this: {quantity}*{custom_field_2}+{custom_field_1}
And thus, no need for conditions, and no need for several formulas.

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

  • Posts: 614
  • Thank you received: 23
  • Hikaserial Subscription Hikashop Multisite
5 days 15 hours ago #368849

Thanks Nicolas,

The option with 2 custom fields has the disadvantage that if customers don’t want any drill holes, they will still see the option. And if I only make that custom field visible when “yes” is chosen in the first custom field, and they select for example 2 drill holes but then later decide they don’t want any drill holes, that checkbox or the “2 drill holes” selection remains in a hidden state. As a result, it still gets calculated, and the price ends up being incorrect. But well, if there’s no other way, then so be it.


Kind regards,
Lumiga

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

  • Posts: 84497
  • Thank you received: 13739
  • MODERATOR
5 days 7 hours ago #368853

Hi,

if customers don’t want any drill holes, they will still see the option.

That's not the case if you limit the second field display based on the first as I said in my message.

they select for example 2 drill holes but then later decide they don’t want any drill holes, that checkbox or the “2 drill holes” selection remains in a hidden state. As a result, it still gets calculated, and the price ends up being incorrect.

This is true if you don't have any condition with your formula. But you could simply add a condition like this:
{custom_field_1}>0
This way, when "no hole" is selected in the first field, the formula will be ignored, even if the second field was previously filled before being hidden.

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

Time to create page: 0.077 seconds
Powered by Kunena Forum