$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Have Woocommerce show product price if id, when not logged in|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Have Woocommerce show product price if id, when not logged in

matteradmin9PV0评论

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
Add a comment  | 

2 Answers 2

Reset to default 1

If 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>';
}

}
Post a comment

comment list (0)

  1. No comments so far