WooCommerce订阅 - 行动挂钩没有触发续订
问题描述:
我已经做了一个自定义功能,当他们的订阅付款成功时,账户资金(£40)添加到用户的帐户。WooCommerce订阅 - 行动挂钩没有触发续订
我遇到的问题是钩子似乎没有触发,当更新发生时资金没有添加到帐户。
我启用了Woocommerce中的调试功能,并在cron管理中手动推送更新,当我这样做时,该功能可以工作并且资金被添加到帐户中。
这是我的功能(functions.php);
add_action('processed_subscription_payment', 'custom_add_funds', 10, 2);
function custom_add_funds($user_id) {
// get current user's funds
$funds = get_user_meta($user_id, 'account_funds', true);
// add £40
$funds = $funds + 40.00;
// add funds to user
update_user_meta($user_id, 'account_funds', $funds);
}
----- -----解决
我需要了WordPress的内存限制,IPN网址是致命的误码/耗尽
答
你应该尝试这种不同的方法使用此2 different hooks(和表示刚刚接收到的支付订阅的$subscription
对象):
- 订阅付款时触发第一个挂钩。这可以是初始订单,转换订单或续订订单的付款。
- 当订阅进行续订付款时,会触发第二个挂钩。
这是段(与它的代码):
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 the user ID from the current subscription object
$user_id = get_post_meta($subscription->ID, '_customer_user', true);
// get current user's funds
$funds = get_user_meta($user_id, 'account_funds', true);
// add £40
$funds += 40;
// update the funds of the user with the new value
update_user_meta($user_id, 'account_funds', $funds);
}
这应该工作,但因为它是未经检验的,我真的不知道,即使它是基于其他好的答案我有女佣。
此代码在您的活动子主题(或主题)的function.php文件或任何插件文件中。
谢谢,我已经把它放进去了,现在正在等待下一次更新。 –
这不会导致它触发两次? –
不,因为如果您阅读文档,您将看到第一个钩子是在初始订单中触发的,第二个钩子是针对每个续订支付的... – LoicTheAztec