Created
June 2, 2015 16:44
-
-
Save messica/f59d0eeba5f3393476cd to your computer and use it in GitHub Desktop.
Force Add PayPal Express + Pay by Check addons to work together
This file contains hidden or 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 | |
| /* | |
| * Force Add PayPal Express + Pay by Check addons to work together | |
| */ | |
| function my_init() { | |
| remove_action("pmpro_checkout_boxes", "pmpropbc_checkout_boxes"); | |
| remove_action("pmpro_checkout_boxes", "pmproappe_pmpro_checkout_boxes", 20); | |
| } | |
| add_action('init', 'my_init'); | |
| function my_pmpro_checkout_boxes() | |
| { | |
| global $pmpro_requirebilling, $gateway, $pmpro_review; | |
| //get primary gateway | |
| $setting_gateway = get_option("pmpro_gateway"); | |
| //only show this if we're not reviewing, and not on paypal or check | |
| if (empty($pmpro_review) && $setting_gateway != 'paypal' && $setting_gateway !== 'check') { | |
| ?> | |
| <table id="pmpro_payment_method" class="pmpro_checkout top1em" width="100%" cellpadding="0" cellspacing="0" | |
| border="0" <?php if (!$pmpro_requirebilling) { ?>style="display: none;"<?php } ?>> | |
| <thead> | |
| <tr> | |
| <th><?php _e('Choose your Payment Method', 'pmpro'); ?></th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <td> | |
| <div> | |
| <input type="radio" name="gateway" value="<?php echo esc_attr($setting_gateway); ?>" | |
| <?php if (!$gateway || $gateway == $setting_gateway) { ?>checked="checked"<?php } ?> /> | |
| <a href="javascript:void(0);" | |
| class="pmpro_radio"><?php _e('Check Out with a Credit Card Here', 'pmpro'); ?></a> | |
| <input type="radio" name="gateway" value="paypalexpress" | |
| <?php if ($gateway == "paypalexpress") { ?>checked="checked"<?php } ?> /> | |
| <a href="javascript:void(0);" | |
| class="pmpro_radio"><?php _e('Check Out with PayPal', 'pmpro'); ?></a> | |
| <input type="radio" name="gateway" value="check" | |
| <?php if ($gateway == "check") { ?>checked="checked"<?php } ?> /> | |
| <a href="javascript:void(0);" class="pmpro_radio"><?php _e('Pay by Check', 'pmpropbc'); ?></a> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <?php //here we draw the PayPal Express button, which gets moved in place by JavaScript ?> | |
| <span id="pmpro_paypalexpress_checkout" style="display: none;"> | |
| <input type="hidden" name="submit-checkout" value="1"/> | |
| <input type="image" value="<?php _e('Check Out with PayPal', 'pmpro'); ?> »" | |
| src="<?php echo apply_filters("pmpro_paypal_button_image", "https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"); ?>"/> | |
| </span> | |
| <script> | |
| var pmpro_require_billing = <?php if($pmpro_requirebilling) echo "true"; else echo "false";?>; | |
| //choosing payment method | |
| jQuery(document).ready(function () { | |
| //move paypal express button into submit box | |
| jQuery('#pmpro_paypalexpress_checkout').appendTo('div.pmpro_submit'); | |
| function showPayPalExpressCheckout() { | |
| jQuery('#pmpro_billing_address_fields').hide(); | |
| jQuery('#pmpro_payment_information_fields').hide(); | |
| jQuery('#pmpro_submit_span').hide(); | |
| jQuery('#pmpro_paypalexpress_checkout').show(); | |
| pmpro_require_billing = false; | |
| } | |
| function showCreditCardCheckout() { | |
| jQuery('#pmpro_paypalexpress_checkout').hide(); | |
| jQuery('#pmpro_billing_address_fields').show(); | |
| jQuery('#pmpro_payment_information_fields').show(); | |
| jQuery('#pmpro_submit_span').show(); | |
| pmpro_require_billing = true; | |
| } | |
| function showFreeCheckout() { | |
| jQuery('#pmpro_billing_address_fields').hide(); | |
| jQuery('#pmpro_payment_information_fields').hide(); | |
| jQuery('#pmpro_submit_span').show(); | |
| jQuery('#pmpro_paypalexpress_checkout').hide(); | |
| pmpro_require_billing = false; | |
| } | |
| //detect gateway change | |
| jQuery('input[name=gateway]').click(function () { | |
| if (jQuery(this).val() == 'paypalexpress') { | |
| showPayPalExpressCheckout(); | |
| } else if (jQuery(this).val() == 'check') { | |
| showFreeCheckout(); | |
| } else { | |
| showCreditCardCheckout(); | |
| } | |
| }); | |
| //update radio on page load | |
| if (jQuery('input[name=gateway]:checked').val() != 'paypalexpress' && pmpro_require_billing == true) { | |
| showCreditCardCheckout(); | |
| } | |
| else if (pmpro_require_billing == true) { | |
| showPayPalExpressCheckout(); | |
| } | |
| else { | |
| showFreeCheckout(); | |
| } | |
| //select the radio button if the label is clicked on | |
| jQuery('a.pmpro_radio').click(function () { | |
| jQuery(this).prev().click(); | |
| }) | |
| //every couple seconds, hide the payment method box if the level is free | |
| function togglePaymentMethodBox() | |
| { | |
| if (typeof code_level !== 'undefined') | |
| { | |
| if(parseFloat(code_level.billing_amount) > 0 || parseFloat(code_level.initial_payent) > 0) | |
| { | |
| //not free | |
| jQuery('#pmpro_payment_method').show(); | |
| } | |
| else | |
| { | |
| //free | |
| jQuery('#pmpro_payment_method').hide(); | |
| } | |
| } | |
| pmpro_toggle_payment_method_box_timer = setTimeout(function(){togglePaymentMethodBox();}, 200); | |
| } | |
| togglePaymentMethodBox(); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| add_action('pmpro_checkout_boxes', 'my_pmpro_checkout_boxes'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment