Automatic discount based on the status of your professional baseball team?

Tourist
17 0 0

Can anyone tell me how I can automatically give my shopify customers 50% off their shopping cart if all of these rules are satisfied:

 

- They have a baseball and a baseball glove in the shopping cart

- They have made a past purchase from my store in which the receipt shows a city

- The city in the second point above has a professional baseball team and they are currently leading the opposing team by 5 points.

 

So for example, if I made a purchase from Toronto in the past.  And right now, the Toronto Blue Jays are leading the New York Yankees by 10 to 2.  Then I'm going to put a baseball, baseball glove, and some other items in my shopping cart and checkout and get 50% off.

 

Is there a plugin I can use to do this?  If not, which API hook do I use to override the purchase price?

0 Likes
Shopify Partner
1677 42 140

 

Afaik for shopify apps there is no baseball stats centric app, or even a general data consuming app that would let you pull some random source in.

Your first thing to solve is getting the baseball data either via manual entry, api, or parsed feed.

AND here it seems like you want to also get that data during a live event???

 

You'll have to do some research https://www.google.com/search?q=baseball+stats+public+api

To do this applessly it's important that the data be publicly accessible with permissions in some fashion and in a data consumable format.

Keep in mind unless the data source can return the calculated point lead you will probably have to make a few requests.

 

For example the MLB have a standings page JSON but it's meant for non-commercial usage without written permision. If you can get commercial access aparently there are tonnes of libraries in different languages for it.

 


@agentjuly wrote:

Is there a plugin I can use to do this?  If not, which API hook do I use to override the purchase price?


The products or discount api to modify things. 

If your a beginner dev I don't recommend this being your first programming project as your trying to modify your income on the fly based on a live event.

Or if your on shopify plus using checkout scripts(though you'll still need the data for the logic to use) maybe even shopify flow to automate this?

 

 

Without the requirement of using live data, the options become easier:

  • Get static data of relevant baseball stats
    • Store somewhere either in metafields, product tags, or a in the theme itself as a liquid string that you parse
      • turn strings into array using string filter split trick {% assign points = 'one,two,three' | split %}
    • Store a list of city names 
    • optionally if your going to use javascript store data in a remote source , or a json object in your theme
  • Check logged in customers
    • order history and match city names
    • check team stats to products in cart( either via javascript or in /cart template)
  • Programmatically add manual discount to shoppers cart before /checkout starts or when checkout is clicked
    • keep in mind dynamic checkout buttons if your using them will need discount added pre-emptively
  • Optionally put in place process tools to validate order discount before fulfillment
  • Update store data regularly

Another approach is use tools to send a discount coupon AFTER such a purchase has been made though still having the problem of where to get the baseball data.

 

Developers app idea tl;dw - example of need for a live events discount app that consumes remote sources.

Problem Solved? ✔️Accept the solution so you can help others.
Confused? Busy? ? Get me to solve it paull.newton+shopifyforum@gmail.com.
Buy me a coffee? ☕ paypal.me/paulnewton or donate to eff.org
0 Likes
Tourist
17 0 0

Thank you Paul, regarding these points here:

 

Programmatically add manual discount to shoppers cart before /checkout starts or when checkout is clicked

  • keep in mind dynamic checkout buttons if your using them will need discount added pre-emptively

 

Can you send me a link to the specific end point or shopify documentation that has an example of this or shows me how to do it?  Specifically, which "event hook" do I need to detect "onCheckout" event, and which end point do I send my order payload to to modify the purchase price?  Or have I misunderstood the whole process?

 

Really appreciate your insight!

0 Likes
Highlighted
Tourist
17 0 0

oh and also assume i am NOT on shopify plus.  I'm just using the regular shopify...(note , I'm still new to this, so i'm not aware of all the details of which versions of shopify can do what)

0 Likes
Shopify Partner
1677 42 140

Use the discount parameter either in a form or via ajax to add a discount to the users session

For the product page use it on the cart submission

, ex: <form action="/cart/?discount=DISCOUNTCODE{% endif %}" method="post">

On the /cart page itself use an input in the carts form

, ex: <input type="hidden" name="discount" value="DISCOUNTCODE" />

 

 

 

Problem Solved? ✔️Accept the solution so you can help others.
Confused? Busy? ? Get me to solve it paull.newton+shopifyforum@gmail.com.
Buy me a coffee? ☕ paypal.me/paulnewton or donate to eff.org
0 Likes
Tourist
17 0 0

Thanks Paul! With the approach you mentioned, how would I prevent the following scenario:

1. user fills shopping cart and goes to cart page
2. user views html source and copies the discount count to a separate text file on their computer
3. user ends browser session and clears cookies
4. user repeats steps 1 to 3 multiple times.

 

I want to avoid the situation where a user can create as many discount codes as he would like and then distribute those coupon codes to other people who can then use it at any time even if they do not belong to a city that has a team that's leading by 5 points.

0 Likes
Shopify Partner
1677 42 140

Seems to be some misconception or miscommunication here, you need to setup a code in the admin and go through checkout using that code before proceeding.

 


@agentjuly wrote:

I want to avoid the situation where a user can create as many discount codes as he would like and then distribute those coupon codes to other people who can then use it at any time even if they do not belong to a city that has a team that's leading by 5 points.


YAGNI. The platforms generalized discount feature isn't built for that level of worry out of the box without custom applications.

Don't build a business around a discount code scheme that will go bankrupt because the code gets shared and you dont enforce the rules for it.

Build one that wants the code to be shared or just don't use them.

 

Either way, the understanding of the flow is flawed, skip to the end: when customers in the checkout they know the code anyway

So what would be the point of them getting the same code multiple times?

If you trying to generate a unique code per customer per visit your going to need an app, or app proxy anyway.

Either that or a whole lot of skill using liquid templates.

 

As for viewing the frontend source the url parameter approach can't avoid that without an app , or clever discount code generation scheme , or diligence to enforce it on the backend.

 

You could make the discount code retrieved elsewhere either plaintext or somehow obfuscated,

and then also use obfuscated javascript to process it for cart submission.

But when they are in the checkout they see the code, anyway.

 

To sidestep all that you will need a custom app.

 

 

 

 

 

 

 

 

Problem Solved? ✔️Accept the solution so you can help others.
Confused? Busy? ? Get me to solve it paull.newton+shopifyforum@gmail.com.
Buy me a coffee? ☕ paypal.me/paulnewton or donate to eff.org
0 Likes
Tourist
17 0 0

Oh, I see, thanks very much for clarifying.

 

So then if we go the custom app approach, would you be able to point me to the specific :

 

- hook and/or event that will notify me of the customer's "on_checkout" event, which means the person is about to make a payment for all shopping cart items, and hence the hook/event will send the payload of cart details to my app server for pre-processing

 

- api end point where I can submit a payload of order details that will include a purchase amount that has been modified by my app. The purchase amount maybe modified by methods such as my app directly overriding the original order amount with whatever amount i choose, or by generating a discount code on the fly which can only be used for this specific purchase

 

Are these tools/mechanisms available to me as a developer working on a shopify basic account? Or is doing this kind of development only allowed on shopify plus accounts?

 

There's so much technical literature that I'm getting confused by what's available to what types of accounts

0 Likes
Shopify Partner
1677 42 140

 


@agentjuly wrote:

 

There's so much technical literature that I'm getting confused by what's available to what types of accounts


If your a high volume businesses it's a major clue to be on a shopify Plus plan.

 

Development wise it's clue to dial it back until you've been able to get through and experiment with the system itself.

 

What your wanting to do in a rudimentary way is interfacing where the MONEY begins to change hands.

So keep in mind the reason the system seems complicated here is the shopify checkout is heavily sandboxed.

 

https://help.shopify.com/en/api/guides/discounts <- GraphQL

https://help.shopify.com/en/api/reference/discounts/discountcode

https://help.shopify.com/en/api/reference/discounts/pricerule

Note: the discountcode and pricerule REST api's are being deprecated this year okay to dev just not for production.

 

 

Just make the discount exist BEFORE they can even go to /checkout and you add it to the cart per above.

, THEN use the pricerule api to update it to the correct pricing??

Or use some method to trigger an automated discount to apply to the users cart.

Or generate a unique item with adjusted pricing for them.

 

 

Your gonna have a hard time making it work how you've outlined it based on how you think it should work as there are alot more discrete steps to the flow.

 

Join this discussion in case the current methods change:

https://community.shopify.com/c/Shopify-APIs-SDKs/Automatically-apply-discount-to-cart-using-API/td-...

 

 


@agentjuly wrote:

Are these tools/mechanisms available to me as a developer working on a shopify basic account? Or is doing this kind of development only allowed on shopify plus accounts?


Most api's are accessible to all plans. When in the help docs any api with limitations should be noted.

Though on a development store you may have access to apis that would not normally be available to a regular store.

Make sure you're setup as a shopify partner and use a dev store so you not wasting monthly fees on a store just for development.

 

 

Also try finding and installing some of the partner friendly discount apps and to through there setup process.

Or just install apps if you have  paid store and remember to uninstall before trials end as not all apps are partner/devstore friendly and will charge you.

Problem Solved? ✔️Accept the solution so you can help others.
Confused? Busy? ? Get me to solve it paull.newton+shopifyforum@gmail.com.
Buy me a coffee? ☕ paypal.me/paulnewton or donate to eff.org
0 Likes