Extensions to the Liquid language for Code Snippets

To support a full range of Liquid code edits using Ablestar Bulk Product Editor, we've added some extensions to the core Liquid language and product variable. The main changes are outlined below.

New Product and Variant Fields

The product variable has the following non-standard fields:

  • category_id - the numeric ID of the standard product category
  • image_count - the number of images a product has (read-only)

Each variant in product.variants also has the cost field which is the cost associated with the variant.

Variable Assignment

You can assign values to specific fields of the product object (and other variables):

{% assign product.vendor = "Ablestar" %}
{% assign product.metafields.custom.condition.value = 'As New' %}

You can also assign variant values while you are in a for loop. For example, this code would set all variants to have a price of 10:

{% for variant in product.variants %}
    {% assign variant.price = 10 %}
{% endfor %}

Tag Filters

We have added several tag filters to simplify modifying tags and metafields. You can add a tag with the add_tag filter. This handles duplicates and different capitilizations of tags:

{% assign product.tags = product.tags|add_tag: 'newtags' %}

You can remove a tag with the remove_tag filter. This also adjusts different capitilizations of tags and only runs if the tag exists:

{% assign product.tags = product.tags|remove_tag: 'newtags' %}

You can see if a product contains a tag with the has_tag filter. You can also use the standard contains Liquid command however, this will also match on tags with different capitalizations:

{% assign hasNewTag = product.tags|has_tag: 'newtag' %} 

You can extract the value from a prefixed tag with the tag_suffix filter. In the example below, the value variable would be set to New if the product had the tag Condition_New :

{% assign value = product.tags | tag_suffix: "Condition_" %}

Collection Filters

You can add and remove manual/custom collections by using the handle of the collection:

{% assign product.collections = product.collections|add_collection:"new-c" %}
{% assign product.collections = product.collections|remove_collection:"old-c" %}

You can also use the has_collection filter to quickly check if a product is in a collection:

{% assign has_summer_sale = product.collections|has_collection:"summer-sale" %}

List Filters

You can add and remove items from a list. This is especially useful for metafields that are a list of strings:
{% assign product.metafields.custom.countries_of_origin = product.metafields.custom.countries_of_origin|add_list:"France" %}

{% assign product.metafields.custom.countries_of_origin = product.metafields.custom.countries_of_origin|remove_list:"Germany" %}
To make sure you are not listing the same item twice you can use the contains filter:
{% unless product.metafields.custom.countries_of_origin contains "France" %}
 {% assign product.metafields.custom.countries_of_origin = product.metafields.custom.countries_of_origin|add_list:"France" %}
{% endunless %}

Additional Filters

You can use the parse_json filter to load text into a JSON object. This can be useful if you have a lot of different values you want to check for:

{% capture vendor_price_adjustment_text %}
{
    "vendor1" : 10,
    "vendor2" : 20,
    "vendor3" : 30
}
{% endcapture %}


{% assign vendor_price_adjustments = vendor_price_adjustment_text | parse_json %}
{% for vendor_adjustment in vendor_price_adjustments %}
  {% assign vendor = vendor_adjustment[0] %}
  {% assign adjustment = vendor_adjustment[1] %}
{% endfor %}

The parse_json filter tries to be more generous in the data it accepts. For example, it will accept fields surrounded by single quotes, and trailing commas for lists, both of which are not supported in the strict JSON specification.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us