Skip to content

Instantly share code, notes, and snippets.

@tiagomatos
Created December 7, 2018 20:06
Show Gist options
  • Save tiagomatos/8fa20d3b1439f45debf55e1f3307d299 to your computer and use it in GitHub Desktop.
Save tiagomatos/8fa20d3b1439f45debf55e1f3307d299 to your computer and use it in GitHub Desktop.
function payment_cost_order_product_id(cart_json){
var id = null;
$.each(cart_json.products, function( i, order_product ){
if(order_product.product_id == '287177'){ // product for adding payment cost.
id = order_product.id;
}
});
return id; // return order_product.id if found
}
$(document).ready(function() {
//Custom Payments / Shipping Logic
var update_payment_methods = function(data){
var region = $("#order_shipping_address_region").val();
// payments to be charged at destination. only available to country=Portugal or Spain.
if ($("#order_shipping_address_country").val() == 'PT') {
$("#order_payment_method_122625").parent('li').show(); // manual // https://www.evernote.com/l/APakQA2Fpc5C2KqA0FdDSwAtRl5iytm-FBk
} else if ($("#order_shipping_address_country").val() == 'ES' && region != '07' && region != '53' && region != 'CE' && region != 'ML') {
$("#order_payment_method_122625").parent('li').show();
} else{
$("#order_payment_method_122625").parent('li').hide(); // manual // https://www.evernote.com/l/APakQA2Fpc5C2KqA0FdDSwAtRl5iytm-FBk
$("#order_payment_method_122625").prop('checked',false).removeAttr('checked');
}
// IfThenPay (referencias multibanco). Only available to country=Portugal.
if ($("#order_shipping_address_country").val() == 'PT'){
$("#order_payment_method_121994").parent('li').show(); // ifthenpay
}else{
$("#order_payment_method_121994").parent('li').hide(); // ifthenpay
$("#order_payment_method_121994").prop('checked',false).removeAttr('checked');
}
// IfThenPay (referencias MBWAY). Only available to country=Portugal.
if ($("#order_shipping_address_country").val() == 'PT'){
$("#order_payment_method_130686").parent('li').show(); // ifthenpay
}else{
$("#order_payment_method_130686").parent('li').hide(); // ifthenpay
$("#order_payment_method_130686").prop('checked',false).removeAttr('checked');
}
}
var remove_payment = function(data){
var order_product_id = payment_cost_order_product_id(data);
Jumpseller.updateCart(order_product_id, 0); // no payment cost on the order.
}
var payment_cost_updated = function(data){
var order_product_id = payment_cost_order_product_id(data);
Jumpseller.updateCart(order_product_id, 1); // only one payment cost per order.
}
var update_payment_cost = function(event){
var payment_method_selected_id = $("input[type=radio][name='order[payment_method]']:checked").val();
console.log(payment_method_selected_id);
// if selected manual payment ("Pagamento à Cobrança").
if(payment_method_selected_id == '122625'){
if ($("#order_shipping_address_country").val() == 'PT'){
Jumpseller.getCart({callback: remove_payment}) // remove before adding.
Jumpseller.addProductToCart(287177, 1, {"País": 1652224}, {callback: payment_cost_updated}) // adds payment cost for portugal.
}
else if ($("#order_shipping_address_country").val() == 'ES'){
Jumpseller.getCart({callback: remove_payment}) // remove before adding.
Jumpseller.addProductToCart(287177, 1, {"País": 1652225}, {callback: payment_cost_updated}) // adds payment cost for Espanha.
}
else {
Jumpseller.getCart({callback: remove_payment}) // remove payment cost from the cart for non PT/ES countries.
}
}else{
Jumpseller.getCart({callback: remove_payment}) // remove payment cost from the cart for non PT/ES countries.
}
}
// start listenning for shipping and payment methods changes.
Jumpseller.paymentMethodListener("input[type=radio][name='order[payment_method]']", {callback: update_payment_cost});
Jumpseller.countryListener("#order_shipping_address_country", {callback: update_payment_cost});
Jumpseller.countryListener("#order_shipping_address_country", {callback: update_payment_methods});
Jumpseller.regionListener("#order_shipping_address_region", {callback: update_payment_methods});
$("#order_shipping_address_country").change();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment