Coding query - why will this not JUST WORK!!!!!!! It's driving me mad!

Tourist
30 0 3

I've some products listed that when clicked should provide a link to an affiliate as opposed to the add to cart button. To create the affiliate option I used Metafield Solid to add the affiliate link data:

Key:url
   Namespace: affiliate

In the themes (Minimal) I made the following changes:

<!-- Affiliate metafield contains? {{ product.metafields.affiliate.url }} -->

      <div class="purchase-section{% if product.variants.size > 1 %} multiple{% endif %}">
        <div class="purchase">

          {% if product.metafields.affiliate.url %}

         <a target="_blank" href="{{ product.metafields.affiliate.url }}" />Buy from Affiliate</a>

          {% else %}

<!-- this would be your original button -->

         <input type="submit" id="add-to-cart" class="btn" name="add" value="{{ 'products.general.add_html' | t | escape }}" />

         {% endif %}

When I preview the page it still shows the Add to cart button, yet when I view the source the comment containing product.metafields.affiliates.url clearly shows the correct affiliate URL. Is there something obvious I'm missing?

I had sought some advice from a code person but they've left me in the lurch and suddenly dropped off the face of the earth!

Any help much appreciated, thank you.

0 Likes
Shopify Expert
57 0 17

Hi Craig,

 

It is really strange as the condition looks correct to me. The only obvious explanation will be that the {{ product.metafields.affiliate.url }} is empty. By looking at several products on your store from your signature, I can see that for the following comment 

<!-- Affiliate metafield contains? {{ product.metafields.affiliate.url }} -->

the produced result is

<!-- What does product metafields contain?  -->

(by inspecting the source via the Inspector) which applies that the meta field is empty and that is why the default Add to cart button is rendered. 

Another way you can try is using the {% unless %} statement.

{% unless product.metafields.affiliate.url == "" or product.metafields.affiliate.url == empty %}

         <a target="_blank" href="{{ product.metafields.affiliate.url }}" />Buy from Affiliate</a>

{% endunless%}

If you could share a link to a product on which the affiliate url is being set in the comment and the Add To Cart buttons is rendered will be very helpful.

 

Hope that helps.

 

Beautiful and responsive Shopify themes crafted with care at www.kabukithemes.com.
0 Likes
Tourist
30 0 3
Thanks so much for your reply. I'll list a test product with an affiliate link and post the link to it. Hopefully that will highlight the problem. Thanks again. Speak soon Craig :-)
0 Likes
Tourist
30 0 3

Hello again

Many thanks once again for your advice. In the process of listing a test product we've managed to solve the problem. I was leaving the ahref in the metafield! All sorted now.

Best wishes

Craig

0 Likes
New Member
1 0 0

Hello Craig,

I am also trying to set up products that can be linked to other websites, but I am really new to all of this. I have added the custom fields app and shopifyFD app, so I can create metafields, but do I still need to add liquid code or will just adding a metafield on a product make the link?

Any ideas?

Thank you

0 Likes
Highlighted
Shopify Expert
57 0 17

Hi Matthew,

You will need to use these metafields in your liquid code in order to display the content set as meta fields. Just like Craig's example, you can create metafields with affiliate namespace and then reference it in the liquid files just as the example above:

<a target="_blank" href="{{ product.metafields.affiliate.url }}" />Buy from Affiliate</a>

where the actual affiliate link will be stored in the url meta field with a namespace "affiliate".

Hope that helps.

 

Beautiful and responsive Shopify themes crafted with care at www.kabukithemes.com.
0 Likes