Calcbuilder extended: Price/qty calculator integrated in Hikashop product/use of CBPlugin
We are going to configure Calcbuilder extended to work inside a Hikashop product page. The input form for this case will be the one built with Hikashop custom fields, the calculator will use these fields to perform calculations and return to the product page the calculated price, the calculated quantity, or both of them. The rest of the checkout will continue unchanged.
The first step is to install and enable the CB Price Calculator plugin that you can find inside your Calcbuilder installer package. Just unzip and launch the hikashopcalcbuilder.zip plugin installer. Please note you need to have installed previously at your site both Hikashop and Calcbuilder extensions.
The plugin installer will create some custom fields automatically at your Hikashop configuration, you'll find a custom field called 'calculator' for the products, and a couple of additional fields for the items 'calculatedprice' and 'calculatedpricecontrol'
You must check all of them are enabled for the front end form. Depending on your Hikashop version, you may also need to enter the fields and enable them specifically for the categories/products where you want to apply the calculator
The plugin itself doesn't need further configuration, you can access the plugin settings to choose if you will want to add a button to launch the calculator (and the text), or if it will launch automatically with each field change otherwise
Using Hikashop custom fields you will build the input form. For this tutorial we'll build a simple example with 2 custom fields, surfacewidth and surfaceheight. The calculator will be in charge of calculating the quantity of the product needed for those settings, and also will return a custom price. At Hikashop custom fields configuration we create our 2 custom fields for the table 'item':
Remember to enable your custom fields for the front end and apply them for the categories/products if required.
Now you can start configuring your calculator. We're going to create a new calculator, and introduce the math at the 'code' section. For this calculator you won't need to create any field at 'Fields' section, or a 'form layout', as we're using the fields and form already created by Hikashop at the product page.
You only need to remember the result for the price must be returned in a variable called 'hk_calculatedprice' , and the quantity at 'hk_calculatedqty', for our example, let's say we need to calculate the surface area according to use input, and our product covers 5m2 per unit, so for quantity calculation we'll use:
Please note we have available the customfields 'surfaceheight' and 'surfacewidth' and that we've rounded up to the next integer using function 'ceil'
For the price, we'll introduce a new formula taking into account the quantity already calculated:
These formulas are introduce at the code section, but remember you also have the option to upload an excel file instead, mapping inputs and outputs same way you will do for a default calculator built with Calcbuilder extended.
Please note you must always return the 'unit' price of your product, hikashop will multiply the quantity selected for this price when going to the checkout/cart, as it does for any other product.
At the calculators list, get the calculator ID you will see at the first column
And place it at the Hikashop product/s where you want to use this calculator:
Now you can see the calculator working at the product front-end. Each time one of the custom fields changes, the calculator will update the quantity and the calculated price field.
If you only need to calculate price, just avoid the $hk_calculatedqty result at your code section, for ex:
With this code only the calculated price will be modified with each calculation. For returning only a calculated quantity, keeping the original price of your product, just don't include the $hk_calculatedprice result at your code.
Formatting output price
You must use a numeric output for the output price, but you may also want to format the price (including currency, decimal separator...) for your users at the front end form. For this purpouse you can add another result at 'hk_calculatedpriceformat' variable, where you can include your formatted output:
//we use number format function to round to 2 decimal places, include thousand and decimal separator, and the euro currency at the end $hk_calculatedpriceformat=number_format($hk_calculatedprice,2,',','.')."€";
This would be the result at the front-end:
If you don't include any formatted result, the default hk_calculatedprice will be shown instead.Last modified on