// Optional: Show an admin notice so maintainers know the sync is active
add_action( 'admin_notices', function() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Only show on plugins or membership pages
$screen = get_current_screen();
if ( $screen && in_array( $screen->id, array( 'plugins', 'memberpress_page_pmpro-membershipLevels', 'surecart_page_sc-products' ) ) ) {
echo '
EcoPulse Notice: Membership synchronization between SureCart and PMPro is actively managed by the system MU-plugin (`ecopulse-sync.php`).
';
}
});error_log( '--- MU-PLUGIN TEST FIRED: ' . date('Y-m-d H:i:s') . ' ---' );
/**
* EcoPulse Alliance: SureCart to PMPro Membership Sync
* Snippet runs everywhere.
*/
add_action( 'surecart/purchase_created', 'ecopulse_sync_surecart_to_pmpro', 10, 1 );
function ecopulse_sync_surecart_to_pmpro( $purchase ) {
error_log( '--- SURECART PURCHASE CREATED HOOK FIRED ---' );
// 1. Map SureCart Product IDs to PMPro level IDs
$product_to_level_map = array(
'52520878-fe9b-4843-a769-ebad34361aa0' => 1, // Moja
'7469e53a-2a68-43b3-a7de-cb9c1c4375c3' => 2, // Mbili
'e3392752-a39d-4b3e-ae89-0ab75605947c' => 3, // Tatu
'020e5997-57dd-4064-98ef-9f5176f8c8b6' => 4, // Nne
'1eb33803-aa25-4229-b239-3874692ec22e' => 5, // Tano
);
// 2. Extract Product ID using SureCart's native structure
$product_id = null;
if ( is_object( $purchase ) ) {
$product_id = is_object( $purchase->product ) ? $purchase->product->id : $purchase->product;
} elseif ( is_array( $purchase ) ) {
$product_id = $purchase['product'] ?? null;
}
if ( ! $product_id || ! isset( $product_to_level_map[ $product_id ] ) ) {
error_log( 'SureCart Sync Skipped: Product ID not mapped or found. Captured ID: ' . print_r( $product_id, true ) );
return;
}
$pmpro_level_id = $product_to_level_map[ $product_id ];
// 3. Get WordPress User object using SureCart's native getWPUser() method
$user = null;
if ( is_object( $purchase ) && method_exists( $purchase, 'getWPUser' ) ) {
$user = $purchase->getWPUser();
}
// Fallback: search user by customer email if getWPUser() returns empty
if ( empty( $user ) && ! empty( $purchase->customer ) ) {
$customer_email = is_object( $purchase->customer ) ? ( $purchase->customer->email ?? null ) : ( $purchase['customer']['email'] ?? null );
if ( $customer_email ) {
$user = get_user_by( 'email', $customer_email );
}
}
if ( empty( $user ) || empty( $user->ID ) ) {
error_log( 'SureCart Sync Failed: No valid WordPress user object found.' );
return;
}
$user_id = $user->ID;
if ( ! function_exists( 'pmpro_changeMembershipLevel' ) ) {
error_log( 'SureCart Sync Failed: Paid Memberships Pro plugin is not active.' );
return;
}
// 4. Calculate Expiration Dates (Smart Early Renewal Logic)
$now_timestamp = current_time( 'timestamp' );
$start_date = date( 'Y-m-d H:i:s', $now_timestamp );
$end_date = date( 'Y-m-d H:i:s', strtotime( '+1 month', $now_timestamp ) );
// Extend by +1 month if member already has an unexpired level
$current_level = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_level && (int) $current_level->id === (int) $pmpro_level_id ) {
if ( ! empty( $current_level->enddate ) && $current_level->enddate > current_time( 'mysql' ) ) {
$existing_end_timestamp = strtotime( $current_level->enddate );
$end_date = date( 'Y-m-d H:i:s', strtotime( '+1 month', $existing_end_timestamp ) );
}
}
// 5. Assign Level in PMPro
$custom_level = array(
'user_id' => $user_id,
'membership_id' => $pmpro_level_id,
'code_id' => 0,
'initial_payment' => 0,
'billing_amount' => 0,
'cycle_number' => 0,
'cycle_period' => '',
'billing_limit' => 0,
'trial_amount' => 0,
'trial_limit' => 0,
'startdate' => $start_date,
'enddate' => $end_date,
);
$status = pmpro_changeMembershipLevel( $custom_level, $user_id );
error_log( "PMPro Level $pmpro_level_id assigned to User ID $user_id. Status: " . var_export( $status, true ) );
}
Warning: Cannot modify header information - headers already sent by (output started at /home/roriamac/public_html/wp-content/mu-plugins/admin_notice.php:1) in /home/roriamac/public_html/wp-includes/sitemaps/class-wp-sitemaps-renderer.php on line 126
https://roriama.com/wp-sitemap-posts-post-1.xmlhttps://roriama.com/wp-sitemap-posts-page-1.xmlhttps://roriama.com/wp-sitemap-taxonomies-category-1.xmlhttps://roriama.com/wp-sitemap-users-1.xml