Restrict woocommerce buy now button based on user role php
-
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 purchaseif ( $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]
- The topic ‘Restrict woocommerce buy now button based on user role php’ is closed to new replies.