Reputation: 101
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
um_fetch_user( get_current_user_id() );
if ( $ultimatemember->user->get_role_name() == '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. I have also tried the Ultimate Member support forum on Wordpress but haven't gotten any direct response from the developers yet.
Upvotes: 1
Views: 795
Reputation: 253784
I have totally revisited your code. Try the following without any guaranty:
add_action('woocommerce_single_product_summary','non_member_remove_single_add_to_cart', 2 );
function non_member_remove_single_add_to_cart(){
global $product, $ultimatemember;
um_fetch_user( get_current_user_id() );
// Check if it's a member user role
$is_member = $ultimatemember->user->get_role() == 'member' ? true : false;
// HERE set your categories in the array (IDs, slugs or names)
$categories = array('restricted');
// Remove single Add to Cart button
if ( ! $is_member && has_term( $category, 'product_cat', $product->get_id() ) )
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
Code goes in function.php file of your active child theme (or active theme). Untested, it could work.
Based on this 2 Ultimatemember snippets code:
Upvotes: 1
Reputation: 101
Thank you @LoicTheAztec!
I was able to get through to Ultimate Member support eventually and there were two problems with my original solution.
the global variable had changed in the UM 2.0 major update yesterday - $ultimatemember->user->get_role()
now changing to UM()->user()->get_role()
– they haven’t gotten around to changing the docs yet.
The code was still not working because each member has multiple roles (eg: wordpress default 'subscriber' & ultimate member's 'member') so support re-worked it again.
Here is the working solution without my WooCommerce Scripts:
$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
}
Here is the full solution I used to remove BUY NOW the button for non-approved customers and instead show a "Restricted Products" notice box that displays custom fields:
$user = wp_get_current_user();
if ( in_array( 'um_member', (array) $user->roles ) )
{
} 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');
add_action( 'woocommerce_single_product_summary', 'restrict_access', 20 );
}
function restrict_access() {
echo '<div class="restrict-access"><h4>';
the_field('notice_title' , 'option');
echo '</h4><p>';
the_field('notice_details' , 'option');
echo '</p><a class="et_pb_button" href="/restricted-product">';
the_field('notice_button_text' , 'option');
echo '</a></div>';
}
Thank you so much for your help on this.
Upvotes: 1