Re: Hide price unless Logged in?

Posts: 2243
Joined: 08/07/2007
AdministratoreLiTe!

In uc_product_view(), each element has an #access property that determines whether it will be shown or not. This is how the settings for the product fields get enforced. In addition, the cost field only allows users with the 'administer products' permission to see it.

You can use hook_nodeapi('view') to change or modify these elements however you want.

Anonymous users have a user id of 0, so you can do something like this:

<?php
 
function custom_nodeapi(&$node, $op, $arg3, $arg4){
    global
$user;
    switch (
$op){
      case
'view':
       
$node->content['display_price']['#access'] = $node->content['display_price']['#access'] && $user->uid;
       
$node->content['sell_price']['#access'] = $node->content['sell_price']['#access'] && $user->uid;
       
$node->content['add_to_cart']['#access'] = $node->content['add_to_cart']['#access'] && $user->uid;
      break;
    }
  }
?>

You want to keep the value of the #access attribute so that it will continue to be affected by the field settings.

If, instead, you want to replace the price with a "Please log in", message, you can change the #value attribute of an element.

Hide price unless Logged in? By: Miso (19 replies) Wed, 09/26/2007 - 20:41