Automatically Add “Get Y” Product When “Buy X” Is Added to Cart

Hi everyone,

I’m working on a custom Shopify Plus implementation and would love some guidance from the community.

Goal: Automatically add the “Get Y” product to the cart when the “Buy X” product (defined in an automatic Buy X Get Y discount) is added to the cart ( without using shopify app store)

What I’m Trying to Achieve : I want to retrieve the Buy X and Get Y product variant IDs — along with the required quantity — directly from the automatic discount configuration in Shopify.

Once I have those IDs, I’ll use them in my theme’s cart.js where I handle the logic of when a “Buy X” product is added to the cart, automatically add the matching “Get Y” product variant.

What I Need Help With

  • How to fetch the Buy X and Get Y variant IDs and quantities from the automatic discount data ( using graphql query, is there a way to use shopify functions / custom app to retrieve the Buy X and Get Y)

Thanks in advance for any insights or shared experiences :folded_hands:

Hi Mohammed,

The discount engine is designed to apply discounts at checkout, but their configuration details (like specific variant IDs and quantities) aren’t directly exposed through GraphQL or the Storefront API for use in your theme’s cart.js. This is by design—discount logic runs server-side during checkout.

That said, you could use metafields to store the X and Y product variants combinations, then use a checkout ui extension or a theme extension to add Y to cart if X is found. This would also require having discounts or a discount function that reflect same X and Y combinations.

Let us know how you make out!

Thanks,
Ian