Use Case:
An employee discount code (“EMPLOYEE”) needs to trigger a free shipping method in Shopify. When this code is present, employees can select free pickup, which the middleware later converts to Will Call for front desk collection.
Solution Provided:
Use input.cart.discountCodes array within a Shipping Rate Presentment Function to detect the discount code. A code example demonstrates:
Checking if “EMPLOYEE” code exists
Hiding all paid shipping options
Adding a free “Employee Pickup” rate
Returning no operations if code is absent
Follow-up Question:
Can the same approach discount line items based on price or variant metafields? The business wants to avoid customer-based discounting (tags, login status) since codes are shared with friends and family. They plan to use compare-at-price or metafields to prevent selling below cost while maintaining the employee benefit.
Status: Solution provided for shipping; line item discounting question remains open.
Summarized with AI on October 24.
AI used: claude-sonnet-4-5-20250929.
Have a fairly unique use case. We have an employee discount, it is substantial. We want to retrieve the discount code (something like “EMPLOYEE”) and if that code is present, offer a free shipping method (we charge for shipping). [Our middleware takes orders with the discount code and changes the shipping method to Will Call, so employees pick up their order at the front desk].
So far I have not seen any practical way to retrieve the discount code if any entered into checkout to offer the free shipping to employees. [We have plenty of Shopify functions up and running].
Any guidance or help would be greatly appreciated.
The key is the input.cart.discountCodes array, which is available inside any Shipping Rate Presentment Function.
You can create a function that checks this array to see if the ‘EMPLOYEE’ code is present. If it is, the function can then modify the available shipping rates, for example, by hiding all paid options and adding a new free one. You can try this logic:
import { shopifyApi } from "@shopify/shopify-api";
export default shopifyApi.define(async (input) => {
const isEmployee = input.cart.discountCodes.some(
(code) => code.code === "EMPLOYEE"
);
if (isEmployee) {
return {
operations: [
// This example hides all other rates and adds a new free one.
...input.shippingOptions.map((option) => ({
hide: { shippingOptionHandle: option.handle },
})),
{
add: {
id: "employee-pickup",
title: "Employee Pickup (Free)",
price: { amount: 0 },
},
},
],
};
}
// If the employee code isn't present, do nothing.
return { operations: [] };
});
After deploying this function within your app, you just need to enable it in your store’s shipping settings.
Thanks! Can I also use this to discount the line items on either price or a metafield on the variant if present? The reason for this is we do NOT want to use the logged in customer attribute, the employee discounts get shared with “friends and family” so we don’t want to manage the customer attribute like tags or whatever to discount, the code works for us. [The business reason behind this development is that the discount is so large that we want to use the compare-at-price, we can manage moving that into a metafield, we don’t want a discount so large we are selling below cost, but want this benefit to employees and their friends and family}