Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. add_action('processed_subscription_payment', 'custom_process_order', 10, 2);
  2.  
  3. function custom_process_order($user_id, $subscription_key) {
  4.  
  5. // split subscription key into order and product IDs
  6. $pieces = explode( '_', $subscription_key);
  7. $order_id = $pieces[0];
  8. $product_id = $pieces[1];
  9.  
  10. // get order total
  11. $order = wc_get_order( $order_id );
  12. $amount = $order->get_total();
  13.  
  14. // get current user's funds
  15. $funds = get_user_meta( $user_id, 'account_funds', true );
  16. $funds = $funds ? $funds : 0;
  17. $funds += floatval( $amount );
  18.  
  19. // add funds to user
  20. update_user_meta( $user_id, 'account_funds', $funds );
  21.  
  22. }
  23.  
  24. add_action('woocommerce_subscription_payment_complete', 'action_subscription_payment_complete_callback', 10, 1);
  25. function action_subscription_payment_complete_callback( $subscription ) {
  26. // Get the instance WC_Order Object for the current subscription
  27. $order = wc_get_order( $subscription->get_parent_id() );
  28.  
  29. $user_id = (int) $order->get_customer_id(); // Customer ID
  30. $total = (float) $order->get_total(); // Order total amount
  31.  
  32. // Get customer existing funds (zero value if no funds found)
  33. $user_funds = (float) get_user_meta( $user_id, 'account_funds', true );
  34.  
  35. // Add the order total amount to customer existing funds
  36. update_user_meta( $user_id, 'account_funds', $funds + $total );
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement