Skip to main content

How to use conditional logic in feeds

In eCommerce, ensuring that product feeds contain accurate, structured, and complete data is crucial for successful product listings on marketplaces like Google Shopping, Facebook, Pinterest, and other comparison shopping engines. The {% if ... %}{% else %} logic in Advanced Product Feeds helps dynamically control which data is included in the feed, preventing errors and optimizing product visibility.

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.