Question

I'm basically creating a points based system for woocommerce in WordPress. This is based on usermeta that is added manually. (the idea being, people recycle products to gain points, then use points to purchase products on a seperate woocommerce that shares the user data).

I have created a checkout that disables if not enough points, or adds up the amount the user will have left after purchasing products (possibly slightly vunerable at this stage but besides the point).

THE PROBLEM i'm having is updating the user meta after the purchase has been made. i.e every user has a box called 'Points' in their user table that only admins can see - this needs to be updated with a new formula of (Current Points - Order total). Heres the code i came up with not sure how to implement this or whether this will actually work.. i planted this in the 'thankyou page' which occurs after an order has been 'placed'

        <?php 

        $user_id = wp_get_current_user();
        $pointsafterorder = $current_user->points - $woocommerce->cart->total;

        // will return false if the previous value is the same as $new_value
        update_user_meta( $user_id, $current_user->points, $pointsafterorder );

         ?>

If anyone has a fix, questions or any thoughts please let me know.

Thanks Rich

Was it helpful?

Solution

The code you have isnt getting the user id.

First you need to query the current user like what you have:

 $current_user = wp_get_current_user();

But the bit you are missing is:

 echo $current_user->ID;

So your code adpated will look like this:

$current_user = wp_get_current_user();
$pointsafterorder = $current_user->points - $woocommerce->cart->total;
update_user_meta( $current_user->ID, $current_user->points, $pointsafterorder );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top