Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
// Variables
var productId = 4482829 // Create virtual product and get the product id
var variant1 = 8769517 // Create a variant of the product, this is the variant that will go to the cart.
// Conditions Function: This function returns true or false depending if the conditions are present or not on the checkout. This part can change depending on the conditions.
var conditions = function(){
return $("#payments_options input:checked").val() == "186648"; // Certain payment option is selected on this example
}
// Finds product_cart_id by searching the productId on the cart. The Product Cart Id is the ID of the instance of the product that is on the cart. This section shouldn't change.
function find_order_product_id(cart_json) {
var id = null;
$.each(cart_json.products, function(i, order_product) {
if (order_product.product_id == productId) {
id = order_product.id;
}
});
return id; // return order_product.id if found
}
// Function to add product to Cart (Removes if previously pressent). This section shouldn't change.
var addProduct = function(data) {
var order_product_id = find_order_product_id(data);
var variants = data.products.map(product => { return product.variant_id })
if(!variants.includes(parseInt(variant1))) {
Jumpseller.updateCart(order_product_id, 0); // no payment cost on the order.
Jumpseller.addProductToCart(productId, 1, {
"Variant": variant1
})
}
}
// Function to remove product from cart. This section shouldn't change.
var removeProduct = function(data) {
var order_product_id = find_order_product_id(data);
Jumpseller.updateCart(order_product_id, 0); // no payment cost on the order.
}
// Function that checks the conditions and adds or removes the virtual product. This section shouldn't change.
function productInCart(){
if(conditions()){
Jumpseller.getCart({callback: addProduct})
}else{
Jumpseller.getCart({callback: removeProduct })
}
}
// Applies function on condition changes and on page load. This part can change depending on the conditions.
$("#payments_options").change(productInCart)
$(document).ready(productInCart)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.