Creating a Promotion Template
Introduction
Step 1: Defining the Required Parameters
Step 2: Template Creation
Template Name
Buy X Get Y Free" or "Get Z% Discount on Y Product with X Purchase.
"Template Type
normal
and coupon
.normal
type, templates will operate based on logical comparisons you create with JavaScript code.coupon
type, the validity of the coupon will be checked first, and then it will operate based on the logical comparisons you create with your JavaScript code. Additionally, a section for dynamically generating coupons will be added to the screen.Template Description
Parameters
Promotions -> Promotion Settings -> Parameters
You can add the parameters you've defined on your screen to your template from here. These parameters will be used to customize your promotion. Please note that once added, these parameters cannot be removed after being used in a promotion.Context
Gift Amount Calculator
1.
2.
3.
cart.suggestionPromotions[]
as an object in the following format.{
"id": "promotionId",
"userMessage": "User message defined in the promotion",
"suggestion": "If you add one more X product, you will receive a discount of Y TL",
}
4.
{
"suggestionPromotions": [
{
"id": "ORNS8M1ROXCXY988",
"userMessage": "",
"suggestion": "Get 1 more and have 20% discount!",
"additionalData": {
"addToCart": [
1, // productID
2,
3
]
}
}
]
}
Buy X and get a discount of Y TL
function calculatePromotion(
body: { subOrders: Array<{ id: string }> },
params: { amoutOff: number; buyProductIds: string[] },
context:{ promotionId: string, userTransactions?: any[], couponGiftAmount?: number },
) {
// We are checking if there is any product from the "buyProductIds" parameter within the "subOrders."
return body.subOrders.some((s) => params.buyProductIds.includes(s.id))
? params.amoutOff
: 0;
}
const body = {
subOrders: [
{
id: '1',
},
{
id: '2',
},
]
};
const params = {
buyProductIds: ["1", "2", "3", "5"], // Apply a discount if any of these products are purchased
amoutOff: 50, // The total amount eligible for the discount
};
// Result: A discount of 50 TL has been calculated based on the purchase of the X product with the existing parameters and returned.
console.log(calculatePromotion(body, params));
Buy X, get Y for Z TL
function calculatePromotion(
body: { subOrders: Array<{ id: string; price: number }> },
params: {
purchasedProductIds: string[]; // An array containing the IDs of products eligible for discounts upon purchase.
discountProductsToBeAppliedIds: string[]; // An array containing the IDs of the products on which the discount will be applied.
discountedProductPrice: number; // The discounted price to be applied for each product.
},
context:{ promotionId: string, // The ID of the promotion that you do not want to be applied again when recalculated from a previous calculation.
userTransactions?: any[], // Data such as user's past transactions and the number of times the promotion has been used can be found.
couponGiftAmount?: number // The entered gift amount on the coupon, if available, is checked for applicability and applied.
},
) {
const purchasedProducts = body.subOrders.filter((s) =>
params.purchasedProductIds.includes(s.id) // Filtering the 'subOrders' array to include only the purchased products based on their IDs.
);
let totalDiscountAmount = 0; // Initialize the variable to hold the total discount amount.
const discountProductsToBeApplied = body.subOrders.filter((s) =>
params.discountProductsToBeAppliedIds.includes(s.id) // Filtering the 'subOrders' array to include only the discounted products based on their IDs.
);
const countOfDiscountProduct = discountProductsToBeApplied.length <= purchasedProducts.length ? discountProductsToBeApplied.length : purchasedProducts.length; // Determine the number of discounted products based on the minimum of either the existing discounted products or the purchased products.
for (let i = 0; i < countOfDiscountProduct; i++) { // Enter a loop for the number of discounted products.
totalDiscountAmount = countOfDiscountProduct * (discountProductsToBeApplied[i].price - params.discountedProductPrice); //Calculate the total discount amount by multiplying the number of discounted products by the difference between the product price and the discounted price.
}
return totalDiscountAmount; // Return the total discount amount.
}
// The 'body' object represents the 'subOrders' data, which contains many products with 'id' and 'price' properties.
const body = {
subOrders: [
{
id: '1',
price: 100,
},
{
id: '2',
price: 150,
},
{
id: '3',
price: 150,
},
{
id: '4',
price: 150,
},
]
};
const params = {
purchasedProductIds: ['1', '2'], // An array containing the IDs of the purchased products.
discountedProductPrice: 50, // The discounted price applied to each product.
discountProductsToBeAppliedIds: ['3', '4'] // An array containing the IDs of the products where the discount will be applied.
};
// Result: With the existing parameters, a total discount of 200 TL is returned based on the purchase of two X products, where, if available, the price of two Y products is set to Z TL each.
console.log(calculatePromotion(body, params));
Gift Item Selector
Apply to the products that need to be purchased for the discount and to the products where the discount is applied.
Apply to all products in the cart.
Apply only to the serviceFee
Calculate Display Price
Calculate Display Price
field is not mandatory. If left empty, the response will default to displaying the product's price information in the displayPrice
field."Detailed Example
calculate
method that are integrated into the promotion process.
Modified at 2024-11-26 14:28:48