JavaScript pricing formula - how to configure and manage it?

This module allows you to add a selected product in the card mini JavaScript program, which will be responsible for calculating its final price based on the configuration of attributes selected by the buyer in the store.



Attention!
In order to use the module JavaScript pricing formula, you must have a properly prepared template of the product card in the mask of your shop. To do so, contact the IdoSell graphic department, which will prepare and perform the appropriate modification.



How to correctly set the product card in the IdoSell panel in order to be able to assign the appropriate formula for calculating the price of the product to it?

In order to assign a ready-made formula for calculating prices in JavaScript to a product, you should start by setting the correct configuration for its price calculation mode on its card in the IdoSell panel, which in this case should indicate the option Price calculation formula.



Here you have to remember that the Mode Price calculation formula cannot be set on the card where the setting Specify other prices for shops is active and then you have to deactivate this setting before it is activated.

After defining the appropriate mode you should then go to form edit formula. To do this, click on the link [edit] under the previously selected option in the Price Calculation Mode column.

You can define the formula in the formula edit box:

  • formula parameters, i.e. their value attributes stored in the JSON structure to be available for the buyer to choose from on your shop's product card

  • the JavaScript function of the formula, i.e. the logic of calculating the final price of the product including all or selected attributes of the product, described in the JavaScript language.

In the code of the formula you are creating, you can also use active variables in the formula, after referring to the method getProductDataFromEngine();, the list of which is available under the link [Active variables] to the right of the field header of the edited function.

After you have completed the necessary parameters and JavaScript functions, you can check whether your formula is working correctly and that its execution time does not exceed acceptable standards. In order to do so, you need to click the 'Check entered formula' button, as a result of which the system will show you a preview of the form of available attributes, where after selecting the appropriate values for them you will be able to check whether the formula correctly calculates the price for them.

Go to the JavaScript formula documentation for calculating prices

Examples of ready-made functions for JavaScript price formulas

Setting different prices in different stores

Product parameters for the JavaScript function:
{
    "minimalPrice": 10,
    "maximalPrice": 500,
}
JavaScrip function:
function calculatePrice() {
    var data = getProductDataFromEngine();

    var price = data["iai:product_minimal_price_gross"];

    if (data["iai:shop_id"] == 1) {

        price += 100;

    } else if (data["iai:shop_id"] == 2) {

        price += 200;

    } else {
        price += 300;

    }
    return {
        price: parseFloat(price).toFixed(3),
        discount: false
    };
}

Setting different prices for sizes in different stores

Product parameters for the JavaScript function:
{
    "minimalPrice": 0,
    "maximalPrice": 500,
}
JavaScrip function:
function calculatePrice() {
    var data = getProductDataFromEngine();

    var price = data["iai:product_minimal_price_gross"];

    if (data["iai:shop_id"] == 4) {
        switch (data["iai:product_size_desc"]) {
            case "37":
                price += 0;
                break;
            case "38":
                price += 20;
                break;
            case "42":
                price += 40;
                break;
            default:
                price += 100;
        }
    } else if (data["iai:shop_id"] == 5) {
        switch (data["iai:product_size_desc"]) {
            case "37":j
                price += 0;
                break;
            case "38":
                price = 25;
                break;
            case "42":
                price += 45;
                break;
            default:
                price += 200;
        }
    } else {
        switch (data["iai:product_size_desc"]) {
            case "37":
                price += 0;
                break;
            case "38":
                price += 35;
                break;
            case "42":
                price += 55;
                break;
            default:
                price += 300;
        }
    }
    return {
        price: parseFloat(price).toFixed(3),
        discount: false
    };
}

Setting a promotional price which will be available in the store only from/to the specified date and time, and then return to the previous level

Product parameters for the JavaScript function:
{
    "minimalPrice": 0,
    "maximalPrice": 500,
}
JavaScrip function:
function calculatePrice() {
    var data = getProductDataFromEngine();

    var price = data["iai:product_max_price_gross"];

    var currentDate = new Date();
    var promoStart = new Date('2020-05-27T16:57:00');
    var promoEnd = new Date('2020-06-28T17:04:00');

    var canBeInPromotion = ((currentDate.getTime() > promoStart.getTime()) &&
        (currentDate.getTime() < promoEnd.getTime()));

    if (canBeInPromotion)
        price -= 150;

    return {
        price: parseFloat(price).toFixed(2),
        discount: true
    };
}

Setting conversion factors for the values of the attributes specified for the product

Product parameters for the JavaScript function:
{
    "minimalPrice": 25,
    "maximalPrice": 500,
    "colour": {
        "type": "select",
        "names": {
            "pol": "Kolor nadruku",
            "eng": "Printing colour"
        },
        "values": {
            "orange": {
                "names": {
                    "pol": "pomara\u0144czowy",
                    "eng": "orange"
                }
            },
            "green": {
                "names": {
                    "pol": "zielony",
                    "eng": "green"
                }
            },
            "red": {
                "names": {
                    "pol": "czerwony",
                    "eng": "red"
                }
            }
        }
    },
    "width": {
        "type": "input",
        "names": {
            "pol": "Szeroko\u015b\u0107 nadruku [cm]",
            "eng": "Printing width [cm]"
        },
        "settings": {
            "type": "int",
            "min": 0,
            "max": 22
        }
    },
    "hight": {
        "type": "input",
        "names": {
            "pol": "Wysoko\u015b\u0107 nadruku [cm]",
            "eng": "Printing height [cm]"
        },
        "settings": {
            "type": "float",
            "min": 0,
            "max": 60.45
        }
    },
    "printSide": {
        "type": "checkbox",
        "names": {
            "pol": "Strona nadruku",
            "eng": "Print side"
        },
        "values": {
            "leftSide": {
                "names": {
                    "pol": "Po lewej",
                    "eng": "Left side"
                }
            },
            "rightSide": {
                "names": {
                    "pol": "Po prawej",
                    "eng": "Right Side"
                }
            }
        },
        "settings": {
            "minChecked": 1,
            "maxChecked": 1
        }
    },
    "typeOfPrinting": {
        "type": "radio",
        "names": {
            "pol": "Rodzaj nadruku",
            "eng": "Type of printing"
        },
        "values": {
            "sublimation": {
                "names": {
                    "pol": "Sublimacja",
                    "eng": "Sublimation"
                }
            },
            "flexFlock": {
                "names": {
                    "pol": "Folia flex\/flock",
                    "eng": "Flex\/flock foil"
                }
            },
            "flexLatex": {
                "names": {
                    "pol": "Flex lateksowy",
                    "eng": "Flex latex"
                }
            },
            "thermotransfer": {
                "names": {
                    "pol": "Termotransfer",
                    "eng": "Thermotransfer"
                }
            }
        }
    },
    "additionalInscription": {
        "type": "input",
        "names": {
            "pol": "Dodatkowy napis",
            "eng": "Additional inscription"
        },
        "settings": {
            "type": "string",
            "min": 0,
            "max": 20
        }
    }
}
JavaScrip function:
function calculatePrice(colour, width, hight, printSide, typeOfPrinting, additionalInscription) {
    switch (colour) {
        case "orange":
            price += 0;
            break;
        case "green":
            price += 0.10 * price;
            break;
        case "red":
            price += 0.30 * price;
            break;
    }

    if (width && hight)
        price += (width * hight * 10)

    printSideFactor = 1;
    
    if (printSide.indexOf("leftSide") > -1) {
        printSideFactor += 0.1;
    }

    if (printSide.indexOf("rightSide") > -1) {
        printSideFactor += 0.3;
    }

    price *= printSideFactor;

    if (typeOfPrinting && typeOfPrinting == "sublimation") {
        price += 150;
    }
    if (typeOfPrinting && typeOfPrinting == "flexFlock") {
        price += 250;
    }
    if (typeOfPrinting && typeOfPrinting == "flexLatex") {
        price += 350;
    }
    if (typeOfPrinting && typeOfPrinting == "thermotransfer") {
        price += 450;
    }

    if (additionalInscription && additionalInscription.length > 0) {
        price += additionalInscription.length * 3;
    }

    return {
        price: parseFloat(price).toFixed(3),
        discount: false
    };
}