How can I set up a category that links to an external site

Last updated

If you want some category in Ecwid to link to an external site or to a separate page of your site, you just need to add a special JavaScript code after the Ecwid integration code on the page where your store is installed. This code uses the Ecwid Javascript API for handling the Ecwid 'OnPageLoad' event and redirecting your visitors to an external page if they try to open some particular category in Ecwid. 

Please see this code below. ​

This code works for any Ecwid store, however the very first part of it should be modified to fit your exact needs. The part which requires modifying is this one:


// categoryURLs array as category Id => redirection URL
var categoryURLs = {                                                                      
   '1765387' : 'http://www.google.com',                                                   
   '1797366' : 'http://www.ecwid.com'                                                     
};  

In order to set redirection rules for your store you should edit the 'categoryURLs' variable. In the example above:

1765387 and 1797366 are the IDs of categories which should link to external pages;

'http://www.google.comand 'http://www.ecwid.com' are those pages.

1. To get the ID of the category you want to set as a link in your store please refer to this article 

2. When you edit the list of category URLs please make sure that each URL is properly enclosed in quotation marks and each line ends with comma except the last line.

You can use the code above on all of your storefronts because category IDs remain the same for all of them. Moreover, since it is not changed from one storefront to another, the best way to use it is to place it in a javascript file (with a .js extension) and call this file on multiple pages instead  of pasting the code into pages one by one.

You can use the following js file as a base for your own one: https://gist.githubusercontent.com/makfruit/1373124/raw/ecwid_category_redirects.js

To make it work please do the following:

  1. Download this javascript file.
  2. Modify redirection rules as I described earlier in this post.
  3. Place it  somewhere in the web with public access.
  4. Specify the location of the external JavaScript file on the page you want it to be executed on by appending a link to it to your Ecwid integration code. Such a link should look like this:

<script type="text/javascript" language="javascript" src="www.example.com/myfile.js"></script> 

where "www.example.com/myfile.js" should be replaced with the location of the file. 

The described method (with inclusion of a js file) is also recommended for Wordpress users. The Wordpress editor that is used to edit the source code of Ecwid shop page parses JavaScript codes incorrectly. So it is safer to use the script in the external file as described above, even if you use the WP 'ecwid-shopping-cart' plugin for your store integration. You need to find a page where your Ecwid store is installed (it can be found under the 'Pages' tab in your WP admin panel, usually it is called Store), edit it, switch to HTML editor and insert a link to your external JavaScript file into its source after all the Ecwid integration codes. 

There is also a more simple way to make the links in the category listings by adding those links into the category description (see Ecwid control panel → Catalog → Categories, select the category you need and edit the description in the Category description area at the bottom). But this way your custom links will appear above all the other category contents and not among the other subcategories.

​For product details

You also can use this solution, if you need to redirect customer to another page upon visiting the products details page of a particular product. You just need to change the code above a bit. Change “CATEGORY” to “PRODUCT” in this line

&& 'CATEGORY' == page.type

so that it reads as

&& 'PRODUCT' == page.type

change “page.categoryId” variable to “page.productId” in the code, and of course use the product IDs instead of category IDs accordingly.

Was this article helpful?
0 out of 0 found this helpful
Questions?
Get help
  • Presales Chat

    Need help choosing the right Ecwid plan? Our Sales team has the answer you’re looking for.

  • Forums

    Join the Ecwid community. Discuss support topics and store ideas with other Ecwid users.

  • Contact us

    Still have questions about Ecwid? Let us know! We will be glad to help you with your Ecwid store.

  • Status Monitor

    Get real time status updates on Ecwid services here, 24/7