Jump to content

Change Product Links - Javascript

Recommended Posts

Site URL: https://mayellre.squarespace.com/rentals

Howdy SQSP forum!

I'm looking for a way to redirect or change the link of my products  on my products page from the automatic product pages they link to, to other individual pages I have already created. I'm using products for the rentals because I will be using them with the "Advanced Map Block" plugin & "Universal Filter" from Michael at squarewebsites. These are not totally integrated yet though.

I've done a lot of reading on the forum and I think it will require javascript?? 😅 Any and all help will be greatly appreciated!

Link to comment

After driving myself nuts, turns out no js needed!

For anyone looking for a solution, put this in your code injection

Source

<!-- Custom URL Redirect -->
<script>
   var fromUrl = '/initialURL';
   var toUrl = '/newURL';
   // do not edit below this line
   var fromRegex = "^\\" + fromUrl + "\\/?$";
   var match = location.pathname.match(new RegExp(fromRegex, 'i'));
   if (match && !window.frameElement) {
     window.location.replace(toUrl);
   }
 </script>
<!-- End Of Custom URL Redirect -->
Link to comment
8 hours ago, PrismaVisual said:

turns out no js needed!

The code you’ve provided above is JavaScript! 😃

About: SQSP User for 17 yrs. Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF Digital, building Squarespace Extensions to supercharge your commerce website. 
Content: Links in my posts may refer to SF Digital products or may be affiliate links.
If my advice helped, you can thank me by clicking one of the feedback emojis below. I love coffee too.

Link to comment
  • 2 weeks later...

Hi Guys,

I've been messing around with 2 different approaches since originally posting but haven't been able to work it out (it's been weeks 😑 lol).  I know I'm doing something(s) wrong..I just don't know what I don't know yet! 😅 . I posted this in someone else's thread already but it seems like y'all probably know what you're doing!!!

I'm trying to implement some javascript on my store page so I can redirect the product links. 

I want to redirect product urls like: https://mayellre.squarespace.com/rentals/p/130-water-street to urls like: https://mayellre.squarespace.com/130-water-street.

There are currently 40  rental "products" (more to come eventually), but they will never be able to be added to a cart/purchased. I assume that may be why the first method below doesn't work.

<script>
  window.customCartDrawer = {
onUpdateFunction: function(drawer, data) {
  
$(document).ready(function(){
    $('a').each(function(){
        this.href = this.href.replace('/rentals/p/', '/');
    });

  } }
</script>

 

I was able to get this second option to work for one redirect, however when I tried to implement it for all 40 product links, it only redirects 1 of them. Maybe im targeting them wrong or separating wrong?

Single var set:

<script>
   var fromUrl = '/rentals/p/1-west-street-listing';
   var toUrl = '/1-west-street';
   // do not edit below this line
   var fromRegex = "^\\" + fromUrl + "\\/?$";
   var match = location.pathname.match(new RegExp(fromRegex, 'i'));
   if (match && !window.frameElement) {
     window.location.replace(toUrl);
   }
 </script>

 

My best guess at multiple var (not working) 😕

<script>
var fromUrl = '/rentals/p/1-west-street-listing';
var toUrl = '/1-west-street';

var fromUrl = '/rentals/p/2-gold-street-listing';
var toUrl = '/2-gold-street';

var fromUrl = '/rentals/p/2-water-street-listing';
var toUrl = '/2-water-street';

var fromUrl = '/rentals/p/8-spruce-street-listing';
var toUrl = '/8-spruce-street';

var fromUrl = '/rentals/p/10-barclay-street-listing';
var toUrl = '/10-barclay-street';

var fromUrl = '/rentals/p/10-liberty-street-listing';
var toUrl = '/10-liberty-street';

var fromUrl = '/rentals/p/15-broad-street-listing';
var toUrl = '/15-broad-street';

var fromUrl = '/rentals/p/15-cliff-street-listing';
var toUrl = '/15-cliff-street';

var fromUrl = '/rentals/p/15-park-row-listing';
var toUrl = '/15-park-row';

var fromUrl = '/rentals/p/15-william-street-listing';
var toUrl = '/15-william-street';

var fromUrl = '/rentals/p/19-dutch-street-listing';
var toUrl = '/19-dutch-street';

var fromUrl = '/rentals/p/20-exchange-place-listing';
var toUrl = '/20-exchange-place';

var fromUrl = '/rentals/p/20-pine-street-listing';
var toUrl = '/20-pine-street';

var fromUrl = '/rentals/p/20-west-street-listing';
var toUrl = '/20-west-street';

var fromUrl = '/rentals/p/21-west-street-listing';
var toUrl = '/21-west-street';

var fromUrl = '/rentals/p/21-23-south-william-street-listing';
var toUrl = '/21-23-south-william-street';

var fromUrl = '/rentals/p/30-park-place-listing';
var toUrl = '/30-park-place';

var fromUrl = '/rentals/p/37-wall-street-listing';
var toUrl = '/37-wall-street';

var fromUrl = '/rentals/p/40-broad-street-listing';
var toUrl = '/40-broad-street';

var fromUrl = '/rentals/p/40-broad-street-listing';
var toUrl = '/40-broad-street';

var fromUrl = '/rentals/p/45-wall-street-listing';
var toUrl = '/45-wall-street';

var fromUrl = '/rentals/p/50-west-street-listing';
var toUrl = '/50-west-street';

var fromUrl = '/rentals/p/55-liberty-street-listing';
var toUrl = '/55-liberty-street';

var fromUrl = '/rentals/p/55-wall-street-listing';
var toUrl = '/55-wall-street';

var fromUrl = '/rentals/p/63-wall-street-listing';
var toUrl = '/63-wall-street';

var fromUrl = '/rentals/p/67-wall-street-listing';
var toUrl = '/67-wall-street';

var fromUrl = '/rentals/p/70-pine-street-listing';
var toUrl = '/70-pine-street';

var fromUrl = '/rentals/p/71-broadway-listing';
var toUrl = '/71-broadway';

var fromUrl = '/rentals/p/75-wall-street-listing';
var toUrl = '/75-wall-street';

var fromUrl = '/rentals/p/80-john-street-listing';
var toUrl = '/80-john-street';

var fromUrl = '/rentals/p/85-john-street-listing';
var toUrl = '/85-john-street';

var fromUrl = '/rentals/p/88-greenwich-street-listing';
var toUrl = '/88-greenwich-street';

var fromUrl = '/rentals/p/90-west-street-listing';
var toUrl = '/90-west-street';

var fromUrl = '/rentals/p/90-william-street-listing';
var toUrl = '/90-william-street';

var fromUrl = '/rentals/p/95-wall-street-listing';
var toUrl = '/95-wall-street';

var fromUrl = '/rentals/p/100-barclay-street-listing';
var toUrl = '/100-barclay-street';

var fromUrl = '/rentals/p/110-greenwich-street-listing';
var toUrl = '/110-greenwich-street';

var fromUrl = '/rentals/p/114-liberty-street-listing';
var toUrl = '/114-liberty-street';

var fromUrl = '/rentals/p/123-washington-street-listing';
var toUrl = '/123-washington-street';

var fromUrl = '/rentals/p/130-water-street-listing';
var toUrl = '/130-water-street';

var fromUrl = '/rentals/p/180-water-street-listing';
var toUrl = '/180-water-street';
   // do not edit below this line
   var fromRegex = "^\\" + fromUrl + "\\/?$";
   var match = location.pathname.match(new RegExp(fromRegex, 'i'));
   if (match && !window.frameElement) {
     window.location.replace(toUrl);
   }
 </script>
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.