We’re currently working on shopify product discounts where we’re fetching the discount value via line item property and utilizing it to provide fixedAmount off on the line item with selling plan. As per the given documentation, we’ve referred that we could also pass theCartLineTarget along with ProductVariantTarget within the targets as part response from product discount functions. Ref: https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/target
Currently we’re trying to replicate the same behaviour as mentioned here within output_2.json of this example where discounts are applied on cartLines rather than productVariants:
https://shopify.dev/docs/api/functions/reference/product-discounts/graphql#functionrunresult
Although the cartLine is accepted as per official documentation we’re getting the below error within the partner logs stating:
"Expected one of valid values: productVariant. Got: cartLine"
Adding the relevant references below -
Output (STDOUT)
{
"discountApplicationStrategy": "ALL",
"discounts": [
{
"targets": [
{
"cartLine": {
"id": "gid://shopify/CartLine/0"
}
}
],
"message": "11 off plus additional discount based on product type value",
"value": {
"fixedAmount": {
"amount": 11
}
}
},
{
"targets": [
{
"cartLine": {
"id": "gid://shopify/CartLine/1"
}
}
],
"message": "15 off plus additional discount based on product type value",
"value": {
"fixedAmount": {
"amount": 15
}
}
},
{
"targets": [
{
"cartLine": {
"id": "gid://shopify/CartLine/2"
}
}
],
"message": "18 off plus additional discount based on product type value",
"value": {
"fixedAmount": {
"amount": 18
}
}
}
]
}
Error Message
[
{
"path": [
"discounts",
0,
"targets",
0
],
"explanation": "Expected one of valid values: productVariant. Got: cartLine"
},
{
"path": [
"discounts",
1,
"targets",
0
],
"explanation": "Expected one of valid values: productVariant. Got: cartLine"
},
{
"path": [
"discounts",
2,
"targets",
0
],
"explanation": "Expected one of valid values: productVariant. Got: cartLine"
}
]
run.graphql
query RunInput {
cart {
lines {
id
quantity
attribute (key: "Product_Type") {
key
value
}
sellingPlanAllocation{
priceAdjustments {
perDeliveryPrice {
amount
}
price {
amount
}
}
sellingPlan {
id
name
recurringDeliveries
}
}
merchandise {
__typename
... on ProductVariant {
id
}
}
}
}
}
run.js
// @ts-check
import { DiscountApplicationStrategy } from "../generated/api";
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
*/
const discount = 25;
const uniqueGiftCard = 2;
/**
* @type {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
discountApplicationStrategy: DiscountApplicationStrategy.All,
discounts: []
};
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
console.error("FUNCTION IS RUNNING");
const targets = input.cart.lines
.filter(line => line.quantity >= 1 && line.sellingPlanAllocation && line.sellingPlanAllocation.priceAdjustments.length && line.sellingPlanAllocation.priceAdjustments[0] && line.sellingPlanAllocation.priceAdjustments[0].price.amount && line.attribute && line.attribute.value && line.merchandise.__typename === 'ProductVariant')
.map(line => {
return {
productVariant: {
id: line.merchandise.id
},
cartLine: {
id: line.id
},
productTypeValue: parseFloat(line.attribute.value) // Assuming attribute value is parsable to float
}
});
const discounts = targets.map(target => ({
targets: [{ cartLine: target.cartLine }],
message: (target.productTypeValue + ' off plus additional discount based on product type value'),
value: {
fixedAmount: {
amount: target.productTypeValue // Dynamic amount based on product type
}
}
}));
/**
* @type {FunctionRunResult}
*/
const ACTUAL_DISCOUNT = {
discountApplicationStrategy: DiscountApplicationStrategy.All,
discounts: discounts
};
return targets.length === 0 ? EMPTY_DISCOUNT : ACTUAL_DISCOUNT;
};
Also note: found the error in code stating - Type ‘{ cartLine: { id: string; }; }’ is not assignable to type ‘Target’.
Property ‘productVariant’ is missing in type ‘{ cartLine: { id: string; }; }’ but required in type ‘Target’.