Concurrent discounts with overlap in line items?

Hey y’all!
Apologies if this has been covered before.

I’m working on building a discount function app that discounts a particular product group when all the items are in the cart together (pretty much a bundle but not using the bundle API).

I have it working the way I like it on its own, however we want to implement multiple versions of the discount at the same time. E.G.

  • Bundle 1 discount: Product A, B, and C 12% off
  • Bundle 2 discount: Product A, B, C, and D 20% off

This is where it gets interesting. I’ve set up the rust function run to count bundle components in the cart so that if the customer wants to add multiple bundles they can, but if you add a mismatch (e.g. 2 product A, 2 product B, and 1 product C) it splits out the line items so only one set gets the discount.

The problem is that if I have Bundle 1 in the cart and I add some items from bundle 2, it messes with the component counting function because it will just find the qualifying cart lines regardless if they have been discounted previously or not.

If I can flag a line item as belonging to Bundle 1 or Bundle 2 (or already having a discount applied to it) I should be able to apply that to the counting logic and not count them toward other bundles. Is there a way to do that? I’ve looked into selection strategy, but if I use First then users would only be able to add one bundle to the cart and get it discounted.

Here’s a quick(ish) video that visualizes the problem

Thanks for taking a look!

For extra context, I found this Github issue that makes it appear possible, but the link to the documentation for cart line targets that closes the issue doesn’t really clear it up for me.

It looks like I could maybe add a metafield to the line item based on the triggeringDiscountCode to flag it (does that field work if it’s an automatic discount?), and then include logic that checks against the line item’s metafields when I count up the distinct bundles. If there’s an easier way I’d love to hear it :smile:

Ok, so was able to get this sorted.
I set up the frontend so that each separate “bundle” page adds some of the same products but flags it with a unique attribute value with the key of _bundle when adding to cart so we can group them.
Then we can query that key in the generate_run file:

uery Input($collectionIds: [ID!]) {
  cart {
    lines {
      id
      quantity
      bundleAttribute: attribute(key: "_bundle") {
        value
      }
      cost {
        subtotalAmount {
          amount
        }
      }
      merchandise {
        __typename
        ... on ProductVariant {
          product {
            id
            inAnyCollection(ids: $collectionIds)
          }
        }
      }
    }
  }

Then I created a field in the discount UI that matches the attribute value (based on template suffix), so in the function run we can access that value to search for line items that have that attribute added to them.

I feel like there’s probably a simpler, less convoluted way to do this but it’s working so far!