Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active August 17, 2019 23:11
Show Gist options
  • Save aatronco/f3eaf8943294e92c43f629ed89337b84 to your computer and use it in GitHub Desktop.
Save aatronco/f3eaf8943294e92c43f629ed89337b84 to your computer and use it in GitHub Desktop.
// Step 1: Create a virtual Product Called "Envio a Cobranza" with two Variants: Portugal/Spain
// Step 2: Get the Product and variants IDs
// Step 3: replace the following variables with the Product ID (in the product settings URL), Variant Ids (see tip below) and Manual Payment ID (In the payment method URL in the admin panel)
var productId = "2388937"
var portugalId = "1883423"
var spainId = "1883423"
var paymentId = "136033"
/* TIP: To get the variant ID of the countries I added this code to the product page and checked the console
<script>
{% for variant in product.variants %}
console.log("{{variant.sku}} - {{variant.id}}")
{%endfor%}
</script>
*/
// Step 4: Add this code to a script on the checkout:
function payment_cost_order_product_id(cart_json){
var id = null;
$.each(cart_json.products, function( i, order_product ){
if(order_product.product_id == productId ){ // product for adding payment cost. You need to create a virtual product with the additional price. The ID is the number in the URL of the product in the admin panel.
id = order_product.id;
}
});
return id; // return order_product.id if found
}
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.
}
$(document).ready(function() {
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 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 == paymentId ){ //This is the payment method (envio a cobrança) ID, you can get it from the URL of the payment method in the admin panel.
if ($("#order_shipping_address_country").val() == 'PT'){
Jumpseller.getCart({callback: remove_payment}) // remove before adding.
Jumpseller.addProductToCart(productId, 1, {"País": portugalId}, {callback: payment_cost_updated}) // adds payment cost for portugal. The code 1883423 is the variant "Portugal" in the virtual product added.
}
else if ($("#order_shipping_address_country").val() == 'ES'){
Jumpseller.getCart({callback: remove_payment}) // remove before adding.
Jumpseller.addProductToCart(productId, 1, {"País": spainId}, {callback: payment_cost_updated}) // adds payment cost for Espanha. The code for the variant "Spain"
}
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});
$("#order_shipping_address_country").change();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment