Set URL (handle) to include SKU in Shopify by using Arigato Automation
Enhance your product URLs by automatically incorporating the SKU, ensuring efficient and organized product identification in your Shopify store.
This workflow fires when a product is created or updated. It combines the SKU of the product with the product's title.
There are many variations you can do with this automation. For example, you could choose to set the URL (handle) to include a metafield or a tag instead of using the SKU.
It's reassuring to know that Shopify handles any redirects that you might need when URLs change in your store.
Here are a few very handy tips that you should know about when customizing this automation
First, you should know that all products in Shopify use a one-to-many relationship. One product has many variants. This is hidden to the administrator in Shopify for the first variant but it becomes important in this case.
Since the SKU you set on the first product is actually attached to the first variant and not the product itself, you need to access that first variant to get the SKU. In the app, if you access the SKU token from the token browser, it will automatically give you a loop of all the variants, but we just want the first item in the loop, so we access that directly by going right for product.variants[0].sku
You might also notice that we're not adding any hyphens in our setting. We're doing that because this is the most flexible approach and because Shopify handles it for us automatically.
For example, let's say that we don't have a SKU set on our product when it gets saved. Rather than the URL becoming something odd with a starting hyphen, instead, it naturally starts without a space or a hyphen and still looks right and works correctly.
Usually, it's a good idea to include a condition on the workflow, so it doesn't fire if it doesn't need to. In this case, it can be challenging and requires some coding in the condition to account for the variations mentioned above. In order for the condition test to work, we need to create the entire handle, exactly as it would be done by Shopify. Let's break down our example with how that can be done.
Here's what you would enter into the condition to make sure that the workflow only fires when it needs to change the URL. Note that Arigato Automation uses Twig, not Liquid for syntax. Twig is very similar to Liquid.
{% set url = product.variants[0].sku ~ ' ' ~ product.title %} {{ url | to_handle }}
That's a bit much, let's think it through. We're creating a variable from the first item in the list of variants and taking the SKU from it. The next bit is adding a space in that variable (that's the ~ ' ' ~ part) then adding the title to it. Finally, our variable is printed while running it through our to_handle function to get the exact handle that Shopify would set.