showing add to cart for logged in users only..woocommerce

been using this code to hide prices..

add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
if(is_user_logged_in() ){
    return $price;
}
else return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see price!';
}

tried modifying it to use for hiding add to cart as well..but no avail..
anyone?

Related posts

Leave a Reply

4 comments

  1. Extending the above code (thanks Ewout), the following code will get rid of all prices and ‘add to cart’ buttons on all woocommerce products, as well as provide an explanation as to why. I needed the code for a website that offers direct selling products and to comply with their rules, I cannot show prices to the general public.

    Add the filter to your theme’s functions.php file.

    add_filter('woocommerce_get_price_html','members_only_price');
    
    function members_only_price($price){
    
    if(is_user_logged_in() ){
    return $price;
    }
    
    else {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
      }
    
    }
    
  2. Have you tried something like this? You would set woocommerce to only show prices when user is logged in.

    add_filter('catalog_visibility_alternate_price_html', 'my_alternate_price_text', 10, 1);
    function my_alternate_price_text($content) {
        return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see price!';
    }
    

    Reference: http://docs.woothemes.com/document/catalog-visibility-options/

    EDIT:

    The reference material has the cart visibility reference

    add_filter('catalog_visibility_alternate_add_to_cart_button', 'my_alternate_button', 10, 1);
    
    function my_alternate_button($content) {
    
        return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see cart!';
    
    }
    
  3. We can do this easily through the woocommerce_is_purchasable and woocommerce_get_price_html hooks.

    Code:

    // Disable purchase for non-logged-in users. (Remove add-to-cart button).
    function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
        if ( ! is_user_logged_in() ) {
            return false;
        }
    
        return $is_purchasable;
    }
    add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
    
    // Show "Login to see prices" instead of price for non-logged in users.
    function m3wc_woocommerce_get_price_html( $price ) {
        if ( ! is_user_logged_in() ) {
            return '<a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Login</a> to see prices';
        }
    
        return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'm3wc_woocommerce_get_price_html', 10, 2 );
    

    And the result:

    non-logged in users

    Source: WooCommerce – Disable purchase for non-logged-in users. (Remove add-to-cart button).