// -check
import { DiscountApplicationStrategy } from "../generated/api";
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
* @typedef {import("../generated/api").ProductVariant} ProductVariant
* @typedef {import("../generated/api").Discount} Discount
* @typedef {import("../generated/api").Target} Target
* @typedef {import("../generated/api").ProductHasAnyTagArgs} ProductHasAnyTagArgs
* @typedef {import("../generated/api").CartLine} CartLine
*
*
*/
/**
* @type {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts: [],
};
/**
* {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
let totalqty = 0;
let strqty = 0;
let bundleqty =0;
let discount_starter = 0;
let starter_array =[];
let bundle_array=[];
let newtarget = [];
var new_array = [];
var totalDiscount = 0;
var baseDiscount = 30.00;
const targets = input.cart.lines
.filter(line => line.merchandise?.__typename === "ProductVariant")
.map(line => {
const productVariant = line.merchandise;
const cost = line.cost;
const qty = line.quantity;
if (productVariant && productVariant.__typename === "ProductVariant" && productVariant.id ) {
const product = productVariant.product;
const tag = product.hasTags.map(tagObject => tagObject.tag);
const hastags = product.hasTags.map(tagObject => tagObject.hasTag);
const hasStarterTag = product.hasTags.some(tagObject => tagObject.tag === "starter" && tagObject.hasTag);
const hasBundleTag = product.hasTags.some(tagObject => tagObject.tag === "bundle-1" && tagObject.hasTag);
if (hasStarterTag) {
strqty += qty;
var inner_starter_array = {
"productVariant": {
id: productVariant.id,
quantity: qty
}
}
starter_array.push(inner_starter_array);
// console.log('inner starer', JSON.stringify(inner_starter_array));
// console.log(product.id);
}
if (hasBundleTag) {
bundleqty += qty;
let inner_bundle_array = {
id: product.id,
tags: product.hasTags,
quantity: qty
}
bundle_array.push(inner_bundle_array);
}
}
return null;
})
.filter(Boolean);
if (bundleqty % 2 === 0) {
discount_starter = bundleqty / 2;
} else {
let even_bundle_qty = bundleqty - 1;
discount_starter = even_bundle_qty / 2;
}
if(strqty == discount_starter){
var newArrayStarter = [];
starter_array.forEach((item) => {
var existingItem = newArrayStarter.find((newItem) => newItem.productVariant.id === item.productVariant.id);
if (existingItem) {
existingItem.productVariant.quantity += item.productVariant.quantity;
} else {
newArrayStarter.push({ productVariant: { ...item.productVariant } });
}
});
newtarget = newArrayStarter;
totalDiscount = baseDiscount * strqty;
// console.log("if executes",JSON.stringify(newtarget));
}else{
totalDiscount = baseDiscount;
var starter_length = starter_array.length;
// console.log("Length of Starter Array : "+ starter_length +" And count of Discount Starter "+ discount_starter);
// To check starter products are different or not
if(starter_length >= discount_starter)
{
var discountStartrerArray = starter_array.slice(0, discount_starter);
// console.log("discountStartrerArray",discountStartrerArray);
var newDiscountStarterArray = [];
var DiscountStarterCountCheck = discount_starter;
discountStartrerArray.forEach((element, index) => {
if(DiscountStarterCountCheck > 0)
{
if(element.productVariant.quantity <= DiscountStarterCountCheck)
{
newDiscountStarterArray.push(element);
DiscountStarterCountCheck = DiscountStarterCountCheck - element.productVariant.quantity;
}else{
var newElementt = {
"productVariant": {
"id": element.productVariant.id,
"quantity": discount_starter
}
};
newDiscountStarterArray.push(newElementt);
DiscountStarterCountCheck = DiscountStarterCountCheck - discount_starter;
totalDiscount = baseDiscount * discount_starter;
}
}
});
newtarget = newDiscountStarterArray;
// console.log("inner if executes");
}else{
// console.log("inner else executes");
var elseDiscountStarterArray = [];
starter_array.forEach((element, index) => {
var newElement = {
"productVariant": {
"id": element.productVariant.id,
"quantity": discount_starter
}
};
elseDiscountStarterArray.push(newElement);
});
totalDiscount = baseDiscount * discount_starter;
newtarget = elseDiscountStarterArray;
}
// console.log("else executes",JSON.stringify(newtarget));
}
if (newtarget.length > 0) {
// console.log('total discount',totalDiscount);
const DISCOUNTED_ITEMS = {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts: newtarget.map(target => ({
targets: [target],
value: {
fixedAmount: {
amount: totalDiscount / newtarget.length, // Distribute the total discount among all quantities
},
},
message: "30Rs off",
})),
};
console.log(JSON.stringify(DISCOUNTED_ITEMS));
return DISCOUNTED_ITEMS;
}else{
return {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts: [],
};
}
}
Hello,
I wanted to apply a discount to each item I am getting in the array. But in my case, the array is correct and the discount is being applied only to the first item, not to all items, so how can I fix this?