Order discount function api not returning deliveryGroups

LucasBudi
Shopify Partner
7 0 2

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:


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

Replies 3 (3)

Liam
Shopify Staff
2731 302 783

Hi Lucas,

 

Thanks for flagging this, I've reached out to our internal team to get more info on this. Will report back here asap.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Liam
Shopify Staff
2731 302 783

Hi again - I've confirmed with our internal functions team that this is a known limit of discount functions - delivery groups will always be null. The intention is to hide this field for certain Function APIs.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

LucasBudi
Shopify Partner
7 0 2

Hi Liam - thanks for chasing this up! Ah that's a shame it will always be null.. When we were exploring the API docs prior to commencing development we believed the deliveryGroups data would be attainable - might be worth clarifying this limitation in the docs for future devs. 

Currently exploring a workaround where the address validation is achieved via checkout ui extension, I'm struggling to pass this validation into the order discount function to enable/disable effectively.. Do you know any other way our desired functionality could be achieved?

Previously we've tried using the shipping discount function to run our address validation then add/remove an attribute from the cart for which the order discount function would check for. The problem we had here was not being able to control which function runs first.

Thanks again for your help mate.