Discussing Shopify Functions development, deployment, and usage in Shopify apps.
Hi all,
I'm planning the build for an app I'd like to install on multiple stores across multiple organisations via a publicly (unlisted) deployed/distributed app, and I'm trying to determine if Shopify Functions is the best tool for the job.
The purpose of the app is to allow merchants to build Gift With Purchase Campaigns (GWP) that wouldn't interfere with other automatic discounts set up natively on the store (such as a free shipping discount, or a discount code) with the GWP criteria being a simple "Spend X, Get Y Free" set up. The Y would only need to be a specifically set productVariant selected by the merchant, the X a monetary amount.
I begun with spinning up a Remix Template App using a product_discount templated extension for a Shopify Function, and have gotten as far as applying a discount to a specific product variant when the spend threshold is met. Which is great. However, I can't find a successful way to achieve a cart add from within the function. The Storefront API is only accessible if we choose a custom distribution which limits us to one organisation, and any attempts to use the Cart AJAX API with a node HTTP client such as Axios fall short because once compiled to web assembly with Javy we're unable to add items to the event queue/event loop.
I can't seem to find any similar projects on the web to see if there are solutions I can learn from, and this impasse has me thinking I'm trying to make a square peg fit a round hole, so I figured I'd ask plain and simple:
Is my goal of creating an app that can be publicly unlisted for distribution across multiple organisations that can add an item to a user's cart automatically and then discount said item achievable/feasible with Shopify Functions?
If it's a case of using the storefront API on a custom distributed application, where would I invoke a mutation for the storefront API? Again the compilation to web assembly prevents use of async so I can't really post a mutation inside the function once a set condition's been met.
Any help is greatly appreciated!
- Paul
Solved! Go to the solution
This is an accepted solution.
Make sure you've enabled it on your site: https://shopify.dev/docs/apps/selling-strategies/bundles/create#step-3-enable-the-bundle-through-ext...
I'm not sure the instructions are on that specific page, but somewhere in that customized bundles tutorial, it explains how you have to use an API to enable your cart transform on the store.
Use a Cart Transform function.
Hi @tobebuilds Thanks for getting back to me! I hadn't spotted Cart Transform come out of Development preview into general release, so thanks for pointing me in the right direction, if I could ask for some further assistance it'd be really helpful:
I've set up a Cart Transform function with an input and an output that fits the FunctionResult shape with an expand operation included.
The app has had it's typegen updated (I'm working in JS) and been re-deployed (With no release version yet while I'm working on it) and the app is running but no matter what I do on the store where the app's installed, the extension doesn't run (No errors, console logs or anything). Is there something I'm missing? (I've double checked the eligibility criteria, the development store I'm working in has no Shopify Scripts, no checkout.liquid file, doesn't have ReCharge, Global-E or Licensify installed, so I'm not sure why it's not running)
This is an accepted solution.
Make sure you've enabled it on your site: https://shopify.dev/docs/apps/selling-strategies/bundles/create#step-3-enable-the-bundle-through-ext...
I'm not sure the instructions are on that specific page, but somewhere in that customized bundles tutorial, it explains how you have to use an API to enable your cart transform on the store.
Hi @tobebuilds Thanks for getting back to me!
You're right it needs enabling from an API, (for anyone else who stumbles onto this thread with a similar problem):
- Every Shopify Function must have a Function Owner with an attribute that matches the ID of your function
- You can create a CartTransform object via the Admin API (You can use GraphiQL to run the mutation)
- This will serve as your function owner for your Shopify Cart Transform Function, your function should now run and log in the partners dashboard for your app extension.