3

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?

Parm
  • 65
  • 1
  • 2
  • 5

4 Answers4

5

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.';
  }

}
Angela
  • 51
  • 1
  • 2
1

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!';

}
Robert Lee
  • 1,541
  • 1
  • 10
  • 20
  • 1
    thanks for the answer..my code works for hiding price for the users that are not logged in..i just need something that hides add to cart for non logged in users as well.. – Parm May 13 '13 at 21:32
  • tried that...i guess that filter would only work if i buy the catalogue visibility extension for woocommerce ..thanks though.. – Parm May 14 '13 at 01:02
0

What about CSS?

button.add-to-cart {
    display: none;
}

body.logged-in button.add-to-cart {
    display: block;
}
Pilgrimish
  • 11
  • 5
0

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).

m3esma
  • 269
  • 3
  • 8