Can I add discount code to metafield and include in add to cart button?

Solved

Can I add discount code to metafield and include in add to cart button?

IliaZ
Shopify Partner
11 3 1

I have created a new product metafield: product.metafields.my_fields.discount_code

 

In my product-template.liquid, there is this section:

 

<button class="btn cv_addtocart{% if section.settings.enable_payment_button %} btn--secondary{% else %} btn--primary{% endif %}" type="button" name="add" data-add-to-cart {% unless current_variant.available %}disabled="disabled"{% endunless %}>
<span data-add-to-cart-text>
                  {% if current_variant.available %}
                  {{ 'products.product.add_to_cart' | t }}
                  {% else %}
                  {{ 'products.product.sold_out' | t }}
                  {% endif %}
</span>
</button>
 {% if section.settings.enable_payment_button %}
                {{ form | payment_button }}
              {% endif %}
            </div>
          </div>

 

I am trying to pass the value of a metafield for a product into the add to cart button. In this case, I want the discount code which is entered into the product metafield to be included.

 

So am hoping to do something like this:

 

 {% if section.settings.enable_payment_button %}
 {{ form | payment_button | discountCode = product.metafields.my_fields.discount_code }}
 {% endif %}

 

Basically, I am trying to automatically apply a normal discount to a product at checkout. I have seen the various shareable links that append the discount code to the url, but this does not suit my needs. Instead, I would like to retrieve the discount code (DISCOUNT20) from a metafield for that product on the checkout page and apply the coupon code there.

 

Any thoughts? I have been stuck on this for a long time 😞😞

Accepted Solution (1)
IliaZ
Shopify Partner
11 3 1

This is an accepted solution.

UPDATE

 

My above reply works locally but does not work when published to live store.

 

After some more digging, I realised that I needed to update my code

 

{%- for item in cart.items -%} 
   {%- if item.product.metafields.my_fields.discount_code.value != blank -%} 
      <input class="js-form-discount" type="hidden" name="discount" value="{{item.product.metafields.my_fields.discount_code.value}}" >
   	{%- endif -%} 
{%- endfor -%}

 

The for and if loop access the item. Without this your shopify store won't be able to pass the metafield value to checkout/cart.

View solution in original post

Replies 3 (3)

IliaZ
Shopify Partner
11 3 1

So I have figured it out.

 

So i located my cart template liquid file and searched for a form

<form action="/cart" method="post" novalidate class="cart1" id="cartForm">

Directly below this line I inserted this

<input class="js-form-discount" type="hidden" name="discount" value="{{product.metafields.my_fields.discount_code}}" >

In my case, value = an interpolated value being retrieved from a product metafield for the product in the cart.

 

FYI - you need to create a metafield in settings, then create a discount code, copy the discount code, go to your product that you want the discount code to apply to, access the product and scroll down to metafields, paste the discount code in the product's discount(if you named your metafield "discount", you might have named it something else) metafield.

 

Hence, as the form will be submitted with a name="discount" and value="discount_code", the checkout page will evaluate to true for discount and use the appropriate values.

 

For such a simple fix, I am surprised it was so hard to figure out. Hope this helps you!

___________________________________

** Some extra reading if you want: https://wearetmbr.com/shopify-auto-fill-discount-code-on-checkout-page/ **

IliaZ
Shopify Partner
11 3 1

This is an accepted solution.

UPDATE

 

My above reply works locally but does not work when published to live store.

 

After some more digging, I realised that I needed to update my code

 

{%- for item in cart.items -%} 
   {%- if item.product.metafields.my_fields.discount_code.value != blank -%} 
      <input class="js-form-discount" type="hidden" name="discount" value="{{item.product.metafields.my_fields.discount_code.value}}" >
   	{%- endif -%} 
{%- endfor -%}

 

The for and if loop access the item. Without this your shopify store won't be able to pass the metafield value to checkout/cart.

nmasters
Excursionist
21 1 3

Hi, Seems like just what we are looking for... but we like to show information or something to promote that this product have a automatic discount code that give the user Buy 3 and get 1 or whatever.. today the customers first see the discount after adding them to the basket.. can this do the that = show a text, sign, picture whatever on the collection page and product page under the product ? Thanks jacob