Asked by: Jagadeesh Chandra
When I add a product to the cart I am using the code below:
addproduct(itemId) {
this.showLoading = true
this.$http.post('/shampoo', {'item': itemId}).then((response) => {
swal({
title: "Success!",
text: "Product added to your basket!",
type: "success",
timer: 1500,
showConfirmButton: false
})
this.showLoading = false
}, (response) => {
this.showLoading = false
})
I created a popup form which for no product was at the cart, once they click on the add to cart button it popups, once the product was their at the cart, if any one need to add another product it again showing popup. I don't want the popup if product was at the cart
here is the code for popup form show() { this.showLoading = true;
$(".loading").css('display', 'block');
$('#products').modal("hide");
//
let code = $('input[name=code]').val();
if(code) {
this.$http.post('/codee', { code: code}).then((response) => {
// $('#basic-form').submit();
}, (response) => {
swal({
title: "Error!",
text: "Sorry this Code is invalid!",
type: "error",
timer: 2500,
showConfirmButton: false
})
$(".loading").css('display', 'none');
this.showLoading = false
$('input[name=code]').val('')
})
Answers
Answered by: Trust Okoroego at 2017-07-11 03:25AM
When you click add to cart, at the server side, you need to check if the item id is in the cart and return a flag to either indicate the item is in the cat or not. I assume this code is responsible for the pop up.
swal({
title: "Error!",
text: "Sorry this Code is invalid!",
type: "error",
timer: 2500,
showConfirmButton: false
})
You need to check for the returned value from the server, if it returns a flag indicating that the item already exist, just return false; that will block the pop up code from being reached. or you could wrap the pop code in an if statement. e.g
if(response.flag === 1){
return false;
// your pop up code goes here
}
or
if(response.flag === 0){
// your pop up code goes here
}
// flag can assume 0 => Added but does not exist, 1=> added but already
// exist. ect
// while the actual data will be stored in response.data
But I am wondering of the reason you are implementing this. If a user clicks add to cart, you are supposed to increment the number of that same item if already exist in the cart
No comments:
Post a Comment