Hi everyone, I am wondering if it is possible to redirect search results to a specific product page. My client has people on his site searching for a type of product he sells, but they are searching for a competitor’s brand. He would like people who search that brand on his site to go straight to his brand’s product page, rather than a “no results” page.
To summarize, if someone was on his Shopify site and searched “XYZ”, which he doesn’t sell, can they be taken to product page “ABC”, which is a similar product to XYZ, just a different brand that he sells cheaper. We’re trying to eliminate those “no results” pages from appearing for certain searches so these potential customers can see comparable products.
Would love to figure out if this is possible! He also doesn’t want to add those keywords to his product page as a “comparable to” though I know this is the easiest solution.
Thanks.
@tmarroyo17
Yeah, the solution is relatively simple and requires just a few lines of code depending on how many pages you want to redirect for.
You can set up a snippet with all of the keywords and products they should link to so that when a search is performed the contents of that snippet are checked and if any match occurs you redirect the customer to the associated page.
I can share exact code later if you need it but the search object reference should help you get there if you know basic JavaScript redirect code.
Hope that helps!
@tmarroyo17
The snippet would have something like this:
{% liquid
assign product_abc = all_products['product-abc']
assign keyword_abc = 'XYZ'
assign redirect_url = ''
if search.performed
if search.terms contains keyword_abc
assign redirect_url = product_abc.url
endif
endif
%}
{% unless redirect_url == blank %}
{% endunless %}
Then you would load that after the beginning of the “” element so that the redirect occurs immediately. I did not test it but if you use that with what your storefront requires I believe it should work for your scenario.
Ideally you would use a few Arrays of product handles and keywords (or something similar) and loop through them so that you are not just filling up the file with conditionals. You could also attach the keyword(s) as a metafield on products or a tag and then use them in a navigation menu or collection so that you do not have to write them all in the code itself and just loop through them to check against the search terms.
Hope that helps!
Rob,
Thanks so much! I am not great with code, but looking over your snippet example, I think I have enough knowledge to plug in what I need to. I will give this a try as soon as I am able and report back on the success or any issues.
Thanks again!
Sorry, one more question… On which page do I this code to? Is it search.liquid?