Untitled
unknown
javascript
3 years ago
1.0 kB
6
Indexable
//add to cart for guest and logged in user
function addToCart(id, client_id) {
//check if client id is int or not
if (Number.isInteger(client_id)) {
var data = {
'action': 'add_to_cart',
'product_id': id,
'client_id': client_id,
'status': 'C',
'quantity': 1
};
//Async false to wait for response
jQuery.ajaxSetup({ async: false });
var ajaxurl = 'http://localhost/yz/assets/php/addToCart.php';
console.log(data);
$.ajax({
url: ajaxurl,
type: 'POST',
data: data,
});
}
else {
//use local storage to create cart for guest
var cart = JSON.parse(localStorage.getItem('cart'));
if (cart == null) {
cart = [];
}
var item = {
'product_id': id,
'client_id': client_id,
'status': 'C',
'quantity': 1
};
cart.push(item);
localStorage.setItem('cart', JSON.stringify(cart));
console.log(cart);
}
}
Editor is loading...