Hello Dev Community! ![]()
I’m working on a Shopify cart transformation function to apply discounts to subscription products based on the discount code used. However, I’m facing a couple of issues related to missing fields in the GraphQL schema. Here’s what I’m trying to achieve:
Goal:
- If the cart contains subscription products and the discount code
RESTARTis applied, I want to:- Remove the discount from all other products.
- Apply the discount only to the first subscription product in the cart.
Issues I’m Facing:
- Error 1: “Cannot query field
titleon typeCartLine”. - Error 2: “Cannot query field
varianton typeCartLine”.
These errors occur because the fields I’m trying to query, like title and variant, don’t seem to be available on CartLine in the GraphQL schema.
Steps I’ve Taken:
- Updated GraphQL Query to include product details and subscription-related data.
- Tried simplifying the query, but still got errors related to missing fields.
What I Need:
- Guidance on which fields are available for querying on
CartLine. - How to access subscription-related data (
sellingPlanAllocation) for subscription products.
Here’s What I’ve Tried So Far:
Updated GraphQL Query:
query CartTransformRunInput {
cart {
lines {
id
quantity
merchandise {
id
title
}
product {
id
title
}
}
}
}
Updated JavaScript Code:
// @ts-check
/**
* @typedef {import("../generated/api").CartTransformRunInput} CartTransformRunInput
* @typedef {import("../generated/api").CartTransformRunResult} CartTransformRunResult
*/
/**
* @param {CartTransformRunInput} input
* @returns {CartTransformRunResult}
*/
export function cartTransformRun(input) {
const cart = input.cart;
// Check if discount code "RESTART" is applied (adjusted logic based on available fields)
const discountCodes = cart.discountCodes ? cart.discountCodes.map(dc => dc.code.toUpperCase()) : [];
// Only continue if discount code "RESTART" is used
if (!discountCodes.includes("RESTART")) {
return { operations: [] };
}
// Identify subscription products (with selling plan, adjusted based on your available fields)
const subscriptionLines = cart.lines.filter(line =>
line.product && line.product.sellingPlanAllocation !== null // Check if the product has a subscription
);
if (subscriptionLines.length === 0) {
return { operations: [] }; // No subscriptions, do nothing
}
const firstSubLineId = subscriptionLines[0].id; // Get the first subscription line's ID
// Prepare operations:
// 1. Remove discount from all lines
// 2. Reapply it only to the first subscription product
const operations = [
{
removeDiscounts: {
all: true,
},
},
{
addDiscount: {
code: "RESTART",
targets: [
{
productVariant: {
id: subscriptionLines[0].product.id, // Apply discount to the first subscription product
},
},
],
},
},
];
return {
operations,
};
}
What I’m looking for:
- Suggestions on how to correctly access subscription-related fields (e.g.,
sellingPlanAllocation) on aCartLine. - Any insights into why the
variantandtitlefields are missing.
Thank you in advance!
Any help or advice would be greatly appreciated!