0

I am new to Craft Commerce and trying to get a handle on how to display variants on the Product Page. I want to display only the variants which have a specific SKU. I have the following code. (I included the all the code as my problem could be any where.)

{% extends 'commerce/_layouts/main' %}
{% block main %}

{% requireLogin %}

{# -- LISTS PRODUCTS-- #}
{% for product in craft.commerce.products.order('title asc').limit(5).find() %}

{# --PRODUCT ROW----------------------------------------------------------------------------------------------------- #}
<div class="row product">

    {# --PRODUCT IMAGE-------------------------------------------- #}
    <div class="two columns">
        {# {% include "commerce/_images/product" with { class: 'u-max-full-width' } %} #}

        {% for image in product.productImage %}
            <img class="u-max-full-width" src="{{ image.url }}" height="{{ image.height }}" width="{{ image.width }}" alt="{{ entry.title }}">
        {% endfor %}

    </div>
    {# ----------------------------------------end PRODUCT IMAGE-- #}


    {# --PRODUCT BODY------------------------------------------------------------------------ #}
    <div class="ten columns">

        <h5>{% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}</h5>

        {% if product.variants|length %}

            {# --PRODUCT INPUT FORM (Add to Cart)------------------------- #}
            <form method="POST">
                <input type="hidden" name="action" value="commerce/cart/updateCart">
                <input type="hidden" name="redirect" value="commerce/cart">
                <input type="hidden" name="qty" value="1">
                {{ getCsrfInput() }}

                <select name="purchasableId">
                    {% for purchasable in product.variants %}

                        <option 
                            {% if purchasable.stock <= 0 and purchasable.unlimitedStock == false %}disabled {% endif %} 
                            value="{{ purchasable.purchasableId }}">


                                {# --PRICE LEVEL ONE----------------------------------- #}
                                {% if currentUser.isInGroup( 'priceLevelOne' ) %}

                                    {% set variants = craft.commerce.variants({
                                            product: product,
                                            sku: '2654982'
                                    }) %}

                                    {% for variant in product.variants %}
                                            Product Number: {{ variant.sku }} |
                                            Price:  {{ variant.price|currency(cart.currency) }}
                                    {% endfor %}
                                {# -------------------------------end PRICE LEVEL ONE-- #}



                                {# --PRICE LEVEL TWO----------------------------------- #}                                  
                                {% elseif currentUser.isInGroup( 'priceLevelTwo' ) %}

                                    {% set variants = craft.commerce.variants({
                                            product: product,
                                            sku: '2895264'
                                    }) %}

                                    {% for variant in product.variants %}
                                        {% if variant.priceLevel == 'priceLevel2' %}
                                            Product Number: {{ variant.sku }} |
                                            Price:  {{ variant.price|currency(cart.currency) }}
                                        {% endif %}
                                    {% endfor %}



                                {# --PRICE LEVEL THREE--------------------------------- #}  
                                {% elseif currentUser.isInGroup( 'priceLevelThree' ) %}

                                    {% set variants = craft.commerce.variants({
                                            product: product,
                                            sku: '3568659'
                                    }) %}
                                    {% for variant in product.variants %}
                                        {% if variant.priceLevel == 'priceLevel3' %}
                                            Product Number: {{ product.stockDescription1 }} |
                                            Price:  {{ variant.price|currency(cart.currency) }}
                                        {% endif %}
                                    {% endfor %}


                                {% endif %}

                        </option>

                    {% endfor %}
                </select>



                {# -- ADD TO CART BUTTON------------------------------- #}
                {# <select name="options[giftWrapped]">
                    <option value="no">No gift wrap.</option>
                    <option value="yes">Gift wrapped.</option>
                </select> #}

                {% if product.unlimitedStock or (product.unlimitedStock == false and product.totalStock > 0 ) %}
                    <button type="submit">{{ "Add to cart"|t }}</button>
                {% else %}
                    {{ "Out of Stock"|t }}
                {% endif %}
                {# --end ADD TO CART BUTTON---------------------------- #}

            </form>
            {# ---------------------end PRODUCT INPUT FORM (Add to Cart)-- #}

        {% endif %}

    </div>
    {# --------------------------------------------------------------------end PRODUCT BODY-- #}

</div>
{# ------------------------------------------------------------------------------------------------ end PRODUCT ROW-- #}

{% endfor %}

{% endblock %}
3

To fill out the answer above:

Use the plugin skeleton builder here to make a plugin: http://pluginfactory.io

Fill in the fields at the top, and in the choices below choose just 'variables'.

Generate your plugin, and then you'll see in the zip you get that it has all the scaffolding a plugin needs.

In the file variables\PluginnameVariable.php you should change this example function:

public function exampleVariable($optional = null)
{
    return "And away we go to the Twig template...";
}

to e.g.

public function purchasableBySku($sku)
{
    return craft()->commerce_purchasables->getPurchasableBySku($sku);
}

And then in your template you can just

{% set purchasable = craft.pluginName.purchasableBySku(whatever) %}
{% if purchasable|length %}

...to get a specific purchasable by sku. (Note untested but should be pretty much right!)

**** HOWEVER, all that being said, I would probably in practise do it a different way, unless I am mis-understanding the question. It seems like you're already looping through the variants so why not just compare the sku?? *Unless you're trying to display a variant of a different product than the one you're looping over??

{% for variant in product.variants %}
    {% if variant.sku == "whatever %}
        ...

etc. As long as you're talking about variants of the same product, you've already fetched what you need, so why do it again?

| improve this answer | |
  • Jeremy, your practical method of using a conditional is what I originally used. While I thought it return only one variant in the dropdown, it actually returns all the variants for the product but uses the information for the variant which is conditionally selected. When added to the cart it then adds the information for variant two or three depending on which was selected from product dropdown. Any suggestions on what I am doing wrong? – user1663102 May 3 '16 at 5:29
  • {# --PRICE LEVEL ONE----------------------------------- #} {% if currentUser.isInGroup( 'priceLevelOne' ) %} {% for variant in product.variants %} {% if variant.title == "PriceLevel-2" %} Product Number: {{ variant.sku }} | Price: {{ variant.price|currency(cart.currency) }} {% endif %} {% endfor %} {# -------------------------------end PRICE LEVEL ONE-- #} – user1663102 May 3 '16 at 5:30
  • I can't follow that - can you either add an edit to question above or come to commerce slack? – Jeremy Daalder May 3 '16 at 6:02
  • Good Morning Jeremy. It took me a little while, but your help gave me the direction I needed. Was able to figure it out finally. Had to add a conditional to the purchasable as well as the variant. Works perfectly. – user1663102 May 3 '16 at 13:34
  • Good to hear. Might as well accept the answer then :) – Jeremy Daalder May 3 '16 at 13:36
1

In Commerce variants are the same as a purchasable which have a service to get by sku.

getPurchasableBySku($sku) on Commerce_PurchasablesService.php inside the Commerce plug-in.

You could very easily write your own Twig variable to handle this for you and return only the purchasable, or variant you want to show on the product page.

| improve this answer | |
  • I just downloaded the trial version of Commerce. First time using it. Not sure what to do. Can you possibly give me a little more information? Really lost here. – user1663102 May 2 '16 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.