php - WooCommerce Subscriptions - Action hook not triggered on renewal -
i have made custom function adds account funds (£40) user's account when subscription payment successful.
the problem have hook doesn't seem trigger, when renewell happens funds not added account.
i enabled debugging in woocommerce , pushed renewal manually within cron management, when function works , funds added account.
here function (functions.php);
add_action('processed_subscription_payment', 'custom_add_funds', 10, 2); function custom_add_funds($user_id) { // current user's funds $funds = get_user_meta( $user_id, 'account_funds', true ); // add £40 $funds = $funds + 40.00; // add funds user update_user_meta( $user_id, 'account_funds', $funds ); }
----- solved -----
i needed memory limit on wordpress, ipn url fatal errored/exhausted
you should try different approach using 2 different hooks (and $subscription
object representing subscription has received payment):
- the first hook triggered when payment made on subscription. can payment initial order, switch order or renewal order.
- the second hook triggered when renewal payment made on subscription.
this snippet (with code in it):
add_action('woocommerce_subscription_payment_complete', 'custom_add_funds', 10, 1); add_action('woocommerce_subscription_renewal_payment_complete', 'custom_add_funds', 10, 1); function custom_add_funds($subscription) { // getting user id current subscription object $user_id = get_post_meta($subscription->id, '_customer_user', true); // current user's funds $funds = get_user_meta( $user_id, 'account_funds', true ); // add £40 $funds += 40; // update funds of user new value update_user_meta( $user_id, 'account_funds', $funds ); }
this should work, it's untested, not sure, if it's based on other answers have maid.
this code goes in function.php file of active child theme (or theme) or in plugin file.
Comments
Post a Comment