Condition the display and calculation of option prices to the custom fields

  • Posts: 145
  • Thank you received: 6
5 days 11 hours ago #372677

Hi
I want to write a plugin for Hikashop that displays and calculates the price of optional products based on the Product Custom Field that the customer selects. Also, in each option, based on the variant that the customer chooses (by comparing the Custom Field of each option's variants with the custom fields of the other product's variants), other variants of the option products are displayed.

Let me explain the scenario I'm looking for:
There is a curtain store website.
The customer chooses the product variant (color or model) he wants, by doing this, the product price changes.
Then he enters the length and width of the product in the custom fields created for this purpose.
Using the price calculation plugin (length * width * price), the product cost is calculated.
There is also a Product Custom Field where the customer chooses whether he wants electric or manual curtains. If the customer chooses manual, there is no need for Options, but if he chooses electric, the Option products (motor and fittings) must be displayed.
These 2 options (motor and fittings) have variants that have their own prices. Also, these variants have Product Custom Fields that They contain the maximum weight, length, and width they can withstand. For example, if the length of the curtain entered by the user is more than 3 meters, only the heavy motor, which is one of the motor variants and has the ability to move the curtain more than 3 meters, should be displayed in the motor option, but if the curtain length is less than 3 meters, both the light motor and the heavy motor options should be displayed so that the customer can choose whichever he likes.
Regarding the fittings, the appropriate fittings should be displayed depending on the length and width of the curtain. These options are finally added to the calculated price in the price calculation plugin.

Is it possible to implement such a plugin in Hikashop and can you advise me on what events I should use?


Life is the unique stage of our art. Everyone sings their own song and leaves the stage. The stage is always permanent. Blessed is that song that people remember.

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

  • Posts: 85945
  • Thank you received: 14133
  • MODERATOR
5 days 56 minutes ago #372681

Hi,

Yes it is possible, and most of it does not need a custom plugin at all: the whole pricing side can be done with custom item fields plus our "Price Calculations" plugin (on the marketplace). Only the conditional display and filtering of the options needs a bit of custom JavaScript. Here is the breakdown.

Pricing (no code, configuration only)
The Price Calculations plugin lets you define several formulas, each with an optional condition, and it recalculates the price live on the product page. In a formula you can reference any product or item custom field by its column name, plus {price}, {cart_product_quantity} and others, and you have math operators and a few PHP functions. Two important points for your case:
- {price} already reflects the selected variant AND the option costs, so options are added to the total automatically.
- the price preview refreshes by AJAX as the customer changes the fields.

So:
- Length x width: create two custom item fields (table "item") named for example length and width, then a formula like:

{length} * {width} * <your rate> + {price}
(or {price} * {length} * {width}, depending on how your base price is defined).
- Electric vs manual: the motor and fittings are options, so their price already folds into {price}. If the math has to differ between the two cases, add a second formula with a condition on your electric/manual field, for example condition {type} == 'electric' with its own formula, and a formula with no condition as the fallback. The plugin uses the first formula whose condition matches.

The only part that needs custom JavaScript
The plugin calculates the price, it does not decide which options or which option variants are shown or selectable. Two of your requirements are display logic, not pricing:
1. Showing the motor and fittings options only when "electric" is chosen.
2. Showing only the motor/fittings variants whose maximum length/width/weight fit the dimensions the customer entered (heavy motor only above 3 m, appropriate fittings by size, etc.).

These react to values typed live on the page, so they have to be done in JavaScript. Store the max length/width/weight of each motor and fittings variant as product custom fields on those variants, read them server side, inject them as a JS map, and on every length/width change show or hide the matching options. For point 1 the electric/manual show-hide can be a couple of lines of JS. You can also prune an option's variant list server side with the onBeforeOptionDisplay event (fired in front/views/product/tmpl/option.php), but since the dimensions are entered live the real filtering has to happen in JS.

Note that a formula cannot read the option products' own variant custom fields (they are not exposed as tags on the main product), which is another reason the dimension filtering is done in JS rather than in a formula.

So: custom item fields + the Price Calculations plugin for all the pricing, and a small JavaScript override for the conditional display and the dimension based filtering of the options.

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

Time to create page: 0.056 seconds
Powered by Kunena Forum