1

I'm trying to show custom fields on the cart but I'm missing something (and new to Commerce, so bear with me). When I output {% for lineItem in cart.lineItems %} within my {% for item in cart.lineItems %} it outputs all the products again within the same line. How can I get the custom field to only show up on the line that is being output?

{% set lineItemHasErrors = false %}
{% for item in cart.lineItems %}
    {% if item.hasErrors() %}
        {# if the line item has errors lets record it so we can hide totals later (since they wont make sense) #}
        {% set lineItemHasErrors = true %}
    {% endif %}
    <tr class="item" {% if item.hasErrors() %}style="background-color:rgba(255, 0, 0, .1);"{% endif %}>
        <td>
            {% for lineItem in cart.lineItems %} 

            {% if lineItem.purchasable.product.type.hasVariants %}
              {% switch lineItem.purchasable.product.type %}
                {% case "wallpaper" %}
                    {#  Only output the varHeight if the product type is wallpaper #}
                    <strong>{{ lineItem.description }}</strong><br>
                    Height: {{ lineItem.purchasable.varHeight }}
                {% default %}
                    {{ lineItem.description }}
              {% endswitch %}

            {% else %}
                {#  Non variant purchasable  #}
                <strong>{{ lineItem.description }}</strong>

            {% endif %}

            {% endfor %}

            {% if item.options.material is defined %}
            <div class="select">
              <select name="lineItems[{{ item.id }}][options][material]">
                <option value="no"
                        {% if item.options.material == 'PeelNStick' %}selected{% endif %}>
                    Peel N Stick
                </option>
                <option value="yes"
                        {% if item.options.material == 'PrePasted' %}selected{% endif %}>
                   Pre Pasted
                </option>
              <div class="select__arrow"></div>
            </div>
            {% endif %}

            <br>
            <label>
            <input type="checkbox"
                   name="lineItems[{{ item.id }}][remove]"
                   value="1"> Remove Item
            </label>
        </td>
        <td>


        <td class="text-right">
            {% if not lineItemHasErrors %}
                {% if item.onSale %}
                    <s>Price: {{ item.price|commerceCurrency(cart.currency) }}</s>
                    <br>
                    Sale Off: {{ item.saleAmount|commerceCurrency(cart.currency) }}
                    <br>
                    Sale Price {{ item.salePrice|commerceCurrency(cart.currency) }}
                    <br>
                    Sale Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
                    <br>
                {% else %}
                    Price: {{ item.price|commerceCurrency(cart.currency) }}
                    <br>
                    Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
                    <br>
                {% endif %}
            {% endif %}

            {% for sale in item.purchasable.sales %}
                {% if loop.first %}
                    Sales Applied:
                {% endif %}
                {{ sale.name }}<br>
            {% endfor %}
        </td>
    </tr>
    {% if not lineItemHasErrors %}
        {% for adjustment in item.adjustments %}
            <tr class="adjustment">
                <td></td>
                <td>
                    <strong>{{ adjustment.type }} {{ adjustment.name }}</strong><br>({{ adjustment.description }}
                    )
                </td>
                <td class="text-right">{{ adjustment.amount|commerceCurrency(cart.currency) }}</td>
            </tr>
        {% endfor %}
    {% endif %}


 {% endfor %}

enter image description here

2

{% for item in cart.lineItems %} and {% for lineItem in cart.lineItems %} are the same loop - you're just assigning a different name to the iterable item! Just remove the inner loop and rename the inner variables to item instead of lineItem. Maybe I'm missing what you're trying to achieve?

1
  • That worked. I wasn't sure if they were separate elements. Thanks for clarifying. Still wrapping my head around Commerce.
    – glider
    Mar 6 '20 at 22:07

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.