WooCommerce订阅 - 续订时未触发动作挂钩

问题描述 投票:1回答:2

我已经制定了一项自定义功能,可以在订阅付款成功时将帐户资金(40英镑)添加到用户的帐户中。

我遇到的问题是挂钩似乎没有触发,当续费发生时,资金没有添加到账户。

我在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网址是Fatal Errored / exhausted

php wordpress woocommerce subscriptions hook-woocommerce
2个回答
0
投票

您应该使用此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文件中,或者也可以放在任何插件文件中。


-1
投票

每次付款完成时,woocommerce_subscription_payment_complete挂钩都会触发,因此新的订阅付款和续订都会导致付款。

我使用以下代码解决此问题...

add_action('woocommerce_subscription_payment_complete','my_function');
function my_function($subscription) {

    $last_order = $subscription->get_last_order( 'all', 'any' );

    if ( wcs_order_contains_renewal( $last_order ) ) {
        return;
    } else {

        // 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 );
   }

}

希望这有助于某人

© www.soinside.com 2019 - 2024. All rights reserved.