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

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

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

下面的代码已经粘贴到我主题中的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帖子,但该帖子大约有2年的历史,因此各种Woocommerce设置可能已经改变 - 因为它可能是目前无效的原因。

php wordpress woocommerce account woocommerce-subscriptions
1个回答
1
投票

你正在使用的钩子似乎不再存在。请尝试使用以下更简单的代码:

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文件中。它应该有效。

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