Help with Shopify Cart Transform Function: Query Fields Not Found

Hello Dev Community! :waving_hand:

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 RESTART is 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:

  1. Error 1: “Cannot query field title on type CartLine”.
  2. Error 2: “Cannot query field variant on type CartLine”.

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:

  1. Updated GraphQL Query to include product details and subscription-related data.
  2. Tried simplifying the query, but still got errors related to missing fields.

What I Need:

  1. Guidance on which fields are available for querying on CartLine.
  2. 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 a CartLine.
  • Any insights into why the variant and title fields are missing.

Thank you in advance! :folded_hands: Any help or advice would be greatly appreciated!

@Saad_Ullah_Sajid :
Note: The product field is not directly available on cart.lines in the CartTransformRunInput schema. The schema typically provides merchandise (resolving to ProductVariant), and to access the parent product, you would query within merchandise like merchandise { ... on ProductVariant { product { id id title } }.

Try something like this :

query CartTransformRunInput {
  cart {
    lines {
      id
      quantity
      attributes(key: "addons_added") { key value }
      cost {
        amountPerQuantity { amount currencyCode }
        totalAmount { amount currencyCode }
      }
      merchandise {
        ... on ProductVariant {
          id
          title
          price { amount currencyCode }
          product {
            id
            title
          }
        }
      }
    }
  }
}

Hi @Saad_Ullah_Sajid - are you still experiencing this issue or ca I mark this as solved?