• Resolved ciberdom

    (@ciberdom)


    Hello to all the forum;

    How can I hide a product to a certain role?
    I would like to hide a product to the “Guest” and “Customer”, this product should only be visible to the user role “Partner”

    Thanks in advance

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • WooCommerce Product Visibility by User Role plugin lets you show or hide WooCommerce products by customer’s user role. When enabled, plugin will add new “Booster: Product Visibility by User Role” meta box to each product’s edit page.

    Plugin URL – https://www.ads-software.com/plugins/product-visibility-by-user-role-for-woocommerce/

    Thread Starter ciberdom

    (@ciberdom)

    Thanks @mdshak

    Is it possible to do it without having to use a plugin?
    Actually I just have to hide a product and if I can avoid having to install a plugin for something so simple would be perfect.

    You can drop this simple function into your functions.php of your theme.

    function mdshak_exclude_product_from_wholesale( $q ){
    
     $current_user = wp_get_current_user();
     $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array(); // PUT THE IDS AS YOU WANT TO HIDDEN
    
     if ( !in_array( 'Partner', $current_user->roles ) ) {
      $q->set( 'post__not_in', $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN );
     }
    
    }
    
    add_action( 'woocommerce_product_query', 'mdshak_exclude_product_from_wholesale' );
    Thread Starter ciberdom

    (@ciberdom)

    Thank you very much for the code,
    Can you help me a little more please?
    I do not know how to add the ID.
    Product to hide:
    https://vegan-tattoo.com/tienda/expositor/
    ID: 27721

    User role for which the product should be visible:
    partner

    Thread Starter ciberdom

    (@ciberdom)

    hi @mdshak);
    It does not work. But I do not know if I have added it correctly

    function mdshak_exclude_product_from_wholesale( $q ){

    $current_user = wp_get_current_user();
    $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array(27721); // PUT THE IDS AS YOU WANT TO HIDDEN

    if ( !in_array( ‘partner’, $current_user->roles ) ) {
    $q->set( ‘post__not_in’, $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN );
    }

    }

    add_action( ‘woocommerce_product_query’, ‘mdshak_exclude_product_from_wholesale’ );

    You can try this code.

    function mdshak_exclude_product_from_wholesale( $q ){
    
     $current_user = wp_get_current_user();
     $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array('27721'); // PUT THE IDS AS YOU WANT TO HIDDEN
    
     if ( !in_array( 'partner', $current_user->roles ) ) {
      $q->set( 'post__not_in', $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN );
     }
    
    }
    
    add_action( 'woocommerce_product_query', 'mdshak_exclude_product_from_wholesale' );
    Thread Starter ciberdom

    (@ciberdom)

    @mdshak, Neither has that code worked.

    You can just give a try to this code also.

    ` function mdshak_exclude_product_from_wholesale( $q ){

    $current_user = wp_get_current_user();
    $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array(‘27721’); // PUT THE IDS AS YOU WANT TO HIDDEN

    if ( !in_array( ‘partner’, $current_user->roles ) ) {
    $q->set( ‘post__not_in’, $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN );
    }

    }

    add_action( ‘pre_get_posts’,’mdshak_exclude_product_from_wholesale’ );`

    Thread Starter ciberdom

    (@ciberdom)

    @mdshak;
    It does not work. The product is still visible to the “Guest” and “customer”

    Where did you paste this code? Can you please share the screenshot URL to take troubleshooting steps.

    Thread Starter ciberdom

    (@ciberdom)

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Hide product to a user role’ is closed to new replies.