• Resolved orlafitz

    (@orlafitz)


    I am trying to remove the Woocommerce “Buy Now” button for the user role “um_member”.

    I’ve been able to remove the button by logged in user with “if ( is_user_logged_in() ) {”

    However, when I try to use the user role snippet from here: https://docs.ultimatemember.com/article/164-getrole ” if ( $ultimatemember->user->get_role() == ‘um_member’ ) {”

    I get the following error: “Call to a member function get_role() on null”

    Here is the full working code snippet for logged in users:

    // If User is not logged in don't allow them to purchase
    if ( is_user_logged_in() ) {
        
    } else {
    
    	//function for deleting ....
    function remove_product_description_add_cart_button(){
        global $product;
    
        // Set HERE your category ID, slug or name (or an array)
        $category = 'restricted';
    
        //Remove Add to Cart button from product description of product with id 1234   
        if ( has_term( $category, 'product_cat', $product->id ) ) 
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
    
    add_action('wp','remove_product_description_add_cart_button');
    	
    }
    

    Here is how I attempted to implement it to restrict by the user role:
    `
    // If User is not logged in don’t allow them to purchase

    if ( $ultimatemember->user->get_role() == ‘um_member’ ) {

    } else {

    //function for deleting ….
    function remove_product_description_add_cart_button(){
    global $product;

    // Set HERE your category ID, slug or name (or an array)
    $category = ‘restricted’;

    //Remove Add to Cart button from product description of product with id 1234
    if ( has_term( $category, ‘product_cat’, $product->id ) )
    remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_add_to_cart’, 30 );
    }

    add_action(‘wp’,’remove_product_description_add_cart_button’);

    }
    `

    Any help would be hugely appreciated on this!

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

Viewing 10 replies - 1 through 10 (of 10 total)
  • if ( $ultimatemember->user->get_role() == ‘Member’ ) {// do stuff}

    Thread Starter orlafitz

    (@orlafitz)

    Hi Txpmr,
    Thank you for the reply but am still getting Call to a member function get_role() on null

    Full code as implemented in functions.php:

    // If User is not logged in don't allow them to purchase
    if ( $ultimatemember->user->get_role() == ‘Member’ ) {
      //if ( is_user_logged_in() ) {
        
    } else {
    
    	//function for deleting ....
    function remove_product_description_add_cart_button(){
        global $product;
    
        // Set HERE your category ID, slug or name (or an array)
        $category = 'restricted';
    
        //Remove Add to Cart button from product description of product with id 1234   
        if ( has_term( $category, 'product_cat', $product->id ) ) 
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
    
    add_action('wp','remove_product_description_add_cart_button');
    	
    }
    • This reply was modified 6 years, 7 months ago by orlafitz.

    have you tried using get_role_name instead?

    https://docs.ultimatemember.com/article/165-getrolename

    Thread Starter orlafitz

    (@orlafitz)

    Yup – same error comes up – it seems like I am missing a variable. Do I need to do something to define get_role_name() or get_role().

    I’ve also tried dynamically fetching the user_id:

    um_fetch_user( get_current_user_id() ); 
    if ( $ultimatemember->user->get_role_name() == ‘Member’ )

    I keep getting the same error Call to a member function get_role_name() on null

    read the UM article about using shortcodes for user roles.

    Thread Starter orlafitz

    (@orlafitz)

    I’ve been through every permutation of the shortcodes – using do_shortcode(‘ [um_show_content roles=’member’]’); but that threw up errors (syntax error, unexpected 'member' (T_STRING)) which was why I moved on to the solution I am working on. The logic of the current solution makes more sense to use but I just can’t figure out how to get Ultimate Member to work with the functions file.

    where are you calling it in the script? it could be displaying null because you not logged in. make sense?

    Thread Starter orlafitz

    (@orlafitz)

    I am calling it in the functions file – the purpose of the if statement is to determine if the user is assigned a role – if not do nothing. The script doesn’t seem able to connect to the Ultimate Member role ID ‘um_member’. It seems like there is a piece of the puzzle missing but I don’t have enough experience with PHP to figure it out. It is a pity that there are no developers from Ultimate Member on this support forum – perhaps I will try Stack Overflow.

    Thread Starter orlafitz

    (@orlafitz)

    The issue was that the global variable had changed in UM 2.0 with $ultimatemember->user->get_role() now changing to UM()->user()->get_role() – they haven’t gotten around to changing the docs yet. With a little more help from Ultimate Member support here is the full working code incase anyone ever needs it :-):

    $user = wp_get_current_user();
    if ( in_array( 'um_member', (array) $user->roles ) )
    {
        // DO THIS IF USER IS A MEMBER
    } else {
        // DO THIS IF USER IS NOT A MEMBER	
    }

    Thank you for all your help TXPMR – without the updated docs we couldn’t have figured this one out :-D!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Restrict woocommerce buy now button based on user role php’ is closed to new replies.