Shopify shipping discount function for tagged customers

HeroLikesToCode
Shopify Partner
2 0 1

Hi all, newbie here. I'm trying to create a free shipping function that only applies to customers tagged with "Faire". 

This is my run.ts

import type {
  RunInput,
  FunctionRunResult,
  Input
} from "../generated/api";


const EMPTY_DISCOUNT: FunctionRunResult = {
  discounts: [],
};

type Configuration = {};

export function run(input: RunInput): FunctionRunResult {
  const configuration: Configuration = JSON.parse(
    input?.discountNode?.metafield?.value ?? "{}"
  );
  if(input?.cart?.buyerIdentity?.customer?.hasAnyTag){
    
    return{
      discounts: [
        {
          message: "Free Shipping for Faire Customers",
          targets: [
            {
            deliveryGroup: {
            id: ""
            }
          }
        ],
          value: {
            percentage: {
              value: 100
            }
          }
        }
      ]
    }
  }
  return EMPTY_DISCOUNT;
};

and this is my run.graphql

query RunInput {
  cart {
    buyerIdentity {
      customer {
        hasAnyTag(tags: ["Faire"])
      }
    }
    deliveryGroups {
      id
      deliveryOptions {
        handle
        title
      }
      selectedDeliveryOption {
        handle
        title
      }
    }
  }
  	
  discountNode {
    metafield(namespace: "$app:flaire-free-shipping", key: "function-configuration") {
      value
    }
  }
}

The function deploys without error but does not work in the cart. Any help would be greatly appreciated.

Replies 2 (2)

rajimulislamjoy
Shopify Partner
186 16 22

You're correctly checking if the customer has the "Faire" tag. However, in the return statement, you're not specifying the deliveryGroup id for which the discount should apply. You need to populate the id field within the targets object with the appropriate delivery group id. Make sure to fetch the deliveryGroup id from the input data and assign it to the targets accordingly.

Please don't forget to Like & Mark Solution to the post that helped you. Thanks!
If you'd like to support me, you can Buy Me a Coffee
Need a Shopify Designer or Dropshipping Expert ?
Hire us at WhatsApp! For Shopify Design | Shopify Customize | Dropshipping
HeroLikesToCode
Shopify Partner
2 0 1

Hey Rajimulislamjoy, thanks for the quick response. I'm not sure how do I go about fetching the is for the deliveryGroup. Any tips on how to do that?