Skip to content

Instantly share code, notes, and snippets.

@PawelGIX
Last active April 4, 2023 14:29
Show Gist options
  • Save PawelGIX/aa1048ec1af5fdfb411a67c99a633f73 to your computer and use it in GitHub Desktop.
Save PawelGIX/aa1048ec1af5fdfb411a67c99a633f73 to your computer and use it in GitHub Desktop.
Prestashop add product to cart JS
function addProduct(id_product, quantity, totalQty, delay)
{
	const token = prestashop.static_token;
	const url = prestashop.urls.pages.cart;
	const query = 'controller=cart&add=1&action=add&ajax=true&token=' + token + '&id_product=' + id_product + '&id_customization=0&qty=' + totalQty;
	var controllerUrl = url.concat(query);
	var functionName = "addToCart";
	
	$.ajax({
		cache: false,
		data: query,
		beforeSend: function () 
		{
			setTimeout(delay);
		},
		success: function(resp) 
			{
				// console.log(resp);
				prestashop.emit('updateCart', {
					reason: {}, resp: resp
				});
			},
			error: function(resp) {
				// console.log(resp);
				prestashop.emit('handleError', {eventType: 'addProductToCart', resp: resp});
			}
	});
}

Example usage:

addProduct(31,1,1,100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment