Skip to main content
CalcBuilder logo

CalcBuilder Tutorial

How to create a calculator with a dynamic price for Hikashop?

Selling something whose price depends on what the customer types in — a banner by size, a service by hours, a made-to-measure item? With Calc Builder you can calculate that price with your own formula and hand it straight to a Hikashop cart as a real product, in one click. This tutorial builds a custom banner calculator that does exactly that.

How it works

Calc Builder ships a ready-made snippet called Link to Hika Shop code. You paste it at the end of the calculator’s PHP code and fill in the product details. When a visitor presses Calculate:

  1. your PHP code computes the price from the form fields;
  2. Calc Builder creates a Hikashop product with that name, description and price;
  3. the result shows an Add to Cart button that sends the visitor to the Hikashop cart with that product and quantity.

Hikashop must be installed and published on the same Joomla site. The calculator itself needs nothing else: no extra plugin and no manual product to create in advance.

1. Create the calculator and its form fields

In Components → Calc Builder click New and give the calculator a name, for example Custom banner. Then open Form fields and add three Number fields:

VariableLabelSuggested settings
widthWidth (cm)Required, minimum 20, maximum 500
heightHeight (cm)Required, minimum 20, maximum 300
qtyUnitsRequired, minimum 1, default 1

See Calc Builder 6: form fields and the Number field type for every option.

2. Calculate the price in the PHP code

Open the PHP code tab. The variables of your form fields are available by name. Compute the price first:

// area in square metres
$area = ($width / 100) * ($height / 100);

// 18.50 per square metre, 12.00 minimum per unit
$unit_price = max(12, round($area * 18.5, 2));

3. Paste the “Link to Hika Shop” snippet

On the same PHP code screen, the Code Tips panel has an entry called Link to Hika Shop code. Copy its code to the end of your PHP code (after the calculations) and set the values. You can use any variable you calculated above:

// Modify these parameters according to your product
$hk_quantity = $qty;
$hk_product_name = 'Custom banner';
$hk_product_description = 'Custom banner, ' . $width . ' x ' . $height . ' cm';
$hk_pricehikaproduct = $unit_price;
$hk_product_code = 'BANNER';
VariableWhat it does
$hk_quantityUnits added to the cart. If it is empty or 0, no product is created and no button is shown.
$hk_product_nameProduct name in Hikashop.
$hk_product_descriptionLong description of the product.
$hk_pricehikaproductThe dynamic price — the number your formula produced.
$hk_product_codeProduct code. Calc Builder appends a random suffix so every generated product has a unique code.

Build the name and description from numbers and your own fixed text, as above. Do not paste free-text fields typed by the visitor (such as a Text or Textarea field) into them: apostrophes and other special characters are not escaped when the product is created.

4. Optional: more product options

The snippet also accepts extra data. All of it is optional:

  • $hk_productfields['product_width'] = 23; — fills any column of the Hikashop product table (#__hikashop_product), such as width, length or weight.
  • $hk_productfields['id_category'] = 5; — the Hikashop category where the product is placed. The default is category 2.
  • $hk_productfields['product_image'] = 'banner.jpg'; — an image file already in Hikashop’s upload folder.
  • $hk_file_name and $hk_file_description — attach the calculation result, as a PDF, to the product, so the customer can download it after buying. Leave $hk_file_name empty to attach nothing.

5. Design the result

Open Exit Layout and show the customer what they are about to buy. Use the placeholders of the variables you calculated:

<h4>Your custom banner</h4>
<p>Size: ##width## x ##height## cm — ##qty## unit(s)</p>
<p>Price per unit: <strong>##unit_price##</strong></p>

You do not have to add the cart button yourself: Calc Builder adds the Add to Cart button below the result when a Hikashop product has been generated. See the result (Exit Layout).

6. Publish and test

Publish the calculator as a menu item or a module (publishing a calculator). Then on the front end:

  1. Enter a width, height and number of units and press Calculate.
  2. Check the price shown in the result.
  3. Press Add to Cart. You land in the Hikashop checkout with the product, the description and exactly the price your formula returned.

Each calculation creates a new, one-off product in Hikashop, so the catalogue will grow with every calculation that is run. If that bothers you, put them in a dedicated category with id_category and clean it up from time to time in the Hikashop back end.

Common questions

  • Can the price come from an Excel file instead of PHP? Yes. Map the output cell to a variable in the spreadsheet engine and assign it: $hk_pricehikaproduct = $total;.
  • Can the customer pick the quantity? Yes — add a Number field and assign it to $hk_quantity, as in this tutorial.
  • Nothing happens or no button appears. Check that Hikashop is installed, that $hk_quantity is not empty or 0, and that the snippet is at the end of the PHP code.
...
CalcBuilder

Create dynamic calculators

Buy now!
...
List Manager

Build different lists for your site

Buy now!