I need to hide the price and add to cart button for non logged user on my website, I try with some code I found on the web but it's not entirely working.
Here is the code :
add_action('after_setup_theme', 'activate_filter');
function activate_filter()
{
add_filter('woocommerce_get_price_html', 'show_price_logged');
add_filter('woocommerce_get_suffix_html', 'show_price_logged');
}
function show_price_logged($price)
{
if (is_user_logged_in()) {
return $price;
} else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Veuillez vous <a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">connecter</a> afin de visualiser nos tarifs';
}
}
This replace the price with a text only for products who are "Variable product", for normal product I just have nothing instead of the price.
And also with the theme I use (flatsome) I can view the product inside a modal, and here I still have the Add To Cart button.
What can I add to hide this button in the modal and show the text instead of the price for non variable products ?
I need to hide the price and add to cart button for non logged user on my website, I try with some code I found on the web but it's not entirely working.
Here is the code :
add_action('after_setup_theme', 'activate_filter');
function activate_filter()
{
add_filter('woocommerce_get_price_html', 'show_price_logged');
add_filter('woocommerce_get_suffix_html', 'show_price_logged');
}
function show_price_logged($price)
{
if (is_user_logged_in()) {
return $price;
} else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Veuillez vous <a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">connecter</a> afin de visualiser nos tarifs';
}
}
This replace the price with a text only for products who are "Variable product", for normal product I just have nothing instead of the price.
And also with the theme I use (flatsome) I can view the product inside a modal, and here I still have the Add To Cart button.
What can I add to hide this button in the modal and show the text instead of the price for non variable products ?
Share Improve this question asked Jun 20, 2019 at 19:26 AntipolDevAntipolDev 11 Answer
Reset to default 0Use WordPress function is_user_logged_in().
- Not sure if you are using custom template files or not but you will definitely need to so this custom code will not get overwritten by any theme updates. So in your custom php file, you can wrap price and button in an if statement using that function to check whether a user is logged in then show price and button.
if ( is_user_logged_in() ) {
echo "Price: " $price;
echo "<button>Buy</button>
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745383117a4625321.html
评论列表(0条)