@justin,
I just made an improvement to your code that addresses this problem (still wonder why Woocommerce makes the admin or shop manager the author of the order when he’s the one who completes it – still wonder why also there is no customer id in the order object :
in /includes/functions.php :
/**
* [add_credits_to_user_account description]
* @param [type] $order_id [description]
*
* @since 1.1 - Now fired on woocommerce_order_status_completed action. This is a * change to rid infinite reloads
* of credits after pruchase.
*
*Changed by Charles Senard on October, 2015, 17. The customer is credited with the *amount of credits he has bought, vs. before where it was the admin in some cases (when the admin performed an action on the order)
*/
add_action( 'woocommerce_order_status_completed', 'add_credits_to_user_account' );
function add_credits_to_user_account ( $order_id )
{
write_log( "function add_credits_to_user_account");
$order = new WC_Order( $order_id );
$customer_id = get_post_meta( $order->id , '_customer_user', true );
if ( count( $order->get_items() ) > 0 )
{
foreach ( $order->get_items() as $item )
{
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$credit_amount = floatval( get_post_meta( $product_id, "_credits_amount", true ) );
$current_users_wallet_ballance = floatval( get_user_meta( $customer_id, "_uw_balance", true ) );
$la62_success = update_user_meta( $customer_id, "_uw_balance", ( $credit_amount + $current_users_wallet_ballance ) );
}
}
}
/**
* [custom_woocommerce_auto_complete_order description]
* @param [type] $order_id [description]
* @return [type] [description]
*
* Changed by Charles Senard on 2015, October 17th
* Action deactivated - We may plan to delete it but remains for means of non -
* regression tests
*/
//add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id )
{
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
if ( count( $order->get_items() ) > 0 )
{
foreach ( $order->get_items() as $item )
{
if(has_term('credit', 'product_cat', $item['product_id']))
$order->update_status( 'completed' );
}
}
}
Hey @justin,
How about sharing this plugin on Github, so that we can make it better together?
@ reminute2008,
if this piece of code solved your problem – I think you should change directly in the plugin – , you might mark this thread as “resolved”. Thanks ??
Have a good day,
Charles