Home › Forums › Stripe Payments Plugin › I need to hook into the cancel subscription function
Tagged: cancellation hook
- This topic has 8 replies, 4 voices, and was last updated 2 years, 4 months ago by benbois.
-
AuthorPosts
-
August 31, 2021 at 9:37 am #4234davidkochgParticipant
Hi S plugin
Is there a way to add my own function when a subscription is chancelled?
BR,
JesperAugust 31, 2021 at 10:54 am #4235alexanderfoxcParticipantHi Jesper.
There are two hooks related to subscription deletion –
asp_subscription_ended
andasp_subscription_canceled
. The latter is what you need. Here’s how you can hook to it:add_action ('asp_subscription_canceled', 'asp_subscription_canceled_handler', 10, 2); function asp_subscription_canceled_handler ($sub_id, $event_data) { ASP_Debug_Logger::log(json_encode($event_data)); }
$sub_id
is Stripe’s subscription ID
$event_data
is event data received from Stripe’s hook.This code dumps event data into plugin’s debug log (if it’s enabled). This way you would be able to see what kind of data is available.
To trigger the event, just create a subscription in test mode and then cancel it using cancel URL.
July 12, 2022 at 9:00 am #4617benboisParticipantHi,
I inserted your sample code in the functions.php file (child theme) but it is not fired after a cancellation, either by using a cancellation link (https://domain.com/?asp_sub_action=cancel&sub_token=XXXXX) or by clicking on the Cancel Subscription button (admin).
Right now, only tested with Stripe Test Mode.Any idea?
July 12, 2022 at 9:34 am #4618benboisParticipantWell, apparently the usage of this hook has been limited to WP-Members Membership Plugin.
Can you confirm?!July 13, 2022 at 3:57 am #4621AdminKeymasterWe are not familiar with that particular plugin you mentioned but yes there are some plugins out there which can create a conflict. So the best option is to do the following test:
Test for Plugin and Theme Conflict Before Posting an Issue or a Bug
July 14, 2022 at 6:39 am #4623benboisParticipantStrange, after disable all plugins & gone back to a standard theme (Sketch), the hook still refuse to fired!
The hook works when added inside your code (after the hook for eMember plugin) but not outside of the plugin, in functions.php file for example?!July 15, 2022 at 4:41 am #4628AdminKeymasterThis particular hook is triggered by a Stripe hook. It runs early on the loading process so this code needs to be put into a place that is visible much earlier. If you are putting the code into a file that is loading AFTER the hook is being triggered, then you will see the behavior that you are seeing. Not all hooks are triggered at the same time. Different hooks have different conditions. You may need to get help from a WordPress coder for this.
You should probably make a small custom plugin and put this custom code in that plugin so it is visible when that hook is triggered. It’s generally a good idea to put all your customization code into a separate plugin. Otherwise in the future if you switch your theme, you will lose all the customization.
July 15, 2022 at 6:30 am #4629benboisParticipantThanks for your reply.
I’ll try out in a separate plugin.July 15, 2022 at 8:07 am #4630benboisParticipantThanks, it works perfectly in a plugin.
That was the key, as mentioned above, to trigger earlier with this hook!Problem solved!
-
AuthorPosts
- You must be logged in to reply to this topic.