Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created January 4, 2021 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/883452f13b96a7427378974a36308053 to your computer and use it in GitHub Desktop.
Save andrewlimaza/883452f13b96a7427378974a36308053 to your computer and use it in GitHub Desktop.
Update user meta on subscription payment completed for Paid Memberships Pro.
<?php
/**
* Update user meta when a recurring payment occurs and an order is created inside Paid Memberships Pro.
* Follow this guide to add code to your WordPress site - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* Hook reference: https://www.paidmembershipspro.com/hook/pmpro_subscription_payment_completed/
*/
function my_change_meta_on_payment_received( $morder ) {
update_user_meta( $morder->user_id, 'meta_key', 'my value' );
}
add_action( 'pmpro_subscription_payment_completed', 'my_change_meta_on_payment_received', 10, 1 );
@kylehi2222
Copy link

Hi there,

Thanks for this Andrew. Do you think you could take a moment to check my code:

`function my_change_meta_on_payment_received( $morder ) {

$membership_id = $morder->membership_level->id;
$startdatedb = apply_filters( "pmpro_checkout_start_date", "'" . current_time( 'mysql' ) . "'", $morder->user_id, $morder->membership_level );
$membership_startdate = date( 'Y-m-d H:i:s', $startdatedb );

if ($membership_id == '1')  {
	$startdate = strtotime($membership_startdate);
	$endDate = date("Y-m-d H:i:s", strtotime("+1 month", $startdate));
} elseif ($membership_id == '2') {
	$startdate = strtotime($membership_startdate);
	$endDate = date("Y-m-d H:i:s", strtotime("+1 year", $startdate));
}
$timezone = 'GMT';
$enddategmt = $endDate . ' ' . $timezone;

update_user_meta( $morder->user_id, 'subscription', '$membership_id' );
update_user_meta( $morder->user_id, 'subscriptionEndDate', '$enddategmt' );

}
add_action( 'pmpro_subscription_payment_completed', 'my_change_meta_on_payment_received', 10, 1 );
`

@andrewlimaza
Copy link
Author

Hi @kylehi2222,

This looks correct to me. 👍

@Clautrade
Copy link

Clautrade commented Jul 3, 2021

Hello Andrew, many thanks for your post.
I'm trying to use it but unfortunately I get 2 issues and I hope to fix it...

  1. It seems not work with recurring payments.
    Paypal (by IPN) send: txn_type=recurring_payment_profile_created"
    In the file ... wp-content/plugins/paid-memberships-pro/services/ipnhandler.php at LINE 61 it's written:
"PayPal Express
	- we will get txn_type express_checkout, or recurring_payment_profile_created, or ..."

I cannot find such rule/action about recurring_payment_profile_created, I think that this is why it doesn't work.


  1. As the action of this hook is: "Update user meta on subscription payment completed for Paid Memberships Pro." I'm using it to Update user meta.
    The only MAIL seeded to user after payment is the INVOICE MAIL, I'm adding info about the User Meta updated by the Hook to inform the user after payment. Unfortunately it does not work fine as it send the data BEFORE the update.

In the LINE 798 of the file: wp-content/plugins/paid-memberships-pro/services/ipnhandler.php there is the code:


`		//email the user their invoice
		$pmproemail = new PMProEmail();
		$pmproemail->sendInvoiceEmail( get_userdata( $last_order->user_id ), $morder );

		//hook for successful subscription payments
		do_action( "pmpro_subscription_payment_completed", $morder );


Why the HOOK is placed after the INVOICE MAIL?


Could you help me to understand and suggest a way to adjust these 2 questions please?
Many thanks. Best Regards
`

@Poonam-thakur-fluidlabs
Copy link

Poonam-thakur-fluidlabs commented Apr 15, 2023

Hi

I am also working on the same concept.

So Every time when recurring payment is made, I want to update the user meta data. I tried your solution . buts its not working for me.

Can you help me to resolve this , here is my code

function my_change_meta_on_payment_received( $morder ) {
$membership_id = $morder->membership_level->id;
$pmpro_membership_post_limit1 = get_pmpro_membership_level_meta(
$membership_id,
"pmpro_membership_post_limit",
true
);
update_user_meta( $morder->user_id, 'product_limit', $pmpro_membership_post_limit1 );
}
add_action( 'pmpro_subscription_payment_completed', 'my_change_meta_on_payment_received', 10, 1 );

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