Dynamicaly generate product add to cart permalink with discount code

Excursionist
59 0 4

Hello,

I am currently trying to configure a permalink on my shopify product pages that adds the current item (Its only shown on product pages) to the cart, with a discount applied.

This is simple enough with basic liquid. However, I am trying to set up the code to dynamically change the discout code, based on the product tags.

Example:

I have a guitar for sale on my site that has the tag "save10" applied to it. I have previously configured a discount code of "save10" in the Shopify backend. A user navigates to that guitar's product page, and is presented with a pop-up window containing a link that would add that specific item to their cart, and take them to the checkout page with the discount code of "save10" already applied.

Solution?:

This is what I've been able to come up with so far...

<a href="http://www.yandasmusic.com/cart/{{ variant.id }}:1{{ url | append: '&discount=' }}{{ url | append: 'product.tags | first' }}">{{ product.tags | first }}</a>

This code produces the following url: 

http://www.yandasmusic.com/cart/:1&discount=product.tags | first

Obviously, this is not working for a number of reasons. 

1. The value of variant.id is not returning a value. I think I need to intialize it somehow first?

2. I have no idea how to use the "product.tags" within the "url | append"

 

I'm sure there has got to be a way to do this! Anyone have any ideas?

Alex

0 Likes
Shopify Expert
196 0 23

Hi Alex,

First where are you using this code in the theme? A product has many variants so you can't just call variant.id unless you are looping though the products variants or have set it somehow.

-Ryan

Stop Stressing About Shopify You’ve Got Better Things To Do => https://320ny.com/shopify/
0 Likes
Excursionist
59 0 4

Hi Ryan,

Currently,the code for this link is placed directly before the add to cart html "form" on my product pages. However, since the link is called up in a jquery "lightbox", it can be moved to any position in the html, without affecting the display.

That makes sense that I would need to call all the product variants, and then specifically select one to use in the url. I'm not quite sure how this is accomplished though.

Would it be possible to provide a select box for a user to choose a variant, ergo changing the url value of the link? If not, I would be totally fine if the code simply just selected the first variant available. I'm using the link as a psudo "add to cart to see price" tool.

0 Likes
Shopify Expert
196 0 23

Hi Alex,

If you just want the first variant and the first tag as the discount code try this:

{% assign = variant_id = product.variants.first.id %}
{% assign = discount_code = product.tags.first %}

<a href="http://www.yandasmusic.com/cart/{{ variant_id }}:1?discount={{ discount_code }}">{{ discount_code}}</a>

Here are two thing to consider:

Variant Id

Here we are just grabbing the first variant id. However, you could use javascript to see what variant is select in the product form and use that in your url. 

Discount Code Reference

Grabbing the first tag might prove troublesome for you. A products tags can be altered by admins and various apps. Relying on the order of these tags is not a great idea. You could preference your tag discount code and grab that or use metafields to store a specific tag. The latter is probably a better option.

Cheers,

Ryan

Stop Stressing About Shopify You’ve Got Better Things To Do => https://320ny.com/shopify/
0 Likes
Excursionist
59 0 4

Thanks for the great information Ryan, I've plugged in your example code snippit, and it's working great!

I agree that selecting the first product tag for referencing the code is not ideal. My only rationality behind selecting the first product tag is that I am using the "Product Discount" app by Bold Apps, which adds any tag I specify to all products in the selected sale. I noticed that the most recently added tag to the product is shown first in the list.

How would I go about preferencing a tag, or storing it in a meta field?

Alex

0 Likes
Highlighted
Shopify Expert
196 0 23

Hi Alex,

If you were to use the metafield approach you need the ability to edit it. To do this you can use a chrome plugin called ShopifyFD. http://shopifyfd.com/ Install this first.

When editing a product in the backend the plugin allows you to add metafields. For example you could add one with a namespace of global, key of discount, and value of save10. The namespace and key can be whatever you want so feel free to change these. 

The code snip would change to this

{% assign = variant_id = product.variants.first.id %}
{% assign = discount_code = product.metafields.global.discount %}

<a href="http://www.yandasmusic.com/cart/{{ variant_id }}:1?discount={{ discount_code }}">{{ discount_code}}</a>

Cheers,

Ryan

Stop Stressing About Shopify You’ve Got Better Things To Do => https://320ny.com/shopify/
0 Likes