Code snippets for product comparisons

The PHP template engine TWIG is used for the code sections. An overview of the most important commands can be found here: TWIG overview.

The variables used in this section can be implemented in all product comparisons or social shopping templates, but it depends on the recipient whether the data can be processed there.

Code snippets to extend the template

Prices

In the product comparison template just created, a fixed price is used for the price transmission. That is the part:

{% set price = product.calculatedPrice %} 
{%- if product.calculatedPrices.count > 0 -%} 
   {% set price = product.calculatedPrices.last %} 
{%- endif -%}

If you replace this code with the following one, you can also export the list prices, for example. If there is no list price, only your item price will be exported as before.

    {%- if product.calculatedPrice.listPrice -%}
        <g:sale_price>
            {%- if product.calculatedPrices is not empty -%}
                {{ product.calculatedPrices.first.unitPrice|number_format(2, '.', '') }}
            {%- else -%}
                {{ product.calculatedPrice.unitPrice|number_format(2, '.', '') }}
            {%- endif -%}
            {{- context.currency.isoCode -}}
        </g:sale_price>
        <g:price>
            {{- product.calculatedPrice.listPrice.price|number_format(2, '.', '') -}}
            {{- context.currency.isoCode -}}
        </g:price>
    {%- else -%}
        <g:price>
            {%- if product.calculatedPrices is not empty -%}
                {{ product.calculatedPrices.first.unitPrice|number_format(2, '.', '') }}
            {%- else -%}
                {{ product.calculatedPrice.unitPrice|number_format(2, '.', '') }}
            {%- endif -%}
            {{- context.currency.isoCode -}}
        </g:price>
    {%- endif -%}

If you need more than one image link, you can add this code to your product rows:

{%- if product.media|length > 1 -%}
   {%- for media in product.media -%}
      <additional_image_link>
          {{- media.media.url -}}
      </additional_image_link>
   {%- endfor -%}
{%- endif -%}

This exports the links with the additional_image_link tag in the file for all additional images.

Shipping Costs

This part of the product comparison template transmits information about shipping costs. This is not required in all files for Google Shopping, you can remove these lines if necessary.

    <g:shipping>
        <g:country>EN</g:country>
        <g:service>Standard</g:service>
        {% if product.shippingFree  %}
           <g:price>0.00 {{ context.currency.isoCode }}</g:price>
        {% else %}
           <g:price>4.95 {{ context.currency.isoCode }}{# change your default delivery costs #}</g:price>
        {% endif %}
    </g:shipping>

Add selling unit and basic unit of the product

If Google wants to have the data for the basic price calculation, you need the contents of the sales unit and basic unit of the product in your feed. You can use these code snippets to add the required information:
Sales unit

<g:unit_pricing_measure>
 {% if product.unit.shortCode is defined and product.unit.shortCode is not null %}
   {{ product.purchaseUnit }} {{ product.unit.shortCode }}
 {% endif %}
</g:unit_pricing_measure>

The variable purchaseUnit stands for the value of the field sales unit, unit.shortcode is the short form of the stored unit of measurement, e.g. "KG".
Basic unit

<g:unit_pricing_measure>
 {% if product.unit.shortCode is defined and product.unit.shortCode is not null %}   
{{ product.referenceUnit}} {{ product.unit.shortCode }}  
{% endif %}
</g:unit_pricing_base_measure>


If this doesn't work for you, try it out with product.unit.translated.shortCode instead of product.unit.shortCode.

Here are both code snippets again so that you can copy and paste them directly:

Sales unit

<g:unit_pricing_base_measure>
 {% if product.unit.translated.shortCode is defined and product.unit.translated.shortCode is not null %}
   {{product.purchaseUnit }} {{ product.unit.translated.shortCode }}
 {% endif %}
</g:unit_pricing_base_measure>

Base unit

<g:unit_pricing_base_measure>
 {% if product.unit.shortCode is defined and product.unit.shortCode is not null %}
   {{ product.referenceUnit }} {{ product.unit.shortCode }}
 {% endif %}
</g:unit_pricing_base_measure>

Add variant properties

When exporting variant products, you want to store the properties of the variants in the file as well. To do this, the properties of the product must be run through in a loop. The basic approach is as follows:

{% for option in product.options %}
    {{ option.name }}
{% endfor %}

Of course, you can always extend the code via TWIG. 

Was this article helpful?