Can Twig display custom product options in a FoxyCart template?

mwkdesignmwkdesign Member
in Help edited March 2013
Question about the Twig language and accessing item options: I have looked the forum, at the Twig site, and in the sample templates, but cannot find the answer to this. Apologies if I'm not asking right or looking in the right place, as I'm a newbie at Twig.

So-- my products have a few custom fields, including "Shirt Size" (And my client insists it must be the two words, capitalized.) How would I access this value in Twig? I am thinking something like this:
  item.options['Shirt_Size']    -- does not work
  item.options.Shirt_Size       -- does not work
  item.Shirt_Size               -- does not work
  item.shirt_size               -- does not work

...But nothing seems to work.

However, this does work:
{% for option in item.options %}
  {% if option.name == 'Shirt Size' %}
    <!-- some html output for Shirt Size -->
  {% endif %}
{% endfor %}

But looping through everything just seems way over complicated. Is there some way to just have Twig output a specific option? Provided I always know this option will exist on every product in my store?
Comments
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    You could just do this:
    {% for option in item.options %}
      {% if option.name == 'Shirt_Size' %}
        Shirt Size: {{ option.value }}
      {% endif %}
    {% endfor %}
    
  • Is there a way of getting that value without setting up a for loop?
  • @sparkweb , thanks for the code! That works, and it appears to be the best way to do this.

    After some tinkering, it seems that, technically one could just do this:
    {{ item.options[0]['name'] }}
    {{ item.options[0]['value'] }}
    {{ item.options[0]['class'] }}
    

    However, it is impossible to know whether item.options[0] will always be "Shirt Size." The order of elements in the item.options array is based on the fields present when the item was added to the cart. So, item.options[0] will sometimes be "Shirt Color", other times, "Shirt Brand" or whatever.

    When building a Foxycart template using Twig, if you want to access a specific product option, do a loop as suggested above.
    {% for option in item.options %}
      {% if option.name == 'Shirt_Size' %}
        Shirt Size: {{ option.value }}
      {% endif %}
    {% endfor %}
    
  • RolfRolf Member
    @mwkdesign, I have been using mustache templates for a while and I like it because they are so dumb you can't put any logic in it (except for loops), and in view models I create the methods needed for getting the things needed in the template.

    I haven't touched twig yet, though soon have to work on foxycart 1.1 template, so I will be more familiar with it. I can see why FoxyCart has decided for twig, because it's very powerful as a template engine, though the logic you can use inside templates can be confusing; it can't do stuff you would expect, or before you know it, the template engine is again packed with too many options that it is not a template system anymore (imho).

    Anyway, this is just small talk.

    You could perhaps create a JS function as a helper to get the options you need (via get/set) and inject those in your template. So then you can do get('shirt_color') or whatever and it doesn't matter where it is in the array of options.

    Rolf
  • @Rolf: thanks for the tips. I will check out mustache templates. Definitely agree that there are some non-intuitive aspects of Twig, but, overall it is an amazing addition to Foxycart's capabilities. Still getting the hang of working with Twig. I have tried using JS functions to do this. For some reason I cannot get any document.write() functions to actually work when attempting to write something inside the cart table. Although Foxycart tells me that it should work, I have had no luck as of yet. I am still experimenting though, and that issue is entirely for another thread. So I will leave it at that! :) Thanks for the tips, I appreciate it!
  • RolfRolf Member
    @ mwkdesign, mustache can't be used with FoxyCart though. You shouldn't use document.write JS for messing with the table. Are you familiar with jQuery? you can do easy DOM editing and insert stuff. Personally I use MooTools because I'm not a fan of jQuery's code style, but if you use FoxyCart you have jQuery included as library, so you better use it. You can create a killer template with twig and then fine tune it with JS using jQuery/JS
Sign In or Register to comment.