Created
February 24, 2017 02:13
-
-
Save WPprodigy/269dfe0628965c32a4453215e17a1195 to your computer and use it in GitHub Desktop.
Check to add account funds at the processing order status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_order_status_processing', 'wc_ninja_add_funds_at_processing', 5 ); | |
function wc_ninja_add_funds_at_processing( $order_id ) { | |
if ( method_exists( 'WC_Account_Funds', 'add_funds' ) ) { | |
wc_ninja_maybe_increase_funds( $order_id ); | |
} | |
} | |
function wc_ninja_maybe_increase_funds( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
$items = $order->get_items(); | |
$customer_id = $order->get_user_id(); | |
if ( $customer_id && ! get_post_meta( $order_id, '_funds_deposited', true ) ) { | |
foreach ( $items as $item ) { | |
$product = $order->get_product_from_item( $item ); | |
if ( ! is_a( $product, 'WC_Product' ) ) { | |
continue; | |
} | |
$funds = 0; | |
if ( $product->is_type( 'deposit' ) || $product->is_type( 'topup' ) ) { | |
$funds = $item['line_total']; | |
} else { | |
continue; | |
} | |
WC_Account_Funds::add_funds( $customer_id, $funds ); | |
$order->add_order_note( sprintf( __( 'Added %s funds to user #%d', 'woocommerce-account-funds' ), wc_price( $funds ), $customer_id ) ); | |
update_post_meta( $order_id, '_funds_deposited', 1 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment