Hello Daniel, Ben here - your Shopify Guru!
You can set the product on sale in your Shopify admin using the compare price. You cannot edit the code to automatically apply a discount because this would be a security issue. If this was possible anyone could reproduce your code and apply discounts automatically to your products, which would not be an ideal situation.
If you set the product on sale in your Shopify admin it will show the sale price in the buy button as well.
I hope this helps! Feel free to reach out if you have any further questions :)
Ben C - Shopify Guru
The issue is that we use coupon codes to handle campaign attribution. We have hundreds of campaigns running at any given time running with different prices so this solution wouldn't work. We use an approach similar to the permalinks to pre-load the cart method for our landing pages currently. I'm looking for something like this but the Buy Button so that we can stop using our custom Buy Button.
Below is the custom solution I wrote to handle our needs. It works just the way we want but it isn't a scalable solution right now. I'm trying to avoid writing our own button generator and I was hoping to use the Buy Button provided by Shopify.
We would also like to be able to put the discount code in an URL, so that the discount is automatically applied to the cart. It doesn't necessarily need to be in a buy button URL, we would also want to use that to direct the users directly to the store's homepage and have that discount applied to whatever they end up purchasing, so I've created a separate thread on the forum.
There are a couple solutions you may want to look at.
Another thread has documented the steps a developer needs to take to modify a theme to auto apply the discount the way you have described. Ultimately we ended up doing this after using an app for a while. We needed a little more flexibility around the way this was displayed on the cart page. What is still an issue is that the discounted total is not available at the cart page because the discount isn't applied until checkout. As a work around for now, we simply have a message explaining this to customers.