Adding a product to the cart at a discounted price

Hi, I have searched extensively online and in the Shopify forums for an answer to a question and have found none, so here I am.

I am developing a Shopify app which provides customers an opportunity to buy products at a discount. This requires a way to add an existing product to the shopping cart at a discounted rate. So if the product is $100, the customer may get 10% off, and the product should be added to the cart at $90 instead of $100.

The Discounts APIs may be an option here; however there appears to be no way to apply a discount code automatically to a shopping cart. The discount only appears when a discount code is manually added at discount or if a special link containing the discount code is visited. This is not what I want as it is would confuse end customers; I want them to immediately see a discount in their cart.

Therefore, I have opted to clone products. I remove all variants except the chosen one and then calculate and use the discounted price for that remaining variant, and then that cloned product is added to the customer’s cart.

Cloning works great except that the product is now potentially shown in collections around the site. For example, if a collection has a rule to show all products containing “Hiking” and the cloned product contains this word, the cloned product will show there, and other customers could see the discounted product — this should not be possible. Shop owners would be angry.

I need a way to hide the cloned product from all smart collections. I could add a rule to exclude based on some criteria, but this does not work with disjunctive smart collections as products are included if any rule evaluates to true (“OR” rather than “AND”).

I can modify themes to skip cloned products when for looping through collection products, but then this messes up pagination (e.g., the count is off, and there could be one or more blank pages).

I can set inventory to zero or unpublish the product, but then the customer can no longer purchase the product (an error shows when checking out).

There MUST be a way to hide a give product from all collections. Maybe something with metafields. I was able to hide products from search engine listings and searches via metafields as described here: https://shopify.dev/tutorials/manage-seo-data-with-admin-api#hide-a-resource-from-search-engines-and-sitemaps.

What are my options? Is there a way to hide a product from all collections?

Or, is there a better way than cloning to discount a product when adding it to the cart?

Please help.

Anyone? Please help.

I’d like to add that using Automatic Discounts will not work as only one automatic discount may be active per shop.