Examples

This page contains examples about common tasks like login, product price calculation and other similar examples.

Contents

Calculating product total price

There are few things you should keep in mind when calculating total price of a product:

Description of calculation of these below.

Calculating product’s ingredients’ price

Every ingredient has its default price defined. When calculating ingredient’s price in product its price must be calculated using pricing factor and pricing sum from product’s variant data. Pricing factor defines the factor ingredient price is multiplied with. Pricing sum defines the sum that must be added into ingredient price. Normally found values is 1 for the factor and 0 for the sum. The values can be found from variant’s ingredient_pricing_factor and ingredient_pricing_sum.

Formula to calculate price of added ingredient:

ingredient_price = ingredient price * variant pricing factor
ingredient_price = ingredient_price + variant pricing sum

Formula to calculate price of changed ingredient

changed_ingredient_price = (new ingredient’s price * variant pricing factor) + variant pricing sum
original_ingredient_price = (original ingredient’s price * variant pricing factor) + variant pricing sum
ingredient_price = changed_ingredient_price - original_ingredient_price

Sum of an ingredient change might be negative. If it is negative it should be considered as 0. Changing ingredient can’t make total price of product lower than original price.

Calculating product extras price

Calculating product extras is fairly simple. Extras can either increase or decrease total price of product, or might be free (price 0).

DO NOT use pricing factor and pricing sum to calculate extra price

Calculating order product price

Basic formula to calculate total price of order product is:

ingredient_price = added ingredients’ price + changed ingredients’ price
product_price = (variant price + ingredient_price + extra prices) * amount
Note that when restaurant has lunch time and restaurant has dinner_time_in_online_orders set, product’s dinner_price should be used instead of normal price.

Small order fee

It is possible for restaurants to have an additional fee applied to orders. If the restaurant’s small_order_fee_limit is greater than 0.00, value of small_order_additional_fee should be added to orders where the total price of products in the order is less or equal to the small_order_fee_limit.

The small order additional fee is to be added to both takeaway (“fetch” in the API) and delivery orders.

if(small_order_fee_limit > 0 && total_price_of_products <= small_order_fee_limit) {
    order_total_price = order_total_price + small_order_additional_fee;
}

Calculating delivery fee for delivery order

It is possible for restaurants to have free delivery after the total price of order exceeds certain amount. Given that total price of the products in the order is greater than the value of restaurant’s delivery_free_over, then delivery_fee is not added. I f the restaurant’s delivery_free_over is 0.00, delivery_fee is added to the order.

Restricting creation of delivery orders

Restaurants may have an attribute delivery_possible_over set. If the value of the delivery_possible_over attribute is greater than zero, total price of all products in the order must exceed the value of the delivery_possible_over for ordering using home delivery methods to be possible.

If the value of the delivery_possible_over is zero (0.00), the attribute does not limit ordering using the home delivery methods, meaning the feature is disabled.

if(delivery_possible_over > 0 && total_price_of_products <= delivery_possible_over) {
    // Delivery order is not possible
}

Calculating the order’s total price

The sub prices should be first calculated using the information and formulas provided above.

The basic formula to calculate the total price of takeaway order is:

fetch_order_absolute_total_price = total_price_of_products + small_order_additional_fee + payment method cost

The basic formula to calculate the total price of delivery order is:

absolute_total_price = fetch_order_absolute_total_price + delivery_fee + night time fee

If there is payment method cost percent too, the total price needs to be multiplied with payment method cost percent.