How to use wc_paying_customer function to create shortcodes and display content only to paying customers

First of all, I am a beginner and don’t know php well, so please forgive my ignorance..!
I can’t figure out how to properly create a shortcode allowing me to show content to customers who have already bought a product only, using [CUSTOMER]content[/CUSTOMER] inside posts for them and [USER]other content[/USER] inside posts for others (people who have not bought a product yet).

I wanted to use the function wc_paying_customer included in woocommerce as written below:

Read More
function wc_paying_customer( $order_id ) {

$order = wc_get_order( $order_id ); 
if ( $order->user_id > 0 ) {
    update_user_meta( $order->user_id, 'paying_customer', 1 );

    $old_spent = absint( get_user_meta( $order->user_id, '_money_spent', true ) );
    update_user_meta( $order->user_id, '_money_spent', $old_spent + $order->order_total );

    $old_count = absint( get_user_meta( $order->user_id, '_order_count', true ) );
    update_user_meta( $order->user_id, '_order_count', $old_count + 1 );
 }
}

Links : http://docs.woothemes.com/wc-apidocs/function-wc_paying_customer.html and http://docs.woothemes.com/wc-apidocs/source-function-wc_paying_customer.html#192-212

So I added this in my functions php :

add_shortcode('CUSTOMER','check_customer');
function check_customer($atts,$content=""){
if (wc_paying_customer()){
return do_shortcode($content);
    }
}
add_shortcode('USER','check_user');
function check_user($atts,$content=""){
if (!wc_paying_customer()){
    return do_shortcode($content);
    }
}

But it does not seem to work and I don’t understand why (again, I’m totally new at this – first stackoverflow question – so I probably made a mistake or forgot something but I need some help to figure it out).

Thank you !

EDIT: I used the answer provided by Diggy below, and it worked beautifully. Many thanks 🙂
If someone else wants to use this, here is what should be added to your custom functions php file:

function so27296867_is_paying_customer()
{
$user_id = get_current_user_id();

if( '' != get_user_meta( $user_id, 'paying_customer', true ) )
    return true;

return false;
}
add_shortcode('CUSTOMER','check_customer');
function check_customer($atts,$content=""){
if( so27296867_is_paying_customer() ){
return do_shortcode($content);
}
}
add_shortcode('USER','check_user');
function check_user($atts,$content=""){
if( !so27296867_is_paying_customer() ){
return do_shortcode($content);
}
}

Then inside your page content display content for paying customers using the [CUSTOMER][/CUSTOMER] shortcodes and [USER][/USER] for others. I found the necessity for this as I allow people to create an account before buying anything, but need them to have purchased a product to access other sections inside their account when loggedin.

Thanks again to diggy for providing such a great fix !

Related posts

Leave a Reply

1 comment

  1. You can check the current user’s paying_customer user meta field with following function:

    function so27296867_is_paying_customer()
    {
        $user_id = get_current_user_id();
    
        if( '' != get_user_meta( $user_id, 'paying_customer', true ) )
            return true;
    
        return false;
    }
    

    and use the function in a conditional check:

    if( so27296867_is_paying_customer() ){
        // current user is paying customer, do stuff
    }