Can anyone help me with this code. I had it working at one time but due to some changes in WooCommerce it no longer works.
The code must run on order completion and update the users meta field based on how many credits they earned from the purchase.
Thanks
function custom_update_user_credits( $order_id = NULL ) {
global $woocommerce;
// Set product ids and credits
$product_id1 = 80;
$ad_credits1 = 10;
$product_id2 = 82;
$ad_credits2 = 20;
$product_id3 = 83;
$ad_credits3 = 30;
$customer_id = get_post_meta( $order_id, '_customer_user', TRUE );
$user_credit = (int) get_user_meta( $customer_id, 'wpcf-ad_credits', TRUE );
if ( $order_id && 0 != $customer_id ) :
$order = &new WC_Order( $order_id );
// Get the order items
$items = get_product( $post->ID );
// Loop through the order items looking for the right products
foreach($items as $item) :
// Credits
if ( $item['id'] == $product_id1 ) :
$credit_total = $user_credit + $ad_credits1;
elseif ( $item['id'] == $product_id2 ) :
$credit_total = $user_credit + $ad_credits2;
elseif ( $item['id'] == $product_id3 ) :
$credit_total = $user_credit + $ad_credits3;
update_user_meta( $customer_id, 'wpcf-ad_credits', $credit_total, $user_credit );
endif;
endforeach;
endif;
}
add_action( 'woocommerce_order_status_completed', 'custom_update_user_credits' );