Home > Blockchain >  Hide Add to cart button for specific role and replace with other button
Hide Add to cart button for specific role and replace with other button

Time:04-16

For non logged in users I use the php snippet below. Visitors see a "Where to buy" button and a "Ask a demo" button instead of the Woocommerce Add to cart button. Logged in users / customers with a specific role gets the "Add to cart button" instead of the 2 buttons for not logged in customers. There is also a viewonly role who has a login and can download materials but don't have an Add to cart button on single product pages. However with the code below the viewonly users can still see the regular Add to cart button. Any idea how i can fix this?

add_action( 'init', 'disable_add_to_cart' );
 
function disable_add_to_cart() {
    if ( !is_user_logged_in() OR $user_role == 'viewonly' ) {
        add_filter( 'woocommerce_is_purchasable', '__return_false');
    }

function hide_price( $price ) {
  if ( !is_user_logged_in() ) {
    $price = '';
  $user_role = 'viewonly';
    }
  return $price;
}
  
add_filter( 'woocommerce_get_price_html', 'hide_price' );
add_filter( 'woocommerce_cart_item_price', 'hide_price' );
add_filter( 'woocommerce_cart_item_subtotal', 'hide_price' ); 
add_filter( 'woocommerce_order_formatted_line_subtotal', 'hide_price' );  
add_filter( 'woocommerce_cart_subtotal', 'hide_price' );  
add_filter( 'woocommerce_cart_totals_order_total_html', 'hide_price' ); 
} 
  
add_action( 'init', 'hide_price_add_cart_not_logged_in' );

function print_login_to_see() {
   echo '<div ><a  href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
    echo '<a  href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
        echo '</div>';
}
  
function hide_price_add_cart_not_logged_in() {   
   if ( ! is_user_logged_in() ) {      
      remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
      remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
      remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
      remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); 
    add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 31 );
  }
}```

CodePudding user response:

To replace the buttons, all you have to do is use the actions you removed and place your button function into it. I am using the author role for testing purposes, but this will check if the user is not logged in and what their role is. Then, remove the add to cart button from the shop page loop and also the single product page. Once they are removed, we add what we want into where we just removed the button from.

If you need to, just change the conditions within the if statement and that will change who sees which set of buttons.

Now all you have to do is style your buttons the way you'd like them! Let me know if you have any questions! :)

$user_role = wp_get_current_user(

function print_login_to_see() {
    echo '<div ><a  href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
     echo '<a  href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
         echo '</div>';
 }
 
 if ( current_user_can( 'viewonly' ) || !is_user_logged_in() ) { // Set the user role to the specific role you want to replace the buttons for.
     
     // Remove button from products in the loop and single product page
     remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
     remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
     
     // Add the actions you just removed, but insert your button function
     add_action( 'woocommerce_after_shop_loop_item', 'print_login_to_see');
     add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 30 );
 }
  • Related