<p>To fill out the answer above:</p>

<p>Use the plugin skeleton builder here to make a plugin:  <a href="http://pluginfactory.io" rel="nofollow">http://pluginfactory.io</a></p>

<p>Fill in the fields at the top, and in the choices below choose just 'variables'.</p>

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

<p>In the file <code>variables\PluginnameVariable.php</code> you should change this example function:</p>

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

<p>to e.g.</p>

<pre><code>public function purchasableBySku($sku)
{
    return craft()-&gt;commerce_purchasables-&gt;getPurchasableBySku($sku);
}
</code></pre>

<p>And then in your template you can just</p>

<pre><code>{% set purchasable = craft.pluginName.purchasableBySku(whatever) %}
{% if purchasable|length %}
</code></pre>

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

<p>**** 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??</p>

<pre><code>{% for variant in product.variants %}
    {% if variant.sku == "whatever %}
        ...
</code></pre>

<p>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?</p>

CC BY-SA 3.0
2016-05-03T05:29:38.170Z
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
CC BY-SA 3.0
2016-05-03T05:30:03.783Z
{# --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
CC BY-SA 3.0
2016-05-03T06:02:08.893Z
I can't follow that - can you either add an edit to question above or come to commerce slack?
Jeremy Daalder
CC BY-SA 3.0
2016-05-03T13:34:40.110Z
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
CC BY-SA 3.0
2016-05-03T13:36:22.160Z
Good to hear. Might as well accept the answer then :)
Jeremy Daalder
CC BY-SA 3.0
2016-05-02T23:48:34.227Z
2016-05-02T23:48:34.227Z
Jeremy Daalder
<p>In <code>Commerce</code> variants are the same as a <code>purchasable</code> which have a service to get by <code>sku</code>.</p>

<p><code>getPurchasableBySku($sku)</code> on <code>Commerce_PurchasablesService.php</code> inside the Commerce plug-in.</p>

<p>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.</p>

CC BY-SA 3.0
2016-05-02T22:57:02.347Z
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
CC BY-SA 3.0
2016-05-02T14:02:47.880Z
2016-05-02T16:20:15.933Z
2016-05-02T16:20:15.933Z
mcclaskiem
<p>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.)</p>

<pre><code>{% extends 'commerce/_layouts/main' %}
{% block main %}

{% requireLogin %}

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

{# --PRODUCT ROW----------------------------------------------------------------------------------------------------- #}
&lt;div class="row product"&gt;

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

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

    &lt;/div&gt;
    {# ----------------------------------------end PRODUCT IMAGE-- #}


    {# --PRODUCT BODY------------------------------------------------------------------------ #}
    &lt;div class="ten columns"&gt;

        &lt;h5&gt;{% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}&lt;/h5&gt;

        {% if product.variants|length %}

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

                &lt;select name="purchasableId"&gt;
                    {% for purchasable in product.variants %}

                        &lt;option 
                            {% if purchasable.stock &lt;= 0 and purchasable.unlimitedStock == false %}disabled {% endif %} 
                            value="{{ purchasable.purchasableId }}"&gt;


                                {# --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 %}

                        &lt;/option&gt;

                    {% endfor %}
                &lt;/select&gt;



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

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

            &lt;/form&gt;
            {# ---------------------end PRODUCT INPUT FORM (Add to Cart)-- #}

        {% endif %}

    &lt;/div&gt;
    {# --------------------------------------------------------------------end PRODUCT BODY-- #}

&lt;/div&gt;
{# ------------------------------------------------------------------------------------------------ end PRODUCT ROW-- #}

{% endfor %}

{% endblock %}
</code></pre>

<plugin-craftcommerce>
Limit variants In Craft Commerce
# Limit variants In Craft Commerce

(Asked by: username_0 on 2016-05-02)

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----------------------------------------------------------------------------------------------------- #}


    {# --PRODUCT IMAGE-------------------------------------------- #}
    
        {# {% include "commerce/_images/product" with { class: 'u-max-full-width' } %} #}

        {% for image in product.productImage %}
            
        {% endfor %}

    
    {# ----------------------------------------end PRODUCT IMAGE-- #}


    {# --PRODUCT BODY------------------------------------------------------------------------ #}
    

        {% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}

        {% if product.variants|length %}

            {# --PRODUCT INPUT FORM (Add to Cart)------------------------- #}
            
                
                
                
                {{ getCsrfInput() }}

                
                    {% for purchasable in product.variants %}

                        


                                {# --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 %}

                        

                    {% endfor %}
                



                {# -- ADD TO CART BUTTON------------------------------- #}
                {# 
                    No gift wrap.
                    Gift wrapped.
                 #}

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

            
            {# ---------------------end PRODUCT INPUT FORM (Add to Cart)-- #}

        {% endif %}

    
    {# --------------------------------------------------------------------end PRODUCT BODY-- #}


{# ------------------------------------------------------------------------------------------------ end PRODUCT ROW-- #}

{% endfor %}

{% endblock %}

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_1 on 2016-05-02. Score: 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?

> **Comments:**
> **username_0**: 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?  
> **username_0**: {# --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-- #}  
> **username_1**: I can't follow that - can you either add an edit to question above or come to commerce slack?  
> **username_0**: 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.  
> **username_1**: Good to hear. Might as well accept the answer then :)  

---

### Answer by username_2 on 2016-05-02. Score: 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.

> **Comments:**
> **username_0**: 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.  

---

