Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created September 30, 2013 14:07
Show Gist options
  • Save claudiosanches/6764354 to your computer and use it in GitHub Desktop.
Save claudiosanches/6764354 to your computer and use it in GitHub Desktop.
WooCommerce - Hide price and add-to-cart button for non-logged in users.
<?php
function woocommerce_template_loop_price() {
if ( is_user_logged_in() )
woocommerce_get_template( 'loop/price.php' );
}
function woocommerce_template_loop_add_to_cart() {
if ( is_user_logged_in() )
woocommerce_get_template( 'loop/add-to-cart.php' );
}
function woocommerce_template_single_price() {
if ( is_user_logged_in() )
woocommerce_get_template( 'single-product/price.php' );
}
function woocommerce_template_single_add_to_cart() {
global $product;
if ( is_user_logged_in() )
do_action( 'woocommerce_' . $product->product_type . '_add_to_cart' );
}
@designbyidi
Copy link

Hey nice work! Any way to add in a message for logged out users?

@CrandellWS
Copy link

@designbyidi https://businessbloomer.com/woocommerce-hide-price-add-cart-logged-users/

/**
 * @snippet       Hide Price & Add to Cart for Logged Out Users
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=299
 * @compatible    WooCommerce 3.6.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
  
add_action( 'init', 'bbloomer_hide_price_add_cart_not_logged_in' );
  
function bbloomer_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', 'bbloomer_print_login_to_see', 31 );
 add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
  
function bbloomer_print_login_to_see() {
echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment