cancel
Showing results for 
Search instead for 
Did you mean: 

Rest API call to get cartId for active guest user and logged in customer

Re: Rest API call to get cartId for active guest user and logged in customer

Hi Kavitha,

 

Sorry I was away, just seeing this reply now.

Thanks for the link, but I'm still stuck at extracting the cart ID for the active guest user who is not logged in yet. can you please advise.

at present, I can get the logged in cartID of a customer but not able to get the one for active customer who is not logged in.

 

<script>
function AddToCartCalCi() {
$.ajax({
url: "https://magento2/rest/V1/carts/mine",
type: "get",
error: function (e) {
console.log("TESTING ERROR", e);
createGuestCart();
},
success: function (resp) {
console.log("testing response", resp.id);
getCart(resp.id);
}
});

function createGuestCart() {
const url = "https://magento2/rest/V1/guest-carts";
$.ajax({
type: "POST",
suceess: function (resp) {
console.log("got guest cart", resp.data);
},
error: function (error) {
console.log("error in creating guest cart", e);
}
});
}
}

function getCart(customerId) {
const url =
"https://magento2/rest/V1/carts/mine?customer_id=" +
customerId;
$.ajax({
url: url,
type: "POST",
error: function (e) {
console.log("TESTING ERROR", e);
},
success: function (resp) {
console.log("testing cart data", resp);
}
});
}


Thank you