• Hi,

    I need to do something quite uncommun.

    I want some products absolutely not accessible and visible for the users who have a username different from a custom product attribute value.

    Ex: the product has the attribute “room” and the value “1400”
    I want that only the user who has “1400” as username to be able to buy it.

    What’s the best way to make it happen ?

    Best Regards,

    Vincent

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Hello there,

    Could you please try to use the following code?

    add_filter( 'woocommerce_product_is_visible', 'myprefix_restrict_product_visibility_per_user', 10, 2 );
    function myprefix_restrict_product_visibility_per_user($visible, $id){
    
    	$user = 'the_username'; /* User name */
    	$attribute_taxonomy = 'the_attribute_slug'; /* Attribute slug */
    
    	$my_attribute = wp_get_post_terms($id, $attribute_taxonomy);
    
    	if(count($my_attribute) > 0){
    		$attribute_array = array();
    		foreach ($my_attribute as $attribute) {
    
    			$attribute_array[] = $attribute->name;
    
    		}
    	}
    
    	if( ! in_array($user, $attribute_array)){
    		$visible = false;
    	}
    
    	return $visible;
    
    }

    Paste the code above to your theme’s functions.php file. Don’t forget to replace the_username and the_attribute_slug to what you need. The attribute slug usually preceded with pa_ (e.g. pa_brand).

    I hope this reply helps.

    Let me know how it work for you.

    Regards

Viewing 1 replies (of 1 total)
  • The topic ‘Exclude products based on username’ is closed to new replies.