Hello Partners community,
My team is building an app that applies an order discount based on shipping address entered at checkout. We have been using an order discount function for this but it seems that there is a bug preventing us from continuing… Since we have tried with Shopify scripts with no success due to checkout.liquid being deprecated. Having explored many workarounds for months now it simply doesn’t seem possible for us to implement our desired discount functionality based on the input.cart.deliveryGroups object returning an empty array.
Here is my input.graphql -
query Input {
cart {
deliveryGroups {
deliveryAddress {
address1
address2
city
countryCode
provinceCode
zip
}
selectedDeliveryOption {
code
deliveryMethodType
}
}
}
discountNode {
metafield(namespace: "budi-discount-order", key: "function-configuration") {
value
}
}
}
Here is my function code:
import {
InputQuery,
FunctionResult,
DiscountApplicationStrategy,
Discount,
} from "../generated/api";
import { validateBudiDeliveryGroup } from "../../../utils/validateBudiDeliveryGroup";
export default (input: InputQuery): FunctionResult => {
let discounts: Discount[] = [];
const eligibleForPercentageDiscount = input.cart.deliveryGroups.find(
(group) => {
return (
group.selectedDeliveryOption?.code === "Standard" &&
validateBudiDeliveryGroup(group)
);
}
);
if (eligibleForPercentageDiscount) {
const discountValue = parseInt(
input.discountNode?.metafield?.value || "0",
10
);
discounts.push({
value: {
percentage: {
value: discountValue,
},
},
message: `${discountValue}% off from BUDI`,
targets: [
{
orderSubtotal: {
excludedVariantIds: [],
},
},
],
});
}
return {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts,
};
};
When this function runs successfully at checkout here is the input from the extension logs, where the empty array is evident:
Input (STDIN)
{
"cart": {
"deliveryGroups": []
},
"discountNode": {
"metafield": null
}
}
Here are a few other posts where partners are also facing this issue:
- https://github.com/Shopify/function-examples/discussions/208
- https://community.shopify.com/topic/2184736
- https://community.shopify.com/post/2043447
Another partner mentioned they were in touch with support and there is a ticket for this to be fixed, can we please have some clarity around this - whether Shopify is aware and there is a ticket to fix it?
Best regards,
Lucas