Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/65b3f038f8c23e324d13b7881982394b to your computer and use it in GitHub Desktop.
Save andrewlimaza/65b3f038f8c23e324d13b7881982394b to your computer and use it in GitHub Desktop.
2 demos to demonstrate the PMPro checkout_levels API call.
<?php
// Copy from below here...
/*
* Add widget to checkout to get result of checkout_levels API call when button is pressed.
*/
function my_pmpro_test_checkout_levels_api() {
?>
<hr/>
<textarea rows="6" id="my_pmpro_checkout_level_api_output" value=""></textarea><br>
<button id='my_pmpro_test_checkout_level_api' type="button">Test API</button>
<script>
function my_pmpro_test_checkout_level_api() {
jQuery.noConflict().ajax({
url: '<?php echo( get_rest_url() ); ?>pmpro/v1/checkout_levels',
dataType: 'json',
data: jQuery( "#pmpro_form" ).serialize(),
success: function(data) {
jQuery('#my_pmpro_checkout_level_api_output').val( JSON.stringify( data ) );
}
});
}
jQuery(document).ready(function () {
jQuery('#my_pmpro_test_checkout_level_api').click(function () {
my_pmpro_test_checkout_level_api();
});
});
</script>
<?php
}
add_action( 'pmpro_checkout_before_submit_button', 'my_pmpro_test_checkout_levels_api' );
/*
* Show live price above payment information box at checkout.
* Note, this will make an API call after every field change at checkout
*/
function my_pmpro_live_price( $param ) {
?>
<h4 id='pmpro_live_price'>Total: $x.xx</h4>
<script>
jQuery(document).ready(function () {
function pmproUpdateLivePrice() {
setTimeout(function(){
jQuery.noConflict().ajax({
url: '<?php echo( esc_url( get_rest_url() ) ); ?>pmpro/v1/checkout_levels',
dataType: 'json',
data: jQuery( "#pmpro_form" ).serialize(),
success: function(data) {
console.log(data);
if ( data.hasOwnProperty('initial_payment_formatted') ) {
jQuery('#pmpro_live_price').html( "Total: " + data.initial_payment_formatted );
}
}
});
}, 500);
}
pmproUpdateLivePrice();
jQuery(".pmpro_alter_price").change(function(){
pmproUpdateLivePrice();
});
});
</script>
<?php
return $param;
}
add_action( 'pmpro_include_cardtype_field', 'my_pmpro_live_price' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment