Kolawole Emmanuel Izzy
Kolawole Emmanuel Izzy

Reputation: 1052

Completely hide products from unauthorized users in WooCommerce

I'm trying to remove a product/products completely from Users that are not logged in and if user is not specific role (e.g Verified Buyer).

I have been able to create a new role called Verified Buyer using the code below;

add_role(
    'verified_buyer',
    __( 'Verified Buyer', 'text-domain' ),
    array(
       'read'         => true,  
        'edit_posts'   => false,
    )
);
//This Role is same role capability as the WooCommerce Customer role

and i have as well added a checkbox to the WooCommerce Add New Product page using the code below

function hide_product_from_unathorised_users() {
  $args = array(
    'id' => '_hide_from_unauthorize_users',
    'label' => 'Hide Product from unauthorized users',
    'desc_tip' => true,
    'description' => __('Check this to hide this product from unauthorized users', 'text-domain')
  );
  woocommerce_wp_checkbox( $args );
}

add_action( 'woocommerce_product_options_advanced', 'hide_product_from_unathorised_users' );

// Save Fields
function product_custom_fields_save($post_id){
    // Custom Product Text Field
    $hide_product_unathorised_users = isset( $_POST['_hide_from_unauthorize_users'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_hide_from_unauthorize_users', esc_attr( $hide_product_unathorised_users ));
}
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');

Now that i have this two options (user role and a checkbox to know which product to hide) ... I want to Hide such product if the following conditions are met;

HIDE PRODUCT COMPLETELY (Even from search Queries) IF;
1. Checkbox is checked on product & User is not logged in
2. Checkbox is checked on product & User logged in & not Verified Buyer or admin Role

Like this

function hide_product_completely_conditionally() {

global $post;

$hide_product_checkbox = get_post_meta( $post->ID, '_hide_from_unauthorize_users', true )

$user = wp_get_current_user();
$authorized_user_role = in_array( 'verified_buyer', (array) $user->roles );
$admin_role = in_array( 'administrator', (array) $user->roles );

    if ( ($hide_product_checkbox == 'yes' && !is_user_loggedin()) || ($hide_product_checkbox == 'yes' && is_user_loggedin() && (!$authorized_user_role || !$admin_role) ) ) {

     //(...) HIDE SUCH PRODUCT COMPLETELY CODE THAT I'M NOT SURE HOW TO WRITE

    }
}

Thanks for your help in advance.

Upvotes: 1

Views: 1109

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253867

The following will code will filter products based on a your custom product field when users are not allowed (and will redirect them to shop page if they try to access manually to a protected product).

// Conditional function checking for authorized users
function is_authorized_user(){
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $caps = $user->allcaps;

        if ( ( isset($caps['edit_product']) && $caps['edit_product'] )
        || in_array( 'verified_buyer', $user->roles ) ) return true;
    }
    else return false;
}

// Filter product query (and search) from unauthorized users
add_filter( 'woocommerce_product_query_meta_query', 'only_authorized_users_meta_query', 10, 2 );
function only_authorized_users_meta_query( $meta_query, $query ) {
    // Hide specific products from unauthorized users
    if( ! is_authorized_user() && ! is_admin() ) {
        $meta_query['relation'] = 'OR';
        $meta_query[] = array(
            'key'     => '_hide_from_unauthorize_users',
            'value'   => 'no',
            'compare' => '='
        );
        $meta_query[] = array(
            'key'     => '_hide_from_unauthorize_users',
            'compare' => 'NOT EXISTS'
        );
    }
    return $meta_query;
}

// Security: Redirect unauthorized users if accessing prodtected products
add_action( 'template_redirect', 'only_authorized_users_redirect' );
function only_authorized_users_redirect() {
    // Specific products redirect for unauthorized users (to be sure)
    if( is_product() && ! is_authorized_user()
    && get_post_meta( get_the_id(), '_hide_from_unauthorize_users', true ) === 'yes' ) {
        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
        exit;
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Instead of using an additional user role, you can:

1) Use use the WC_Customer is_paying_customer boolean property like:

 if( WC()->customer->get_is_paying_customer() ) {
     // Is a playing customer
 } else {
     // NOT a playing customer
 }

2) Or adding custom user meta as:

 update_user_meta( get_current_user_id(), 'allowed_customer', '1' );

Then you will check using:

 if( get_user_meta( get_current_user_id(), 'allowed_customer', true ) ) {
     // Allowed customer
 } else {
     // NOT Allowed customer
 }

Upvotes: 2

filipecsweb
filipecsweb

Reputation: 181

When you say "Remove" I'm assuming you are actually trying to hide the product.

So, using pre_get_posts action hook is your way to go.

The code below will hide any product which has its field _hide_from_unauthorize_users set to yes from users that are not logged in and from users that are logged in but are not a verified_buyer nor an administrator.

Put the snippet below in your functions.php file and pay attention to the comments:

<?php

/**
 * @param WP_Query $query
 */
function _hide_products_from_certain_users( $query ) {
    if ( is_admin() ) {
        return;
    }

    /**
     * Create the query which will make sure only products that are allowed to bee seen will show up.
     */
    $meta_query[] = array(
        'key'     => '_hide_from_unauthorize_users',
        'value'   => 'yes',
        'compare' => '!=',
    );

    $user = wp_get_current_user();

    // If user is not logged in.
    if ( ! is_user_logged_in() ) {
        $query->set( 'meta_query', $meta_query );
    } else {
        $authorized_user_role = in_array( 'verified_buyer', (array) $user->roles );
        $admin_role           = in_array( 'administrator', (array) $user->roles );

        // If the current user is not a verified_buyer nor an administrator.
        if ( ! $authorized_user_role && ! $admin_role ) {
            $query->set( 'meta_query', $meta_query );
        }
    }
}

add_action( 'pre_get_posts', '_hide_products_from_certain_users' );

Btw, you had a few syntax mistakes in your code, I fixed them.

Upvotes: 0

Related Questions