Last active
February 29, 2024 18:06
-
-
Save dparker1005/572a25071c5cbf68bcdb3841f3e6546a to your computer and use it in GitHub Desktop.
2 demos to demonstrate the PMPro checkout_levels API call.
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
<?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_level', | |
dataType: 'json', | |
data: pmpro_getCheckoutFormDataForCheckoutLevels(), | |
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_level', | |
dataType: 'json', | |
data: pmpro_getCheckoutFormDataForCheckoutLevels(), | |
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
the
my_pmpro_live_price
function is not working anymore. not even loading at the checkout. Has any of the hooks changed?