- Posts: 51
- Thank you received: 3
Option Price Calculation
- jfischer_hika
-
Topic Author
- Offline
-- Joomla version -- : 3.10.11
-- PHP version -- : 8.0.20
-- Browser(s) name and version -- : firefox
Hello,
I would like to add an option to certain products with a calculated price.
The option would have three different price calculations based on the selected option variant.
Since options inherit the quantity of the main product, I would need to account for that in the option price.
Therefore, I would need to calculate the price of each option variant based on the quantity of the main product.
For example:
Option named "Concierge Service"
Option Variant "No"
- - no price calculation
Option Variant "Silver"
- - Price is $45 for all quantities.
- - Price calculation: (45/qty)
- - Result: $45 added to total price
Option Variant "Gold"
- - Price is $45 + $1.23 each
- - Price calculation is ((45/qty) + 1.23)
- - Result is $45 + $1.23 per Quantity is added to total price
So if the customer bought a quantity of 25 of the main product at $10 each and chose the "Concierge Service" option ...
- - "No" total price = $250 which is the standard price * qty: (10*25)
- - "Silver" total price = $295 (250 + 45) which is the (price + option calculated price) * qty: (10 + (45/25)) * 25
- - "Gold" total price = $325.75 (250 + 45 + 30.75) which is the (price + option calculated price) * qty: (10 + ((45/25) + 1.23)) * 25
I did purchase the Price Calculations plugin but not sure how to apply it to this scenario.
Is there a way you suggest to accomplish this?
Thank You
Please Log in or Create an account to join the conversation.
You can use the options mechanism only:
www.hikashop.com/support/documentation/i...ct-form.html#options
demo.hikashop.com/index.php/en/hikashop/...ct-page/with-options
- You create first a "concierge service" characteristic with three values: no, silver, gold
- You create a product "concierge service" and add that characteristic as characteristic of the product and you then add the variants, one for each value.
- In each variant, you can enter the price(s). For "no", you don't add any price, for "silver", you can add a price of 45 with a minimum quantity of 1, 22.5 (45/2) for a minimum quantity of 2, 15 (45/3) for a minimum quantity of 3, etc, and for "gold" 46.23 (45 + 1.23) for a minimum quantity of 1, 24.96 (45/2+1.23*2) for a minimum quantity of 2, etc.
The price calculation plugin is not necessary, but it requires entering a lot of prices to handle all the quantities possible. And you can use the quantity per order setting to limit the purchase up to where you enter the prices so that people can't buy the options for more quantity than you setup.
Without using an option, but instead a custom item field and the price calculation plugin I don't see a way to do that as there is no if / else logic in the plugin. It can only do calculations, it cannot do differrent things based on the selection of the user.
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
I could use the options method you suggested ,however, that would require numerous prices since the main product is sold in mass quantities.
A few questions:
1. Is it possible to create a custom field to use as a flag to trigger an if/else statement somewhere in a view file?
It would have to be triggered on selection of the option variant.
The custom field value would be set in the selected option variant.
2. Could I use JS or modify the price calculations plugin to accomplish this?
Please Log in or Create an account to join the conversation.
You can use a view override to add a bit of javascript to trigger when a particular dropdown (an option for example) is changed.
And there, you can then automatically fill the value of a custom item field (which you can hide with a bit of CSS). Then, you can use the price calculation plugin to just read the value from the custom item field to display the calculated price.
Doing it purely with the price calculations plugin would require it to handle condition tags. Not impossible but not that easy to implement properly. To do it properly would require integrating with something like twig ( twig.symfony.com/ ) or mustache ( github.com/bobthecow/mustache.php ). It's a big piece just for a calculation plugin.
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
I tried the options method in your first response but found a few issues with that method;
1. The amount displayed next to the option does not change with the qty field. It only shows the first price level entered into the option variant. This would be misleading to customers.
2. The price field truncates the amount to 5 decimals which causes a rounding error in the final price with options.
For the JS method:
How would I target the select field on change and pass the qty value to the function and then assign to the custom field?
Could you provide a code example?
Please Log in or Create an account to join the conversation.
You can do it like that:
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
That is close to what I tried but couldn't get to trigger.
My code:
Nothing shows in the console log.
There has to be something I am doing wrong.
Please Log in or Create an account to join the conversation.
Well, the ids will differ based on what page you're looking at.
For example, on the page illuminated-manuscript/save-the-date-postcard of your website, there are two options with the ids hikashop_product_option_0 and hikashop_product_option_1, the quantity input field has the id hikashop_product_quantity_field_1 and the custom item field has the id fsw_quantity.
Note that the id hikashop_product_custom_item_value_36 is for the span around the input field.
Also, on the last line, if you keep the quotes around qty or quantity, it will display the text qty or quantity in the input field. If you want to display the value in the qty or quantity variable you need to remove the quotes.
Finally, in your code you use querySelector. In that case you need to use '#hikashop_product_option_7' and not just 'hikashop_product_option_7'
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
I did have the '#hikashop_product_option_7' in my original code but it still did not work.
I finally got it to trigger and update the custom field using the following code:
I see what you mean by the ids. They are dynamically created for each product page. To avoid this I created another custom drop down field so the id will be consistent on all product pages making it easier to target.
How do I get the price with options?
I tried:
This does not work. It returns undefined.
How would I use the calculations plugin to add the new price to the product?
Am I calculating the price in JS and sending that to my custom field ?
What is the syntax used in the price calculations plugin?
There is no instructions on its usage.
Please Log in or Create an account to join the conversation.
The price with options displayed on the page is stored in a hidden input field with the id
The instructions for the formula are directly written in the settings page:
The formula can contain tags which correspond to custom fields of the table 'product' or 'item' (like {length} for the length of a tissue the user wants to purchase, or {custom_text} for some text the user want to be printed on a T-shirt). These custom fields can be created in the menu Display / Custom fields of HikaShop. The tag {price} will be replaced by the price entered in the product/variant.<br/>The formula can also contain operations (like + or *), and some authorized PHP functions ( like strlen() or abs() ).<br/>Note that the result must be a number since it will be used as the price of the product.<br/>Finally, note that you can enter several formulas (for example, if you have different custom fields for different types of products and thus you need different formulas). However, only the first matching formula will be used for a given product price.
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
I managed to get the code to update the custom field based on the selected value of my drop down.
I was able to get the price with this code as well:
Since my calculated price is dependent on the quantity field It makes it more difficult.
- I noticed the qty field changes when options or variants are selected.
changes toCode:hikashop_product_quantity_field_1This "_2" increments on every option selected which causes it to be a moving target.Code:hikashop_product_quantity_field_2
- The price calculation does not trigger if the quantity field is changed by the customer. This would need to happen at any stage the customer changes the quantity field whether on the product page, in the cart, or at checkout.
I am starting to think this is not a viable solution.
Please Log in or Create an account to join the conversation.
1. To avoid the issue, you can change:
document.getElementById('hikashop_product_quantity_field_1')
by:
document.querySelector('#hikashop_product_quantity_main input')
That way, you always get the currently displayed input in the main quantity area of the page.
2. On the product page, you can use the quantity.checked javascript event to trigger your price calculation:
www.hikashop.com/support/documentation/6...ocumentation.html#js
For the cart / checkout, the simplest would be to make the quantity ready only (there is a setting for that in the cart view of the checkout workflow and in the cart module settings).
Otherwise, it would require developing a hikashop plugin implementing the www.hikashop.com/support/documentation/6...tml#onBeforeCartSave event to update the value in the custom item field with the price before the quantity is being updated.
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
Ok, I got it working to update the field and change with quantity.
A couple of changes to your code was needed.
1. I needed to change:
2. I needed to add the missing closing ")" to the event trigger example.
www.hikashop.com/support/documentation/6...ocumentation.html#js
from:
I still can't get the price calculation plugin to work.
I tried the formula {custom_field_id}+{price} but this does not get the custom field values.
It only returns the price of the main product. and changing the options or variants does not always update the calculated price.
Please Log in or Create an account to join the conversation.
1. Well, I said to replace
document.getElementById('hikashop_product_quantity_field_1')
by
document.querySelector('#hikashop_product_quantity_main input')
The full line was:
const qty = document.getElementById('hikashop_product_quantity_field_1').value;
so you should have ended up with:
const qty = document.querySelector('#hikashop_product_quantity_main input').value;
I supposed you removed the .value when doing this and thus you had to add it back to make it work.
2. This was on me. Thanks for the feedback. I've updated the documentation to fix the code sample.
3. After changing the value in the input of the custom item field, if you want the "calculated price" to be refreshed by the calculations plugin, you need to add that line:
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
1. You did say that, and I did copy/paste over the entire line of code deleting the ".value". Sorry for the confusion on my part.
I greatly appreciate your help and your patience.
3. I added the line to update the calculation plugin but I still have an issue.
When I select/change the custom field or update the quantity the calculations are working fine now.
However, when I chose a product with multiple variants and/or options the calculations do not refresh if I select a price altering option or variant after I selected the custom field. When I change multiple options either the calculated price doesn't change or it stops calculating all together reverting back to the "price with options" price.
The calculations only work or update if I select all the options/variants before I select/change the custom field.
Please Log in or Create an account to join the conversation.
3. I've made a new version of the calculations plugin to better handle the options' price. Please download again the install package of the plugin on our website and install it on yours and it should help.
Please Log in or Create an account to join the conversation.
- jfischer_hika
-
Topic Author
- Offline
- Posts: 51
- Thank you received: 3
That corrected the issue. Everything is working as expected.
Thank You
Please Log in or Create an account to join the conversation.