Woocommerce 订阅和账户资金插件之间的网关

Gateway between Woocommerce Subscriptions and Account Funds plugins

我购买了 2 个插件(Woocommerce 订阅和帐户资金),它们在相关文档中声明它们彼此兼容。我希望制作一个 Simple Subscription 产品,在结帐时将产品价格添加为该用户的帐户资金,并在每次 Simple Subscription 产品续订时再次添加。

下面的代码已粘贴到我的主题中 functions.php 文件的底部,但似乎没有在购买订阅时更新帐户资金。

add_action('processed_subscription_payment', 'custom_process_order', 10, 2);

function custom_process_order($user_id, $subscription_key) {

    // split subscription key into order and product IDs
    $pieces = explode( '_', $subscription_key);
    $order_id = $pieces[0];
    $product_id = $pieces[1];

    // get order total
    $order = wc_get_order( $order_id );
    $amount = $order->get_total();

    // get current user's funds
    $funds = get_user_meta( $user_id, 'account_funds', true );
    $funds = $funds ? $funds : 0;
    $funds += floatval( $amount );

    // add funds to user
    update_user_meta( $user_id, 'account_funds', $funds );

}

谁能帮我解决这个问题?由于上面的代码来自一个很棒的 Stack Overflow post,但是那个 post 大约有 2 年的历史,因此各种 Woocommerce 设置可能已经改变了——这可能是它目前不起作用的原因.

您使用的挂钩似乎不存在了。请尝试使用以下更简单的代码:

add_action('woocommerce_subscription_payment_complete', 'action_subscription_payment_complete_callback', 10, 1);
function action_subscription_payment_complete_callback( $subscription ) {
    // Get the instance WC_Order Object for the current subscription
    $order = wc_get_order( $subscription->get_parent_id() );

    $user_id = (int) $order->get_customer_id(); // Customer ID
    $total   = (float) $order->get_total(); // Order total amount

    // Get customer existing funds (zero value if no funds found)
    $user_funds = (float) get_user_meta( $user_id, 'account_funds', true );

    // Add the order total amount to customer existing funds
    update_user_meta( $user_id, 'account_funds', $funds + $total );
}

代码继续在您的活动子主题(或活动主题)的 function.php 文件中。它应该有效。