Skip to content

Instantly share code, notes, and snippets.

@rgoytacaz
Last active February 22, 2019 14:18
Show Gist options
  • Save rgoytacaz/b74a05fc856784367b19b8626a3d264a to your computer and use it in GitHub Desktop.
Save rgoytacaz/b74a05fc856784367b19b8626a3d264a to your computer and use it in GitHub Desktop.
Workaround to update prices at checkout (currently not working)
(function(){
var currentPostalCode = "";
$(window).on('orderFormUpdated.vtex', function(ev, orderForm) {
if(orderForm && orderForm.shippingData && orderForm.shippingData.address && currentPostalCode != orderForm.shippingData.address.postalCode){
currentPostalCode = orderForm.shippingData.address.postalCode;
var params = ({'postalCode':{'value':currentPostalCode}});
$.ajax({
type: 'POST',
contentType: 'application/json',
dataType: 'json',
url: '/api/sessions/',
data: JSON.stringify({
public: params
})
})
.done(function(){
$(window).trigger('vtex.custom.updateCart');
});
}
});
$(window).on('vtex.custom.updateCart', function() {
if(vtexjs.checkout && vtexjs.checkout.orderForm && vtexjs.checkout.orderForm.items && vtexjs.checkout.orderForm.items.length > 0){
var item = {
id: vtexjs.checkout.orderForm.items[0].id,
quantity: vtexjs.checkout.orderForm.items[0].quantity,
seller: vtexjs.checkout.orderForm.items[0].seller
};
var itemToRemove = {
"index": 0,
"quantity": vtexjs.checkout.orderForm.items[0].quantity,
};
vtexjs.checkout.removeItems([itemToRemove]).done(function(){
vtexjs.checkout.addToCart([item], null);
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment