Home > Back-end >  How can i show specific product price to guest
How can i show specific product price to guest

Time:09-20

I have function that hide price for guest but i want to show price on specific product (over ID)..

add_filter( 'woocommerce_get_price_html', 'bbloomer_hide_price_addcart_not_logged_in', 9999, 2 );

function bbloomer_hide_price_addcart_not_logged_in( $price, $product ) {
  if ( ! is_user_logged_in() ) { 
     $price = '<div><a style="color: #F15F33; font-weight: bold;" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Prijavite se da bi vidjeli cijenu', 'bbloomer' ) . '</a></div>';
     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 );
  }

  return $price;
}

CodePudding user response:

Define an array of allowed product products ids, then from $product->get_id() get the product id and check if that product is in array of allowed products ids or not.

if not then do your hide things otherwise skip.

function bbloomer_hide_price_addcart_not_logged_in( $price, $product ) {
    // Define allowed product ids.
    $allowed_product_ids = array( 101, 102, 103 );

    if ( ! is_user_logged_in() && ! in_array( $product->get_id(), $allowed_product_ids, true ) ) {
        $price = '<div><a style="color: #F15F33; font-weight: bold;" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Prijavite se da bi vidjeli cijenu', 'bbloomer' ) . '</a></div>';
        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 );
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'bbloomer_hide_price_addcart_not_logged_in', 9999, 2 );
  • Related