Skip to content

Instantly share code, notes, and snippets.

@wooxperto
Created August 16, 2023 10:52
Show Gist options
  • Save wooxperto/28083ef634e93be514c6f5cc11b42226 to your computer and use it in GitHub Desktop.
Save wooxperto/28083ef634e93be514c6f5cc11b42226 to your computer and use it in GitHub Desktop.
Hide price and add to cart button on the Shop and the single product page for the guest users.
<?php
/**
* @snippet Hide price& Add to cart button on shop and single product page | WooCommerce
* @author WooXperto
* @date 16.08.2023
*/
if( !function_exists("wooXperto_hide_price_addcart_not_logged_in")){
function wooXperto_hide_price_addcart_not_logged_in( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'wooXperto' ) . '</a></div>';
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 );
add_filter( 'woocommerce_is_purchasable', '__return_false' );
}
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'wooXperto_hide_price_addcart_not_logged_in', 9999, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment