Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active March 12, 2020 07:55
Show Gist options
  • Save raazon/1f218d7f0cf2b012f561d231a9623488 to your computer and use it in GitHub Desktop.
Save raazon/1f218d7f0cf2b012f561d231a9623488 to your computer and use it in GitHub Desktop.
Woocommerce hide price and add to cart for logged out user programmatically
<?php
/**
* @snippet Woocommerce hide price & add to cart if not logged in
* @author Razon Komar pal
*/
add_action('init', 'woo_hide_price_add_cart_for_not_logged_in_user');
add_action('template_redirect', 'woo_hide_price_add_cart_for_not_logged_in_user');
function woo_hide_price_add_cart_for_not_logged_in_user()
{
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', 20);
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
add_action('woocommerce_single_product_summary', 'woo_content_instead_of_price_add_cart', 31);
add_action('woocommerce_after_shop_loop_item', 'woo_content_instead_of_price_add_cart', 11);
add_filter('woocommerce_is_purchasable', '__return_false'); // disable add to cart product programmatically
}
}
function woo_content_instead_of_price_add_cart()
{
if (is_singular('product')) {
echo '<p>' . __('Please login to see the price', 'text_domain') . '</p>';
echo '<a href="' . get_permalink(get_option('woocommerce_myaccount_page_id')) . '" title="' . __('Please login to see the price', 'text_domain') . '">' . __('Login', 'text_domain') . '</a>';
} else {
echo '<a href="' . get_permalink(get_option('woocommerce_myaccount_page_id')) . '" title="' . __('Please login to see the price', 'text_domain') . '">' . __('Login', 'text_domain') . '</a>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment