Created
February 2, 2019 13:29
-
-
Save raulfmiranda/e52f05f3b433f7a9981072d08b0afc4d to your computer and use it in GitHub Desktop.
ADICIONAR PRODUCT AO CART COM AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('./util'); | |
var page = require('./page'); | |
var dialog = require('./dialog'); | |
var addDirectlyToCart = function (e) { | |
e.preventDefault(); | |
let addDirectlyToCartLink = $('#hiddenLinkAddDirectlyToCart').attr('href'); | |
let productID = $('#productIdInput')[0].value; | |
let form = $('#productIdForm'); | |
$.ajax({ | |
url: addDirectlyToCartLink, | |
type: 'GET', | |
data: { | |
pid: productID | |
}, | |
crossDomain: true, | |
success: function(response) { | |
// When product does not exist | |
if (response.isProductMaster == null) { | |
openDialog(Resources.PRODUCTID_NOTFOUND); | |
} else if (response.isProductMaster) { // product exists and it is a master product | |
openDialog(Resources.PRODUCTID_ISMASTER); | |
} else { // product does exist and it is not master product | |
ajaxAddProduct(form); | |
} | |
}, | |
error: function(jqXHR, exception) { | |
console.log('FAIL'); | |
} | |
}); | |
}; | |
function ajaxAddProduct(form) { | |
console.log('inside ajaxAddProduct'); | |
// form must have an inpud with 'name' pid | |
$.ajax({ | |
url: util.ajaxUrl(Urls.addProduct), | |
type: 'POST', | |
crossDomain: true, | |
data: form.serialize(), | |
success: function(response) { | |
console.log('SUCCESS ajaxAddProduct'); | |
page.redirect(Urls.cartShow); | |
}, | |
error: function(jqXHR, exception) { | |
console.log('FAIL ajaxAddProduct'); | |
} | |
}); | |
} | |
module.exports = function () { | |
$('#productIdButton').on('click', addDirectlyToCart); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment