<%
// =================================================================
// Final Code by Gemini - This goes into the CONTENT field of the
// "Create a page" node, with "Code Mode" enabled.
// =================================================================
// --- 1. GATHER DATA ---
// Get all 12 book items from the Google Sheet node that ran before.
const allBooks = $('Google Sheet').all();
// Get the form data from the initial webhook.
const webhookData = $('Webhook').first().json.body;
// Get the main data object created by your AI and other nodes.
// IMPORTANT: This assumes the node is named "Code in JavaScript (Lookup in sheet)".
const mainData = $('Code in JavaScript (Lookup in sheet)').first().json;
// Get the coupon code from your HTTP Request node.
const coupon = $('HTTP Request (coupon)').first().json.code.toString().trim();
// --- 2. APPLY LOGIC ---
// Check which phases the user selected.
const addPhase1 = webhookData.add_phase_1 === 'true' || webhookData.add_phase_1 === true;
const addPhase2 = webhookData.add_phase_2 === 'true' || webhookData.add_phase_2 === true;
const addPhase3 = webhookData.add_phase_3 === 'true' || webhookData.add_phase_3 === true;
const selectedBooks = [];
const final_product_ids = [];
// Helper function to find and add books for a selected phase.
function processPhase(phaseNum, shouldAdd) {
if (shouldAdd) {
const startIndex = (phaseNum - 1) * 4;
const phaseBooks = allBooks.slice(startIndex, startIndex + 4);
phaseBooks.forEach(bookItem => {
// IMPORTANT: This assumes your Google Sheet has columns ‘product_id’, ‘image_url’, and ‘name’.
selectedBooks.push(bookItem.json);
final_product_ids.push(bookItem.json.product_id);
});
}
}
// Run the logic for each phase.
processPhase(1, addPhase1);
processPhase(2, addPhase2);
processPhase(3, addPhase3);
// Create the final string of product IDs for the purchase link.
const final_ids_string = final_product_ids.join(‘,’);
// — 3. RENDER THE HTML —
// Now, we start writing the HTML. We can use <%= %> to print our variables.
%>
سیر مطالعاتی اختصاصی <%= mainData.formalName %>
💡 تحلیل هوشمند
<%= mainData.final_output.analysis %>
فاز اول: شروع
۴ کتاب
<%= mainData.final_output.study_plan.phase_1.book_2.title %>
<%= mainData.final_output.study_plan.phase_1.book_3.title %>
<%= mainData.final_output.study_plan.phase_1.book_4.title %>