WooCommerce Subscriptions with Account Funds payme

2019-06-10 09:14发布

I'm running WooCommerce with Subscriptions and Account Funds plugins.

I'm needing to add funds to a user's profile every time a subscription payment is processed.

WooCommerce Subscriptions has the processed_subscription_payment action to hook into.

Account Funds creates a user meta field called account_funds.

Here is the code I've come up with so far, but it doesn't seem to be working. I'm using PayPal Sandbox to test it, but I think they're having problems right now. Either that or my code is bad.

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

function custom_process_order($order_id) {

    global $woocommerce;
    $order = new WC_Order( $order_id );

    $myuser_id = (int)$order->user_id;

    $amount = $order->get_order_total();
    $funds = get_user_meta( $myuser_id, 'account_funds', true );
    $funds = $funds ? $funds : 0;
    $funds += floatval( $amount );
    update_user_meta( $myuser_id, 'account_funds', $funds );

}

I'm trying to pull the user's ID from each processed subscription payment, then add the funds to their account.

Here's the Account Funds file I referenced to help create my function: http://pastebin.com/Teq8AXz8

And here's the Subscriptions documentation I'm referencing: http://docs.woothemes.com/document/subscriptions/develop/action-reference/

What do I seem to be doing wrong?

2条回答
狗以群分
2楼-- · 2019-06-10 09:57

@helgatheviking helped get me extremely close. The only thing that wouldn't work was get_order_total() and WC_Account_Funds::add_funds($customer_id, $amount).

Here's what ended up working for me:

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

}

Thanks @helgatheviking!

查看更多
来,给爷笑一个
3楼-- · 2019-06-10 10:16

The $subscription_key is a unique identifier that is made up of the subscription’s product ID and the ID of the order in which the subscription purchased. Therefore you can split that string into 2 useful variables. Untested, but try the following:

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

function custom_process_order( $user_id, $subscription_key ) {

    if( class_exists( 'WC_Account_Funds' ) ){

        // 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 = floatval( $order->get_total() );

        // alternatively get product price
        // $product = wc_get_product( $product_id );
        // $amount = $product->get_price();

        // add account funds
        WC_Account_Funds::add_funds( $user_id, $amount );
    }

}
查看更多
登录 后发表回答