Shopify Function not updating cart when login status changes

I have a simple product discount function, that checks the tag of a logged in customer and the tag of a product to discount it. It works fine when the customer is logged in and adds to cart, but fails to update the price when:

A. A customer is not logged in, adds the product to cart.

B. The customer then logs in, and the product is not discounted.

C. The product will be discounted if the customer changes quantities of the product or removes/adds back the product.

Here is the run.graphql file

query RunInput {
  cart {
    buyerIdentity {
      customer {
        id
        active_sub: hasAnyTag(tags: ["Active Subscriber"])
      }
    }
    lines {
      quantity
      sellingPlanAllocation {
        sellingPlan {
          name
        }
      }
      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
              fifty_percent: hasAnyTag(tags: ["ellie-exclusive"])
            }
        }
      }
    }
  }
}

And here is the run.rs file:

// -check
import { DiscountApplicationStrategy } from "../generated/api";

// Use JSDoc annotations for type safety
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
* @typedef {import("../generated/api").Target} Target
* @typedef {import("../generated/api").ProductVariant} ProductVariant
*/

/**
*  {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
  discountApplicationStrategy: DiscountApplicationStrategy.All,
  discounts: [],
};

// The configured entrypoint for the 'purchase.product-discount.run' extension target
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
  const customer1 = input.cart.buyerIdentity?.customer?.active_sub;
  console.error("customer1 value = " + customer1);
  const targets = input.cart.lines
  // Only include cart lines with a quantity of two or more
  // and a targetable product variant
  .filter(line => line.quantity >= 1 &&
    line.merchandise.__typename == "ProductVariant" && customer1 == true && line.merchandise.product.fifty_percent == true && line.sellingPlanAllocation?.sellingPlan.name != "Delivery every 1 Month" && line.sellingPlanAllocation?.sellingPlan.name != "Delivery every 1 Month, Charge every 3 Months")
  .map(line => {
    const variant = /**  {ProductVariant} */ (line.merchandise);
    return /**  {Target} */ ({
      // Use the variant ID to create a discount target
      productVariant: {
        id: variant.id
      }
    });
  });

  if (!targets.length) {
    // You can use STDERR for debug logs in your function
    console.error("No cart lines qualify for member discount.");
    return EMPTY_DISCOUNT;
  }

  // The /shopify_function package applies JSON.stringify() to your function result
  // and writes it to STDOUT
  return {
    discounts: [
      {
        // Apply the discount to the collected targets
        targets,
        // Define a percentage-based discount
        value: {
          percentage: {
            value: "50.0"
          }
        }
      }
    ],
    discountApplicationStrategy: DiscountApplicationStrategy.All
  };
};

Question: how can I trigger the function to evaluate when the customer is now logged in with the same cart, without requiring the customer to change quantities, etc. Or is this possible at all?

Thanks.

You’re not the one running into this issue. I don’t think there’s a workaround for forcing the cart to recalculate discounts.

At first, I thought you could use the AJAX Cart API to quickly add & remove a product from the cart (to force recalculation). However, Shopify will likely cache the calculated discounts for the previous state of the cart, so this wouldn’t work.

Sorry, I meant, “you’re not the ONLY one”

A whole year later and this is still an issue!

@fwallace Did you ever find a workaround..?

Hey @fwallace . Hope you are doing well!

Shopify’s function only triggers on cart changes, not login.
Force a silent cart update after login to re-trigger it.