Last active
April 13, 2020 17:54
-
-
Save Tetsuro/4403192 to your computer and use it in GitHub Desktop.
Script for adding a donation product via 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
<script type="text/javascript" charset="utf-8"> | |
//<![CDATA[ | |
// Including jQuery conditionnally. | |
if (typeof jQuery === 'undefined') { | |
document.write({{ "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" | script_tag | json }}); | |
document.write('<script type="text/javascript">jQuery.noConflict();<\/script>'); | |
} | |
//]]> | |
</script> | |
<script> | |
{% assign linklist = linklists['donation'] %} | |
var id = {{ linklist.links.first.object.variants.first.id }}; | |
$(document).ready(function () { | |
$('input[name="checkout"], input[name="goto_pp"], input[name="goto_gc"]').click(function(e) { | |
e.preventDefault(); | |
var amount = $('#donate').val(); | |
amount = amount * 100; | |
amount = parseInt(amount); | |
function addDonation(){ | |
var params = { | |
type: 'POST', | |
url: '/cart/add.js', | |
data: 'quantity='+amount+'&id='+id, | |
dataType: 'json', | |
success: function(line_item) { | |
document.location.href = '/checkout'; | |
}, | |
error: function() { | |
} | |
}; | |
$.ajax(params); | |
} | |
addDonation(); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment