Have your say in Community Polls: What was/is your greatest motivation to start your own business?

How to create a permalink that autofills cart and directs to a specific product page?

How to create a permalink that autofills cart and directs to a specific product page?

conchousa
Tourist
12 0 1

Hi, I am wondering if anybody knows how to create a permalink that autofills my cart with specific products. But, instead of taking the user to the checkout page, it takes them to a specific product page.

 

This is the URL I have generated that fills the cart: https://pecosoutdoor.com/cart/add/?id[]=41529975374029&id[]=40184234016973&id[]=40184233590989

 

This is the product page I would like the user to arrive at with a full cart: https://pecosoutdoor.com/products/the-pecos-table?variant=41111205216461

 

Thanks so much.

Replies 3 (3)

gr_trading
Shopify Partner
1948 145 204

Hi @conchousa 

 

You can custom code this feature in your store.

 

The problem with your logic is what will happen when URL will have more than 1 product will your redirect URL will be same every time?

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
conchousa
Tourist
12 0 1

Yes, I will always want the user to be redirected to the same product page.

 

Could you help me do this?

gr_trading
Shopify Partner
1948 145 204

Hi @conchousa 

 

You can use the below script to add a product in the cart and redirect to your URL

 

var addautocart = getQuerystringNameValue('auto_addcart');
if(addautocart != null){
	cartList = addautocart.split(",");
	var cartItem = [];
	for(var _c=0; _c < cartList.length; _c++){
	  _cart = cartList[_c].split(":")
	  _cItem = {};
	  if(_cart.length > 1){
		_cItem.id = _cart[0];
		_cItem.quantity = _cart[1];
		cartItem.push(_cItem);
	  }
	}
	setTimeout(function(){
	  
	  fetch(window.Shopify.routes.root + 'cart/add.js', {
		method: 'POST',
		  headers: {
			'Content-Type': 'application/json'
		  },
		  body: cartItem
		})
		.then(response => {
		  location.href = "Product URL";
		   return response.json();
		})
		.catch((error) => {
		  console.error('Error:', error);
		});
		
	},500)
}

 

For the query parameters function please visit the below URL

 

https://www.growyourgk.com/get-querystring-value/

 

Your auto-add cart URL would be like this.

https://pecosoutdoor.com?auto_addcart=[variantid]:[qty],[variantid]:1

 

where variantid is the product variant ID and after : QTY you want to add to the cart.

 

Hope this will help...

 

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee