Javascript to run the Add to Cart for Dawn Theme

Javascript to run the Add to Cart for Dawn Theme

izy1
Excursionist
16 0 6

Hi community

I use a page designer and I need the Javascript to allow me to add a product to the cart.
Anyone who knows what the code is?

I have used this, but I can't seem to make that work

fetch( "/cart/update.js", { method: "POST", headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ updates: {}, sections: ["cart-drawer", "cart-icon-bubble"] }) } ).then(res => res.json()) .then(cartData => { const cart = document.querySelector('cart-drawer') cart.renderContents(cartData) }) document.querySelector('cart-drawer.drawer').classList.remove('is-empty');



Reply 1 (1)

gr_trading
Shopify Partner
1976 146 205

use the below script to add an item to the cart.

 

let formData = {
	 'items': [{
   id: 36110175633573,
   quantity: 2
  }]
	};
	fetch(window.Shopify.routes.root + 'cart/add.js', {
	  method: 'POST',
	  headers: {
		'Content-Type': 'application/json'
	  },
	  body: JSON.stringify(formData)
	})
	.then(response => {
	  return response.json();
	})
	.catch((error) => {
	  console.error('Error:', error);
	});

 

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