Skip to content

BUG FIX: Add null check for $pmpro_level in checkout events#9

Open
flintfromthebasement wants to merge 1 commit intostrangerstudios:devfrom
flintfromthebasement:fix/pmpro-level-null-check-checkout-events
Open

BUG FIX: Add null check for $pmpro_level in checkout events#9
flintfromthebasement wants to merge 1 commit intostrangerstudios:devfrom
flintfromthebasement:fix/pmpro-level-null-check-checkout-events

Conversation

@flintfromthebasement
Copy link

Description

Adds a null check for the global $pmpro_level variable in pmproga4_checkout_events() to prevent fatal errors when the membership level is not set.

The Bug

pmproga4_checkout_events() runs on checkout page load and immediately accesses $pmpro_level->id, $pmpro_level->name, and $pmpro_level->initial_payment without checking if the global is set. This causes a fatal error (or PHP warnings/notices) when $pmpro_level is null — which can happen in edge cases like direct page access or when another plugin interferes with the checkout flow.

The Fix

Added an early return when $pmpro_level is empty, following the same guard pattern already used in the function for pmpro_is_checkout():

// Bail if the membership level is not set.
if ( empty( $pmpro_level ) ) {
    return;
}

Testing

  1. Visit the PMPro checkout page normally with a level selected — GA4 events should still fire.
  2. Visit the checkout page without a level parameter — should no longer produce errors.

Fixes a PHP warning 'Attempt to read property on null' thrown when
$pmpro_level is not populated on the checkout page. This can happen
when bots, crawlers, or redirects hit the checkout URL without a
valid level context. Added an early return guard to prevent accessing
properties on a null object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant