Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhaopengme/483d50c9b67d501f03c14751f354ba22 to your computer and use it in GitHub Desktop.
Save zhaopengme/483d50c9b67d501f03c14751f354ba22 to your computer and use it in GitHub Desktop.
Shopify Google Product Feed xml generator
{% layout none %}<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
{% paginate collections.all.products by 1000 %}
{%- assign useSEOtitle = false -%}
{%- assign useSEOdescription = true -%}
{%- assign CountryCode = 'US' -%}
{%- if shop.currency == 'CAD' -%}{%- assign CountryCode = 'CA' -%}{%- endif -%}
{%- assign Color = "" -%}
{%- assign Size = "" -%}
<channel>
<title>{{ shop.name }} {{ collection.title | strip_html | strip_newlines | replace: '&', '&amp;' }}</title>
<link>{{ shop.url }}</link>
<description>{{ collection.description | strip_html | strip_newlines | replace: '&', '&amp;' }}</description>
{% for product in collections.all.products %}
{%- assign GoogleProductCategory = product.metafields.mm-google-shopping.google_product_category -%}
{%- assign Gender = product.metafields.mm-google-shopping.gender -%}
{%- assign AgeGroup = product.metafields.mm-google-shopping.age_group -%}
<!-- Item #{{ forloop.index }} -->
{% for variant in product.variants limit:1 %}
{%- assign Color = "" -%}
{%- assign Size = "" -%}
{%- for option in product.options -%}
{%- if option == 'Color' -%}{%- capture Color -%}{{ variant.options[forloop.index0] }}{%- endcapture -%}
{%- elsif option == 'Size' -%}{%- capture Size -%}{{ variant.options[forloop.index0] }}{%- endcapture -%}
{%- endif -%}
{%- endfor -%}
{%- capture productTitle -%}{{ product.vendor }} {{ product.title }}{%- endcapture -%}
{%- unless productTitle contains Color -%}{%- capture productTitle -%}{{ productTitle }} {{ Color }}{%- endcapture -%}{%- endunless -%}
{%- if useSEOtitle and product.metafields.global.title_tag.size > 0 -%}{%- assign productTitle = product.metafields.global.title_tag -%}{%- endif -%}
{%- assign productDescription = product.description -%}
{%- if useSEOdescription and product.metafields.global.description_tag.size > 0 -%}{%- assign productDescription = product.metafields.global.description_tag -%}{%- endif -%}
{%- assign OnSale = false -%}
{%- assign Price = variant.price -%}
{%- if variant.compare_at_price > variant.price -%}
{%- assign OnSale = true -%}
{%- assign Price = variant.compare_at_price -%}
{%- assign SalePrice = variant.price -%}
{%- endif -%}
<item>
<title>{{ productTitle | strip_html | replace: '&', '&amp;' }}</title>
<link>{{ shop.url }}{{ variant.url }}</link>
<description>{{ productDescription | strip_html | strip_newlines | replace: '&', '&amp;' }}</description>
<g:google_product_category>{{ GoogleProductCategory | replace: '&', '&amp;' }}</g:google_product_category>
<g:item_group_id>{{ product.id }}</g:item_group_id>
<g:id>{{ product.id }}</g:id>
<g:condition>new</g:condition>
<g:price>{{ Price | money_without_currency | remove: '.' | remove: ',' | divided_by: 100.0 }}</g:price>
{%- if OnSale -%}
<g:sale_price>{{ SalePrice| money_without_currency | remove: '.' | remove: ',' | divided_by: 100.0 }}</g:sale_price>
{%- endif -%}
<g:availability>{% if variant.available %}in stock{% else %}out of stock{% endif %}</g:availability>
<g:image_link>http:{% if variant.image.src %}{{ variant.image.src | product_img_url: 'grande' }}{% else %}{{ product.featured_image.src | product_img_url: 'grande' }}{% endif %}</g:image_link>
<g:gtin>{{ variant.barcode }}</g:gtin>
<g:brand>{{ product.vendor }}</g:brand>
<g:mpn>{{ variant.sku }}</g:mpn>
<g:product_type>{{ product.type }}</g:product_type>
<g:age_group>{{ AgeGroup }}</g:age_group>
{% unless Color == "" %}<g:color>{{ Color | strip_html | strip_newlines | replace: '&', '&amp;' }}</g:color>{% endunless %}
{%- unless Size == "" -%}
<g:size>{{ Size | strip_html | strip_newlines | replace: '&', '&amp;' }}</g:size>
<g:size_system>US</g:size_system>
{%- endunless -%}
<g:gender>{{ Gender }}</g:gender>
<g:custom_label_0>{{ product.metafields.mm-google-shopping.custom_label_0 }}</g:custom_label_0>
<g:custom_label_1>{{ product.metafields.mm-google-shopping.custom_label_1 }}</g:custom_label_1>
<g:custom_label_2>{{ product.metafields.mm-google-shopping.custom_label_2 }}</g:custom_label_2>
<g:custom_label_3>{{ product.metafields.mm-google-shopping.custom_label_3 }}</g:custom_label_3>
<g:custom_label_4>{{ product.metafields.mm-google-shopping.custom_label_4 }}</g:custom_label_4>
<g:shipping_weight>{{ variant.weight | weight_with_unit }}</g:shipping_weight>
</item>
{% endfor %}
{% endfor %}
</channel>
</rss>
{% endpaginate %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment