Personalized checkout and custom promotions with Shopify Scripts
//Run.js // @ts-check import { DiscountApplicationStrategy } from "../generated/api"; // Use JSDoc annotations for type safety /** * @typedef {import("../generated/api").RunInput} RunInput * @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult * @typedef {import("../generated/api").Target} Target * @typedef {import("../generated/api").ProductVariant} ProductVariant */ /** * @type {FunctionRunResult} */ const EMPTY_DISCOUNT = { discountApplicationStrategy: DiscountApplicationStrategy.First, discounts: [], }; /** * @param {RunInput} input * @returns {FunctionRunResult} */ export function run(input) { const targets = input.cart.lines // Only include cart lines with at least one pair of items (quantity >= 2) .filter((line) => line.quantity >= 2) // Check for the relevant product tags in the product associated with each cart line .filter((line) => { // Assuming the tags are part of the product or product variant const productTags = line.merchandise.product.tags || []; // Check for different discount tags (2FOR200, 2FOR300, 3FOR500) const has2FOR200 = productTags.includes("2FOR250"); const has2FOR300 = productTags.includes("2FOR300"); const has3FOR500 = productTags.includes("3FOR500"); // Only include lines with at least one of the discount tags return has2FOR200 || has2FOR300 || has3FOR500; }) .map((line) => { // Get the appropriate discount based on the tag and quantity const productTags = line.merchandise.product.tags || []; let discountValue = 0; if (productTags.includes("2FOR200") && line.quantity % 2 === 0) { discountValue = 16.0; // 16% discount for 2FOR200 } else if (productTags.includes("2FOR300") && line.quantity % 2 === 0) { discountValue = 30.0; // 30% discount for 2FOR300 } else if (productTags.includes("3FOR500") && line.quantity % 3 === 0) { discountValue = 20.0; // 20% discount for 3FOR500 } // Calculate how many groups (pairs or triplets) are eligible for discount const eligibleGroups = discountValue > 0 ? Math.floor(line.quantity / (productTags.includes("3FOR500") ? 3 : 2)) : 0; // Generate discount targets for each eligible group return Array.from({ length: eligibleGroups }, () => /** @type {Target} */ ({ cartLine: { id: line.id, }, })); }) // Flatten the array of arrays into a single array .flat(); // If no targets are found, return empty discount if (!targets.length) { console.error("No cart lines qualify for the discount."); return EMPTY_DISCOUNT; } return { discounts: [ { // Apply the discount to the collected targets targets, // Define a percentage-based discount (adjust the value dynamically) value: { percentage: { value: "10.0", // Default discount (you can adjust this based on actual logic) }, }, }, ], discountApplicationStrategy: DiscountApplicationStrategy.First, }; }
query RunInput { cart{ lines { quantity merchandise { ... on ProductVariant { product{ id hasTags (tags: ["2FOR250"]) { hasTag tag } } } } } } }
I am having a problem with this code.It is not applying on the tags on checkout.
Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025Expand into selling wholesale with Shopify Academy’s learning path, B2B on Shopify: Lau...
By Shopify Jan 28, 2025