-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcollection.google-feed-update.liquid
101 lines (88 loc) · 4.72 KB
/
collection.google-feed-update.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{% layout none %}<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
{% comment %}
Google Shopping / Merchant Center + Shopify Product Update Feed by Alex Czartoryski
https://business.czarto.com/2020/10/14/enhance-shopify-google-shopping/
This version: Aug 30, 2022
The latest version of this script available here:
https://github.com/Czarto/ShopifyScripts/blob/master/Templates/collection.google-feed-update.liquid
TODO: Test & handle products without variants
TODO: Test & hanlde products without color
TODO: Specify Sizes to never exclude. eg: Women's 7,8
{% endcomment %}
{% comment %} Settings {% endcomment %}
{%- assign exclude_unavailable_variants = true -%}
{%- assign exclude_variant_colors_with_limited_availability = false -%}
{%- assign ignore_x_smallest_sizes = 1 -%}
{%- assign ignore_x_largest_sizes = 1 -%}
{%- assign minimum_percentage_availability = 50 -%}
{%- assign filter_variantImages_byColor = false -%}
{%- comment -%}
TODO: Move this into a snippet and use capture to assign the variable
{%- endcomment -%}}
{%- case shop.currency -%}
{%- when 'USD' -%}{%- assign CountryCode = 'US' -%}
{%- when 'CAD' -%}{%- assign CountryCode = 'CA' -%}
{%- when 'GBP' -%}{%- assign CountryCode = 'GB' -%}
{%- when 'AUD' -%}{%- assign CountryCode = 'AU' -%}
{%- else -%}{%- assign CountryCode = 'US' -%}
{%- endcase -%}
<channel>
<title>{{ shop.name }} {{ collection.title | strip_html | strip_newlines | replace: '&', '&' }}</title>
<link>{{ shop.url }}</link>
<description>{{ collection.description | strip_html | strip_newlines | replace: '&', '&' }}</description>
{%- paginate collection.products by 1000 -%}
{%- for product in collection.products -%}
{%- comment -%} Get color option {%- endcomment -%}
{%- for option in product.options -%}
{%- if option == 'Color' or option == 'Colour' -%}{% capture option_color %}option{{ forloop.index }}{% endcapture %}{%- endif -%}
{%- endfor -%}
{%- comment -%} Make a list of Colors to exclude {%- endcomment -%}
{%- assign colors_to_exclude = "" -%}
{%- if exclude_variant_colors_with_limited_availability -%}
{%- for color in product.options_by_name['Color'].values -%}
{%- assign variants = product.variants | where: option_color, color -%}
{%- assign variants_to_process_count = variants.size | minus:ignore_x_smallest_sizes | minus:ignore_x_largest_sizes -%}
{%- assign available_count = 0 -%}
{%- assign total_processed_count = 0 -%}
{%- for variant in variants offset:ignore_x_smallest_sizes limit:variants_to_process_count -%}
{%- assign total_processed_count = total_processed_count | plus:1 -%}
{%- if variant.available -%}{%- assign available_count = available_count | plus:1 -%}{%- endif -%}
{%- endfor -%}
{%- if total_processed_count == 0 -%}
{%- continue -%}
{%- endif -%}
{%- assign percentage_availability = available_count | times: 100.0 | divided_by: total_processed_count | round -%}
{%- if percentage_availability < minimum_percentage_availability -%}
{% capture colors_to_exclude %}{{colors_to_exclude}}#{{ color }}{%endcapture%}
{%- endif -%}
{%- endfor -%}
{%- assign colors_to_exclude = colors_to_exclude | split: "#" -%}
{%- endif -%}
{%- for variant in product.variants -%}
<item>
<g:item_group_id>shopify_{{ CountryCode }}_{{ product.id }}</g:item_group_id>
<g:id>shopify_{{ CountryCode }}_{{ product.id }}_{{ variant.id }}</g:id>
<g:mpn>{{ variant.sku }}</g:mpn>
<g:barcode>{{ variant.barcode }}</g:barcode>
{%- comment -%} Additional Images by Color {%- endcomment -%}
{%- assign additional_images = product.images -%}
{%- for option in product.options -%}
{%- if option == 'Color' or option == 'Colour' -%}{% capture variant_color %}{{ variant.options[forloop.index0] }}{% endcapture %}{%- endif -%}
{%- endfor -%}
{% if filter_variantImages_byColor %}{% assign additional_images = product.images | where: "alt", variant_color | sort: 'attached_to_variant' | reverse%}{% endif %}
{% if additional_images.size > 1 %}{%- for image in additional_images offset:1 limit:10 -%}
<g:additional_image_link>https:{{ image.src | product_img_url: 'master' }}</g:additional_image_link>
{% endfor %}{% endif %}
{%- comment -%} Exclude Out of Stock Variants {%- endcomment -%}
{% if exclude_unavailable_variants and variant.available == false %}
<g:pause>ads</g:pause>
{% elsif exclude_variant_colors_with_limited_availability and colors_to_exclude contains variant_color %}
<g:pause>ads</g:pause>
{% endif %}
</item>
{% endfor %}
{% endfor %}
{% endpaginate %}
</channel>
</rss>