3

This is my first time using Craft Commerce. I have a client which needs to have multiple prices for a product. Customers are in one of three price level groups. Each price level group has a price unique to them as a variant. How do I get only the one variant for the appropriate price?

Here is what I tried, but returned all the variants for each price level instead of just one.

                <select name="purchasableId">
                    {% set purchasable = craft.commerce.products %}
                    {% for purchasable in product.variants %}
                        <option 
                            {% if purchasable.stock <= 0 and purchasable.unlimitedStock == false %}disabled {% endif %} 
                            value="{{ purchasable.purchasableId }}">

                                Product Number: {{ product.stockDescription1 }} | 
                                Price: 

                                {% if currentUser.isInGroup( 'priceLevelOne' ) %}

                                    {% set variant = craft.commerce.variants %}
                                    {% for variant in product.variants %}
                                        {% if variant.priceLevel1 == 1 %}
                                            Product Number: {{ product.stockDescription1 }} |
                                            Price:
                                            {{ variant.price|currency(cart.currency) }}
                                        {% endif %}
                                    {% endfor %}

                                {% elseif currentUser.isInGroup( 'priceLevelTwo' ) %}

                                    {% set variant = craft.commerce.variants %}
                                    {% for variant in product.variants %}
                                        {% if variant.priceLevel1 == 2 %}
                                            Product Number: {{ product.stockDescription1 }} |
                                            Price:
                                            {{ variant.price|currency(cart.currency) }}
                                        {% endif %}
                                    {% endfor %}


                                {% elseif currentUser.isInGroup( 'priceLevelThree' ) %}

                                    {% set variant = craft.commerce.variants %}
                                    {% for variant in product.variants %}
                                        {% if variant.priceLevel1 == 3 %}
                                            Product Number: {{ product.stockDescription1 }} |
                                            Price:
                                            {{ variant.price|currency(cart.currency) }}
                                        {% endif %}
                                    {% endfor %}

                                {% endif %}
                        </option>
                    {% endfor %}
                </select>
3
  • You can add custom fields to variants as well, which is what I think you mean by "item." Depending on how you have your products structured you could also create a variant for each custom group which would have the price associated with that and therefore would not effect the checkout flow or having to alter prices of items.
    – mcclaskiem
    Commented Apr 25, 2016 at 14:14
  • Yes, I have those custom fields in my variants, but I need to limit which variants are displayed. Each product will have 5 variants, but I only need one variant depending on what user group. Right now all five variants are returned on the product page. Commented Apr 29, 2016 at 13:42
  • What is the field type on the variant? If you are using categories, you can do a simple relatedTo query to get the one you want, I can provide an example below!
    – mcclaskiem
    Commented Apr 29, 2016 at 13:49

1 Answer 1

3

User groups are not a FieldType so you can create a category that matches the title of the userGroup or put the actual category as a field on the User and the Variant. Then in your template do something like this:

{% set userGroup = currentUser.userGroup.first() %}

{% set variants = craft.commerce.variants.product(product).relatedTo(userGroup) %}

This would probably be the best way since you aren't reliant on string matching a title to find the correct variants.

2
  • I am new so.... {% if currentUser.isInGroup( 'priceLevelOne' ) %} {% set userGroup = currentUser.priceLevel.first() %} {% set variant = craft.commerce.variants.product(parts).relatedTo(userGroup) %} {% for variant in product.variants %} Product Number: {{ variant.sku }} | Price: {{ variant.price|currency(cart.currency) }} {% endfor %} The category group handle is "userGroup". The category title is "Price Level 1" and the slug is "price-level-1". The field containing the category is "priceLevel". What do I have wrong. Commented Apr 29, 2016 at 18:33
  • get rid of the conditional about the isInGroup and simply add a category to a user based on their user group. then all you will need is what I provided and sub in your fieldHandle. Also if you are serving this on a product page you should product instead of parts since that is the variable that commerce passes in by default
    – mcclaskiem
    Commented Apr 29, 2016 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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