Shopify functions - how to apply free shipping as a discount

Topic summary

A developer is attempting to create a Shopify function that applies free shipping as a discount when a certain quantity of products is added to the cart, rather than a percentage-based monetary discount.

Current Implementation:

  • The developer shared JavaScript code using the product discount API
  • Their GraphQL query retrieves cart line items and discount configuration metadata
  • The code filters for cart lines with quantity ≥ 2 and product variant merchandise

Key Issue:

  • The developer is using the wrong API endpoint for their use case

Solutions Provided:

  • One user recommended using Shop Circle’s free Shipping Calculator app as a third-party solution
  • Another user clarified that the Shipping Discount API should be used instead of the Product Discount API for implementing free shipping discounts
  • Reference documentation was provided for the correct API endpoint

Status: The discussion remains open with one follow-up question asking if the original poster resolved the issue, but no response has been provided yet.

Summarized with AI on November 10. AI used: claude-sonnet-4-5-20250929.

Hello,

I am looking to create an extension similar to the discount experience here: https://shopify.dev/docs/apps/selling-strategies/discounts/experience/getting-started

but rather than apply a percentage monetary discount, apply free shipping when x amount of products have been added to the cart.

// -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
*/

/**
*  {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
  discountApplicationStrategy: DiscountApplicationStrategy.First,
  discounts: [],
};

// The configured entrypoint for the 'purchase.product-discount.run' extension target
/**
*  {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
  const targets = input.cart.lines
  // Only include cart lines with a quantity of two or more
  // and a targetable product variant
  .filter(line => line.quantity >= 2 &&
    line.merchandise.__typename == "ProductVariant")
  .map(line => {
    const variant = /**  {ProductVariant} */ (line.merchandise);
    return /**  {Target} */ ({
      // Use the variant ID to create a discount target
      productVariant: {
        id: variant.id
      }
    })
  });

  if (!targets.length) {
    // You can use STDERR for debug logs in your function
    console.error("No cart lines qualify for volume discount.");
    return EMPTY_DISCOUNT;
  }

  return {
   //need to give the customer free shipping here
  };
};

My graphQL is currently as follows: 

query RunInput {
    discountNode {
    metafield(namespace: "$app:shipping-discount", key: "function-configuration") {
      value
    }
  }
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ...on ProductVariant {
            id
        }
      }
      df{
        
      }
    }
  }
}

Thanks

You need Shop Circles free Shipping Calculator app, https://shopcircle.co/apps/shipping-rates-calculator-plus

Hi @SHOPFUNCTIONS28
You need to use the Shipping Discount API, rather than the Product Discount API
https://shopify.dev/docs/api/functions/reference/shipping-discounts

or alternatively try my new app :slightly_smiling_face: shown in my signature

did you figure this out?