Example Shopify Product Discount Function to limit discounts to certain markets and optionally exclude line items on sale
This Shopify Function dynamically applies a 10% discount to eligible products in specific markets, excluding items that are already on sale. It is designed to integrate seamlessly with Shopify Markets, allowing for targeted discounts that enhance the shopping experience and promote sales in designated regions.
- Market-Specific Discounts: Applies discounts only to products in specified markets (
de
,usa
). - Exclusion of Sale Items: Automatically excludes products that are already on sale from receiving additional discounts.
- Dynamic Discount Application: Discounts are applied dynamically based on the cart contents and market during the checkout process.
- Shopify plan that supports Shopify Functions.
- Access to the Shopify admin area to manage functions.
To set up this project:
- Create an extension-only app:
shopify app init
- Create a new Product Discount Function and replace the code in the 'src' folder.
- Deploy the changes
yarn deploy
- Create a discount code using the discountCodeAppCreate mutation
-
Market Handles:
- Adjust the
ELEGIBLE_MARKETS
array in the code to include or exclude market handles as required for your business strategy.
- Adjust the
-
Sale Item Exclusion:
- Modify
EXLUDE_LINE_ITEMS_ON_SALE
totrue
to ensure sale items are excluded from discounts, or set it tofalse
to include them in the discount calculations.
- Modify
Once deployed, the function will automatically apply a 10% discount to all eligible non-sale items in the specified markets at checkout. The function runs in the background and requires no manual intervention post-setup.
-
Discounts Not Applied Correctly:
- Verify that the market handle during checkout matches one of the configured handles in
ELEGIBLE_MARKETS
. - Confirm that items are not on sale if
EXLUDE_LINE_ITEMS_ON_SALE
is set totrue
.
- Verify that the market handle during checkout matches one of the configured handles in
-
Function Errors:
- Check the function for any syntax or logical errors.
- Ensure that all configurations and external references are correctly set up.
For further assistance, please consult the relevant developer documentation.