|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Process Membership Checkout in a Modal or Popover Window |
| 4 | + * |
| 5 | + * title: Process Membership Checkout in a Modal or Popover Window. |
| 6 | + * layout: snippet |
| 7 | + * collection: checkout |
| 8 | + * category: membership-levels |
| 9 | + * link: https://www.paidmembershipspro.com/process-membership-checkout-in-a-modal-or-popover-window/ |
| 10 | + * |
| 11 | + * You can add this recipe to your site by creating a custom plugin |
| 12 | + * or using the Code Snippets plugin available for free in the WordPress repository. |
| 13 | + * Read this companion article for step-by-step directions on either method. |
| 14 | + * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Define the default level to use for the modal. |
| 19 | + */ |
| 20 | +define( 'PMPRO_MODAL_CHECKOUT_DEFAULT_LEVEL', 1 ); |
| 21 | + |
| 22 | +/** |
| 23 | + * Load the checkout preheader on every page |
| 24 | + * We won't be processing on every page, but we |
| 25 | + * want the code that sets up the level and |
| 26 | + * preloads fields from user meta. |
| 27 | + */ |
| 28 | +function pmprocm_preheader() { |
| 29 | + // Bail if PMPro is not loaded. |
| 30 | + if ( ! defined( 'PMPRO_DIR' ) ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if ( ! is_admin() ) { |
| 35 | + if ( empty( $_REQUEST['pmpro_level'] ) ) { |
| 36 | + $_REQUEST['pmpro_level'] = PMPRO_MODAL_CHECKOUT_DEFAULT_LEVEL; |
| 37 | + } |
| 38 | + require_once PMPRO_DIR . '/preheaders/checkout.php'; |
| 39 | + } |
| 40 | +} |
| 41 | +add_action( 'init', 'pmprocm_preheader' ); |
| 42 | + |
| 43 | +/** |
| 44 | + * Add the checkout page modal to every page. |
| 45 | + */ |
| 46 | +function pmprocm_content() { |
| 47 | + // If the PMPro checkout shortcode or block is present on a post/page, skip modal. |
| 48 | + $queried_object = get_queried_object(); |
| 49 | + if ( empty( $queried_object ) || |
| 50 | + empty( $queried_object->post_content ) || |
| 51 | + has_shortcode( $queried_object->post_content, 'pmpro_checkout' ) || |
| 52 | + has_block( 'pmpro/checkout-page', $queried_object->post_content ) |
| 53 | + ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + ?> |
| 57 | + <style> |
| 58 | + div.pmprocm_modal_bg { |
| 59 | + display: none; /* Hidden by default */ |
| 60 | + position: fixed; /* Stay in place */ |
| 61 | + z-index: 1; /* Sit on top */ |
| 62 | + left: 0; |
| 63 | + top: 0; |
| 64 | + width: 100%; /* Full width */ |
| 65 | + height: 100%; /* Full height */ |
| 66 | + overflow: auto; /* Enable scroll if needed */ |
| 67 | + background-color: rgb(0,0,0); /* Fallback color */ |
| 68 | + background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ |
| 69 | + } |
| 70 | + |
| 71 | + .pmprocm_modal_content { |
| 72 | + background-color: #fefefe; |
| 73 | + margin: 5% 25% auto; /* 15% from the top and centered */ |
| 74 | + padding: 40px; |
| 75 | + border: 1px solid #888; |
| 76 | + width: 50%; /* Could be more or less, depending on screen size */ |
| 77 | + } |
| 78 | + |
| 79 | + .pmprocm_modal_close { |
| 80 | + color: #aaa; |
| 81 | + float: right; |
| 82 | + font-size: 28px; |
| 83 | + font-weight: bold; |
| 84 | + margin: -35px -30px 0 0; |
| 85 | + } |
| 86 | + |
| 87 | + .pmprocm_modal_close:hover, |
| 88 | + .pmprocm_modal_close:focus { |
| 89 | + color: black; |
| 90 | + text-decoration: none; |
| 91 | + cursor: pointer; |
| 92 | + } |
| 93 | + </style> |
| 94 | + |
| 95 | + <div class="pmprocm_modal_bg"> |
| 96 | + <div class="pmprocm_modal_content"> |
| 97 | + <span class="pmprocm_modal_close">×</span> |
| 98 | + <?php |
| 99 | + $template = pmpro_loadTemplate( 'checkout', 'local', 'pages' ); |
| 100 | + echo $template; |
| 101 | + ?> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <script> |
| 106 | + jQuery(document).ready(function() { |
| 107 | + // Make sure the form submits to the checkout page |
| 108 | + jQuery('#pmpro_form').attr('action', '<?php echo esc_url( pmpro_url( 'checkout', '?pmpro_level=' . intval( $_REQUEST['pmpro_level'] ) ) ); ?>'); |
| 109 | + |
| 110 | + // Get the modal |
| 111 | + var modal = jQuery('.pmprocm_modal_bg'); |
| 112 | + |
| 113 | + // Get the button that opens the modal |
| 114 | + var btn = jQuery('.pmprocm_modal_btn'); |
| 115 | + |
| 116 | + // Get the <span> element that closes the modal |
| 117 | + var span = jQuery('.pmprocm_modal_close'); |
| 118 | + |
| 119 | + // When the user clicks on the button, open the modal |
| 120 | + btn.click(function() { |
| 121 | + modal.show(); |
| 122 | + }); |
| 123 | + |
| 124 | + // When the user clicks on <span> (x), close the modal |
| 125 | + span.click(function() { |
| 126 | + modal.hide(); |
| 127 | + }); |
| 128 | + |
| 129 | + // When the user clicks anywhere outside of the modal, close it |
| 130 | + modal.on('click', function(e) { |
| 131 | + if (e.target !== this) |
| 132 | + return; |
| 133 | + |
| 134 | + modal.hide(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + </script> |
| 138 | + |
| 139 | + <?php |
| 140 | +} |
| 141 | +add_action( 'wp_footer', 'pmprocm_content' ); |
0 commit comments