Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active November 17, 2020 23:16
Show Gist options
  • Save kimcoleman/6c738a9043a4b4114e51e237d0c1c7b5 to your computer and use it in GitHub Desktop.
Save kimcoleman/6c738a9043a4b4114e51e237d0c1c7b5 to your computer and use it in GitHub Desktop.
Add a radio box to the Sitewide Sale Landing Page that dynamically pulls in that sale's discount code and applicable levels.
<?php
/*
* Add a radio box to the Sitewide Sale Landing Page that dynamically pulls in that sale's discount code and
* draws a radio selection for each level the code applies to.
* Shows the modify level cost text based on the applied discount code for each level.
*/
/**
* We need to set $_REQUEST['level'] early on our
* sitewide sales pages to support other customizations.
*/
function set_request_level_on_sitewide_sales_pages() {
// Get the current page object.
$post_id = url_to_postid( $_SERVER['REQUEST_URI'] );
// Does the current page have an associated Sitewide Sale?
$sitewide_sale_id = get_post_meta( $post_id, 'pmpro_sws_sitewide_sale_id', true );
// The current page is a Sitewide Sale Landing Page.
if ( ! empty( $sitewide_sale_id ) ) {
$default_level_id = PMPro_Sitewide_Sales\includes\classes\PMPro_SWS_Landing_Pages::get_default_level( $post_id );
// If the level was passed in the URL, return that level.
if( ! empty( $_REQUEST['level'] ) ) {
return;
}
// If no level was passed in the URL, specify the default level from the Sitewide Sale.
if ( ! empty( $default_level_id ) ) {
$_REQUEST['level'] = $default_level_id;
define( 'IS_SITEWIDE_SALE', true);
}
}
}
add_action( 'init', 'set_request_level_on_sitewide_sales_pages', 1 );
/**
* Hide the PMPro pricing box at checkout and replace with our own.
*
* Requires Paid Memberships Pro v1.9.5.6+ and the Sitewide Sales Add On v1.0+
*
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function maybe_add_choose_level_section( $include_pricing_fields ) {
global $wpdb, $pmpro_level;
// Make sure Sitewide Sales Add On is active.
if ( ! class_exists( 'PMPro_Sitewide_Sales\includes\classes\PMPro_SWS_Settings' ) ) {
return $include_pricing_fields;
}
// Hide the default pricing box.
if ( PMPro_Sitewide_Sales\includes\classes\PMPro_SWS_Settings::is_active_sitewide_sale_landing_page() ) {
$include_pricing_fields = false;
// Get the current page's associated Sitewide Sale and discount code.
$sitewide_sale_id = get_post_meta( get_queried_object_id(), 'pmpro_sws_sitewide_sale_id', true );
$discount_code_id = get_post_meta( $sitewide_sale_id, 'pmpro_sws_discount_code_id', true );
$sqlQuery = $wpdb->prepare("
SELECT l.id
FROM $wpdb->pmpro_membership_levels l
LEFT JOIN $wpdb->pmpro_discount_codes_levels cl
ON l.id = cl.level_id
WHERE cl.code_id = %d",
$discount_code_id
);
$discounted_levels = $wpdb->get_results($sqlQuery);
$discounted_level_ids = array();
foreach ( $discounted_levels as $level ) {
$discounted_level_ids[] = $level->id;
}
// Show our own radio select and info.
?>
<div id="pmpro_levels_select" class="pmpro_checkout">
<h3><span class="pmpro_checkout-h3-name">Select a Level</span></h3>
<div class="pmpro_checkout-fields">
<?php
foreach ( $discounted_level_ids as $discounted_level_id ) {
$level = pmpro_getLevel( $discounted_level_id );
$sqlQuery = $wpdb->prepare("
SELECT l.id, cl.*, l.name, l.description, l.allow_signups
FROM $wpdb->pmpro_discount_codes_levels cl
LEFT JOIN $wpdb->pmpro_membership_levels l
ON cl.level_id = l.id
LEFT JOIN $wpdb->pmpro_discount_codes dc
ON dc.id = cl.code_id
WHERE dc.id = %d
AND cl.level_id = %d
LIMIT 1",
$discount_code_id,
$level->id
);
$level = $wpdb->get_row($sqlQuery);
//filter adjustments to the level
$level = apply_filters("pmpro_discount_code_level", $level, $discount_code_id);
//apply filters
$level = apply_filters("pmpro_checkout_level", $level);
?>
<div class="pmpro_checkout-field pmpro_checkout-field-radio pmpro_checkout-field-level-radio" style="cursor: pointer;">
<input id="levelradio-<?php echo $level->id; ?>" type="radio" name="level" value="<?php echo $level->id; ?>" <?php checked( $pmpro_level->id, $level->id ); ?> />
<strong class="pmpro_checkout-field-level-name"><?php echo $level->name; ?></strong>
<span class="pmpro_checkout-field-level-cost"><?php echo pmpro_getLevelCost( $level, false, true ); ?></span>
</div>
<?php } ?>
<script>
jQuery('.pmpro_checkout-field-level-radio').click(function() {
jQuery(this).addClass('selected').find("input[type='radio']").prop('checked', true);
});
</script>
</div>
</div>
<?php
}
return $include_pricing_fields;
}
add_filter( 'pmpro_include_pricing_fields', 'maybe_add_choose_level_section' );
@kimcoleman
Copy link
Author

This gist is not compatible with the Sitewide Sales plugin and will need to be updated to work with that plugin. You can use a modified version of this recipe instead: https://gist.github.com/kimcoleman/0146529d87c2950e43b23c9f4d2dfa71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment