Question

I want to add 2 membership statuses to my WordPress WooCommerce based website. The first one would be Basic package - user gets a custom message at the top saying he has to make 2 purchases before he can get access to the rest of the page and the second package would be Premium package - user gets access to the whole page, only if he has made 2 purchases on the site.

How can I achieve this?

Was it helpful?

Solution

I think you should be looking for some kind of WooCommerce membership plugin, e.g. premium extension WooCommerce Membership or free plugin Groups that is available on wordpress.org. This would provide you with access control and the first one would also give you some additional WooCommerce-related features.

Neither of the above plugins, however, offer the solution that you are looking for out of the box and I am not aware of any plugins that would. You would need to write custom code that tracks how many orders user has placed to this date. This could be run when orders are marked completed. Then simply instruct the membership plugin to upgrade user's membership level (both of them have APIs to do so I guess).

This piece of code can be used to count number of customer's orders:

$user_id = 1; // Change to take your real user ID dynamically

$args = array(
    'numberposts'   => -1,
    'meta_key'      => '_customer_user',
    'meta_value'    => $user_id,
    'post_type'     => 'shop_order',
    'post_status'   => 'publish',
    'tax_query' => array(
        array(
            'taxonomy'  => 'shop_order_status',
            'field'     => 'slug',
            'terms'     => 'completed',
        ),
    ),
);

$posts = get_posts($args);

$number_of_orders_to_date = count($posts); // This is your answer
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top