Wordpress

How to hide WooCommerce Add to Cart or Price button for users who are not logged in?

[ad_1]

I will show you how to hide add to cart button or WooCommerce product conditional price. For example, you can remove the add to cart button and the price only for users who are not logged in. All this you can do without any plugins. Let’s find out together!

hide add to cart button or price

How to hide the price of a product, replace the Add to Cart button with a “Login to see price” button for non-logged-in users

Let’s first see how to display the price and add the button to cart to logged in users in the “Login to view price” way.

Login to see prices

Then grab and paste the following code into your theme or child theme’s function.php file:

add_action( 'init', 'show_price_for_logged_in_users' );

function show_price_for_logged_in_users() {
	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', 'user_mesage', 31 );
		add_action( 'woocommerce_after_shop_loop_item', 'user_mesage', 11 );
	}
}
function user_mesage() {
   	echo '<a class="button" href="' . get_permalink( wc_get_page_id('myaccount') ) . '">' . __('Đăng nhập để xem giá', 'woocommerce') . '';
}

Then the result will be displayed as the screenshot below.

Real results log in to see the price

How do I hide a product’s add-to-cart button for non-logged-in users?

results

Now, maybe you don’t want to hide the price, just hide the add to cart button. Don’t worry because it’s also easy to do. Simply paste this code into your theme or child theme’s function.php file.

function catalogue_mode_for_logged_out_users() {
	$isLoggedIn = is_user_logged_in();
	if ( false == $isLoggedIn ) {
	    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );	
	}
}
add_action( 'wp', 'catalogue_mode_for_logged_out_users' );

// Makes add to available for logged in users
add_filter( 'woocommerce_is_purchasable', 'keep_add_to_cart_button', 10, 2 );
function keep_add_to_cart_button( $is_purchasable, $product ) {
	$isLoggedIn = is_user_logged_in();
	if ( true == $isLoggedIn ) {
		return true;
	}
	return false;
}

Save and activate and hopefully this will be the end result.

Hide Add to Cart button from logged in users

Epilogue

So you know how to hide WooCommerce Add to Cart or Price button.

If you find it interesting, you can follow the basic WordPress section to learn more new knowledge.

Follow the fanpage to receive the latest articles: Hocwordpress Group

5
first
vote

Rate items



[ad_2]

Related Articles

Check Also
Close
Back to top button