Skip to main content

How to use conditional logic in feeds

Some products have a sale price and some do not. Some have a GTIN. Some are in stock. A feed that pretends they are all the same either emits empty tags - which marketplaces treat as errors - or forces you to maintain a separate feed per case.

{% if %}{% else %} handles it in one template: output this when the data is there, output something sensible when it is not.

How conditional logic works

In Advanced Product Feeds, the {% if %}{% else %} construct can be used to define fallback values or alternative outputs based on conditions.

Basic syntax

{% if condition %}
Output if condition is met
{% else %}
Output if condition is NOT met
{% endif %}

Examples

1. Display discount only for promotional products
<g:price><![CDATA[{{ product.price | price }}]]> EUR</g:price>
{% if product.price > product.final_price %}
<g:sale_price><![CDATA[{{ product.final_price | price }}]]> EUR</g:sale_price>
{% endif %}

If the product has a discounted price (final_price) that's lower than the regular price (price), it will add the g:sale_price. If there's no discount, this field will not appear.

2. Add promotion expiry date
{% if product.special_price %}
<g:sale_price>{{ product.special_price }} EUR</g:sale_price>
<g:sale_price_effective_date>{{ product.special_from_date | dateFormat: 'c' }}/{{ product.special_to_date | dateFormat: 'c' }}</g:sale_price_effective_date>
{% endif %}

This allows you to specify the exact promotion period, which is recognized by Google Shopping.

3. Add "SALE" label for discounted products
{% if product.price > product.final_price %}
<g:custom_label_4>SALE</g:custom_label_4>
{% endif %}

If the product price has decreased, it adds the "SALE" label. This helps marketplaces filter promotional products.

4. Hide special price if it is empty
{% if product.special_price and product.special_price > 0 %}
<g:sale_price><![CDATA[{{ product.special_price }} USD]]></g:sale_price>
{% endif %}

This ensures that special_price is not only set but also greater than zero.

5. Hide shipping weight if it is empty
{% if product.weight and product.weight > 0 %}
<g:shipping_weight><![CDATA[{{ product.weight }} kg]]></g:shipping_weight>
{% endif %}

This ensures that weight is not only set but also greater than zero.

These conditions prevent empty or zero values from being included in the feed.