Search code examples
phpwordpresswoocommerceproducthook-woocommerce

Hide "Add to Cart" button for logged in users in Woocommerce


On my site (using Woocommerce 3.2.6), I want to hide "Add to Cart" button only for logged in users.

I have this code:

add_action('init', 'hide_price_add_cart_logged_in');
function hide_price_add_cart_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 );
    }
}

EDIT: Someone suggested me to use this:

add_action('init', 'hide_price_add_cart_logged_in');
function hide_price_add_cart_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 );
        return WooCommerce::instance();
    }
}

But that did not work...

I have inserted this code into functions.php file into my theme, but it don't seems to make any change. I still see add to cart button when check some product.

How to remove that button? Where is error in my function?


Solution

  • You should try this instead:

    add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_add_to_cart_button', 1 );
    function remove_loop_add_to_cart_button() {
        // Only for logged in users
        if( ! is_user_logged_in() ) return;
    
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    }
    
    
    add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
    function remove_add_to_cart_button() {
        // Only for logged in users
        if( ! is_user_logged_in() ) return;
    
        global $product;
    
        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        }
        // For all other product types
        else {
            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).

    Tested and works


    Instead of hiding add to cart button on archives pages (like shop) you could replace it with a button linked to the product. So the code will be instead:

    // Replacing the button add to cart by a link to the product in Shop and archives pages
    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product  ) {
        // Only for logged in users
        if( ! is_user_logged_in() ) return;
    
        $button_text = __( "View product", "woocommerce" );
        $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    
        return $button;
    }
    
    add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
    function remove_add_to_cart_button() {
        // Only for logged in users
        if( ! is_user_logged_in() ) return;
    
        global $product;
    
        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        }
        // For all other product types
        else {
            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).

    Tested and works