Last active
July 24, 2021 14:00
-
-
Save charliesjc/d7371103e2b79df63b02c0cf6774d028 to your computer and use it in GitHub Desktop.
Hide prices for logged-out users, filtering by category, and change add-to-cart buttons to login redirect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Hide normal add-to-cart buttonsfor logged-out users, filtering by category, and change add-to-cart buttons to login redirect. | |
*/ | |
add_filter('woocommerce_is_purchasable', 'semper_modify_purchasable'); | |
function semper_modify_purchasable( $is_purchasable ) { | |
if ((has_term('hoodies', 'product_cat') && !is_user_logged_in())) { | |
if (is_singular()) { | |
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); | |
add_action( 'woocommerce_single_product_summary', 'semper_login_for_price_button', 31); | |
} | |
return false; | |
} | |
return $is_purchasable; | |
} | |
function semper_login_for_price_button() { | |
echo sprintf('<a class="single_add_to_cart_button button alt" href="%s">%s</a>', esc_url(get_permalink(wc_get_page_id('myaccount'))), __('Login to see prices', 'text-domain')); | |
} | |
add_filter('woocommerce_loop_add_to_cart_link', 'semper_modify_add_to_cart_link'); | |
function semper_modify_add_to_cart_link($add_to_cart_html) { | |
if ( has_term('hoodies', 'product_cat') && !is_user_logged_in() ) { | |
$add_to_cart_html = sprintf('<a href="%s" class="button">%s</a>', esc_url(get_permalink(wc_get_page_id('myaccount'))), __('Login to see prices', 'text-domain')); | |
} | |
return $add_to_cart_html; | |
} | |
// add_filter( 'woocommerce_variable_sale_price_html', 'semper_hide_prices', 10, 2 ); | |
// add_filter( 'woocommerce_variable_price_html', 'semper_hide_prices', 10, 2 ); | |
/* | |
After testing it for a bit I don't think it's necessary to hook into the above unless you needed to change | |
something specific to variable prices ONLY. | |
*/ | |
add_filter( 'woocommerce_get_price_html', 'semper_hide_prices' ); | |
function semper_hide_prices( $price) { | |
if (!is_admin() && has_term('hoodies', 'product_cat') && !is_user_logged_in()) { | |
$price = '##.##'; // You can set this to '' to remove the price altogether. | |
} | |
return $price; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment