admin管理员组文章数量:1130349
I have some code that is blocking the price from being shown on all products, if the user is not logged in.. this is what I want.
My issue is that I have 1 product that is free, and I need the price to show if the user is not logged in. only on this single product...
Can someone help me target that single product by id and show price the user is not logged in...
here is my original php snippet in funcions.php which blocks the price from being shown when a user is not logged in
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_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' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) .
'">Call for pricing</a>';
}
}
I have some code that is blocking the price from being shown on all products, if the user is not logged in.. this is what I want.
My issue is that I have 1 product that is free, and I need the price to show if the user is not logged in. only on this single product...
Can someone help me target that single product by id and show price the user is not logged in...
here is my original php snippet in funcions.php which blocks the price from being shown when a user is not logged in
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_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' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) .
'">Call for pricing</a>';
}
}
Share
Improve this question
asked Feb 6, 2017 at 20:30
MasterFuelMasterFuel
1011 silver badge1 bronze badge
1
- I do have the id for the product, I am just not sure how to implement it. – MasterFuel Commented Feb 6, 2017 at 20:34
2 Answers
Reset to default 1If you know the id, you can simply check current product id in your woocommerce_get_price_html action:
add_action('after_setup_theme','activate_filter') ;
function activate_filter() {
add_filter('woocommerce_get_price_html', 'show_price_logged');
}
function show_price_logged($price) {
global $product; // get current product
if(is_user_logged_in() || $product->id === 8) { // check product id
return $price;
} else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Call for pricing</a>';
}
}
But if you need more flexibility you could check product custom field. For example, you could set is_free custom field to true or any other value of your choice on product edit page and check it's value like this:
...
global $product;
$is_free_product = get_post_meta($product->id, 'is_free', true);
if(is_user_logged_in() || $is_free_product) {
return $price;
} else {
...
Added $product to the function, and also added a variable with the product ID
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter( 'woocommerce_get_price_html', 'show_price_if_logged_in', 100, 2
); }
function show_price_if_logged_in( $price, **$product** ){
// Define here your free product ID (here for example ID is 121)
**$free_product_id = 121;**
// Shows the price if customer is logged in or if product is your free product
if( is_user_logged_in() || **$free_product_id == $product->id** )
{
return $price;
}
else
{
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Call for pricing</a>';
}
}
I have some code that is blocking the price from being shown on all products, if the user is not logged in.. this is what I want.
My issue is that I have 1 product that is free, and I need the price to show if the user is not logged in. only on this single product...
Can someone help me target that single product by id and show price the user is not logged in...
here is my original php snippet in funcions.php which blocks the price from being shown when a user is not logged in
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_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' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) .
'">Call for pricing</a>';
}
}
I have some code that is blocking the price from being shown on all products, if the user is not logged in.. this is what I want.
My issue is that I have 1 product that is free, and I need the price to show if the user is not logged in. only on this single product...
Can someone help me target that single product by id and show price the user is not logged in...
here is my original php snippet in funcions.php which blocks the price from being shown when a user is not logged in
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_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' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) .
'">Call for pricing</a>';
}
}
Share
Improve this question
asked Feb 6, 2017 at 20:30
MasterFuelMasterFuel
1011 silver badge1 bronze badge
1
- I do have the id for the product, I am just not sure how to implement it. – MasterFuel Commented Feb 6, 2017 at 20:34
2 Answers
Reset to default 1If you know the id, you can simply check current product id in your woocommerce_get_price_html action:
add_action('after_setup_theme','activate_filter') ;
function activate_filter() {
add_filter('woocommerce_get_price_html', 'show_price_logged');
}
function show_price_logged($price) {
global $product; // get current product
if(is_user_logged_in() || $product->id === 8) { // check product id
return $price;
} else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Call for pricing</a>';
}
}
But if you need more flexibility you could check product custom field. For example, you could set is_free custom field to true or any other value of your choice on product edit page and check it's value like this:
...
global $product;
$is_free_product = get_post_meta($product->id, 'is_free', true);
if(is_user_logged_in() || $is_free_product) {
return $price;
} else {
...
Added $product to the function, and also added a variable with the product ID
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter( 'woocommerce_get_price_html', 'show_price_if_logged_in', 100, 2
); }
function show_price_if_logged_in( $price, **$product** ){
// Define here your free product ID (here for example ID is 121)
**$free_product_id = 121;**
// Shows the price if customer is logged in or if product is your free product
if( is_user_logged_in() || **$free_product_id == $product->id** )
{
return $price;
}
else
{
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
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 '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Call for pricing</a>';
}
}
本文标签: phpHave Woocommerce show product price if idwhen not logged in
版权声明:本文标题:php - Have Woocommerce show product price if id, when not logged in 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749031641a2305770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论