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?
@helgatheviking helped get me extremely close. The only thing that wouldn't work was
get_order_total()
andWC_Account_Funds::add_funds($customer_id, $amount)
.Here's what ended up working for me:
Thanks @helgatheviking!
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: