How To Get All Tags, Product Types and Product Vendors using Shopify API

Well using APIs you can’t, let’s see how to overcome this limitation.

While working with one of my personal Shopify Application, I needed to get all the Shopify store’s Tags, but unfortunately there is no way you can accomplish this with Shopify APIs.

Not only for Tags same goes for Product Types and Product Vendors, there is no Shopify API end point which gives you these informations.

Of course it’s not unachievable, you can fetch all products ( not to mention max 250 rows per request limit ) and then iterate through all products, gather those tags and do some filtering to eliminate the duplicates ( wow thats sounds lot work for getting tag list ). This is okay if you have few hundred products, but not ideal solution for thousands of products.

I would like to share what i did for my Shopify Application.

As you already aware that these things can be done in “.liquid” templates in your themes ( that’s sad not with API ), i am using the same idea ( with the help of alternates template for search.liquid ).

Using this solution you can fetch

  1. Tags
  2. Product Types
  3. Product Vendors

Requirements

  1. PHP with CURL
  2. Alternative Liquid Templates for Search

Templates

Create the following templates at your store current theme’s template folder

  1. search.tags.liquid
  2. search.product_types.liquid
  3. search.product_vendors.liquid

search.tags.liquid

{% layout none %}
{% capture output %}
    {% for collection in collections %}	
            {% if forloop.index == 1 %}""{% endif %}
            {% if collection.all_tags.size > 0 %}
                {% for tag in collection.all_tags %}
                
                    {% if output contains tag %}
                    {% else %}
                        ,"{{tag}}"
                    {% endif %}
    
                {% endfor %}  
            {% endif %}	  	
    {% endfor %}
{% endcapture %}
{{ output | strip_newlines | prepend: '[' | append: ']' }}

search.product_types.liquid

{% layout none %}
{% capture output %}
	{% for product_type in shop.types %}  	
		{"type":"{{product_type}}"}{% unless forloop.last %},{% endunless %}
	{% endfor %}
{% endcapture %}
{{ output | strip_newlines | prepend: '[' | append: ']' }}

search.product_vendors.liquid

{% layout none %}
{% capture output %}
  	{% for product_vendor in shop.vendors %}     
      		{"vendor":"{{product_vendor}}"}{% unless forloop.last %},{% endunless %}
  	{% endfor %}
{% endcapture %}
{{ output | strip_newlines | prepend: '[' | append: ']' }}

That’s it your ready to pull those data.

PHP Sample Code

function pullJson( $url ) {
	$curl = curl_init();
	curl_setopt_array($curl, array(
    		CURLOPT_RETURNTRANSFER => 1,
    		CURLOPT_URL => $url,
    		CURLOPT_USERAGENT => 'agent-sark'
	));

	$resp = curl_exec($curl);
	curl_close($curl);
	return json_decode( $resp, true );
}


$storeUrl = "http://". { your store } .".myshopify.com";
/* this will gets you all the tags ( of course with duplicate ) */
$pTags = array_unique( pullJson( $storeUrl ."/search?view=tags" ) );

/* this will gets you all the product types */
$pTypes = pullJson( $storeUrl ."/search?view=product_types" );

/* this will gets you all the product vendor */
$pVendors = pullJson( $storeUrl ."/search?view=product_vendors" );

This method can be used with your Shopify Application
Those liquid templates can be inserted into the store’s current theme via Shopify API during App installation.

Leave a Reply to Michael Cancel reply

Your email address will not be published.

 

1 Comment(s)

  1. Michael August 28, 2015

    Hi,

    One drawback of this solution is that if a password is set for the storefront, the call will not return the values :(.