Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change the level cost text of a PMPro level for an A/B test.
/*
Set a cookie when users enter a specific page.
(You would setup Google Experiments or your A/B testing software
to send 50% of visitors to this page.)
*/
function my_wp_set_cookie_for_ab_test() {
if(is_page('pricing-2')) {
setcookie('ab_test_group_2', 1, 0, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
add_action('wp', 'my_wp_set_cookie_for_ab_test');
/*
Now change the price shown if the ab_test_group_2 cookie is set.
Note that this only changes the price when displayed. It will not
adjust the actual price charged. For that, use the pmpro_checkout_level filter.
*/
function my_pmpro_level_cost_text_for_ab_test($cost, $level) {
global $pmpro_currency_symbol, $pmpro_pages;
//no cookie, we don't need to change anything
if(empty($_COOKIE) || !isset($_COOKIE['ab_test_group_2']))
return $cost;
//check for a specific level
if($level->id == 1) {
//if on the checkout page, act like the lower (real) price is a discount
//note that this assumes the currency symbol is on the left like USD
//and would need to be adjusted for your prices
if(is_page($pmpro_pages['checkout'])) {
$cost = str_replace($pmpro_currency_symbol . '100.00',
'<span class="text-formatting: strike;">' . $pmpro_currency_symbol . '200.00' . '</span> ' . $pmpro_currency_symbol . '100.00',
$cost);
} else {
//otherwise increase the price to 200 from 100
$cost = str_replace('100', '200', $cost);
}
}
return $cost;
}
add_filter('pmpro_level_cost_text', 'my_pmpro_level_cost_text_for_ab_test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.