Button for Discount

Highlighted
Tourist
5 0 1

Is there a way to make a button that applies a discount to a cart? I'm looking to fulfill these requirements:

 

  1. The button only works for the customer that is signed in. It shouldn't work to copy-and-paste a link and give it to someone else.
  2. The discount is for a specific product. If that product isn't in the cart, it gets added.

 

0 Likes
Highlighted
Shopify Expert
808 67 188

I think I have a solution for you:

 

1) You can display the button only to logged in customers with this liquid code:

{% if customer %}
  <!-- button here -->
{% endif %}

Reference: Customer liquid object

 

I don't think you can use the API to apply a discount on button click, but you could use automatic discount to auto apply a discount to a product. Then you wouldn't need to use the button.

 

If you want to use the button, you could use a discount link and send it to cart page, example: /discount/code?redirect=/cart

 

 

I don't know of a way to prevent copy and pasting, there's not an option in Discounts section to apply to a specific person. But you could limit it to logged in customers at least with the code above.

 

2) You can use the Shopify ajax API to programmatically add the item to cart when the button is clicked: https://shopify.dev/tutorials/develop-theme-getting-started-using-ajax-api

Founder of Speed Boostr (Shopify optimization and dev agency).
Creator of Theme Scientist (A/B testing app).
Creator of Order Automator (app that auto tags + fulfills orders, send emails).
Creator of Tip Jar (add a tip button to your store).
Creator of Shopify Analyzer (free performance analysis tool).
My team also builds custom apps and automated solutions.
Enjoy the adventure!
0 Likes