Hide price unless Logged in?

Posts: 67
Joined: 08/10/2007

How would I pull off something like hiding a price & add to cart button with quantity input from catalog view and from product nodes if the users browsing the cart are not registered and logged in (and displaying a custom message like "LOG IN TO VIEW PRICING" or something)?

Where should I look in the code... and for what?

Posts: 2008
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.

Posts: 67
Joined: 08/10/2007

ok, so I used this in my uc_product.module and it works fine (hides the price/add to cart):

function uc_product_nodeapi(&$node, $op, $arg3, $arg4){
  global $user;
  $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;
}

now, how would I edit that code so to add an updated value instead of the display price/sell price/add to cart and display a link to user registration pages instead in order to view the prices/add to cart.

also, would this same process work in uc_catalog.module too, for catalog views too?

Posts: 30
Joined: 10/13/2007

Does this function actually go in uc_product.module under module/ubercart/uc_product ?
I wouldn't think so, as it would get messy updating module/ubercart.

Please let me know where to put this file and how it gets pulled in by Drupal.

Thanks!

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

Make a module of your own and call it "custom" or something. This involves writing a .info file and putting that function in custom.module. The function would be called custom_nodeapi() as well. Once it's in the modules directory, you can enable it like any other Drupal module and you should see it working.

Posts: 30
Joined: 10/13/2007

What I did was create custom.module and placed it under sites/all/modules/custom. But, the function inside I named uc_product_nodeapi() because I thought that the hook part of [hook]_nodeapi had to be the name of the module I am accessing. It seems to work as expected.

Am I missing something?

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

It works because Drupal loads the module file, and uc_product_nodeapi() doesn't exist in uc_product.module. Once the modules are loaded, it doesn't matter what file they came from. Hooks are called by module_invoke(), and since uc_product is an active module, the function uc_product_nodeapi() is a valid hook.

You want to name your hooks after your module so you are guaranteed not to have any name collisions. If I happen to put my own version of uc_product_nodeapi() in uc_product.module, PHP would crash on you.

Posts: 30
Joined: 10/13/2007

Got it. Made the change. Makes perfect sense, thanks.

Posts: 1
Joined: 11/12/2007

Hi all.

Sorry for being that newbie. I just tried that solution, but i copypasted that code into uc_product.module and it didn't work.
Should i change instead of '#access'?
Or shouldn't i paste it into that module?

help pls.

c

Posts: 67
Joined: 08/10/2007

I got that working just fine, but the question still remains - how would I code it so that if the user is not logged in I would "hide" the sell_price and few other fields AND change the #value of sell_price to actually display a login link AND show that sell_price variable (since I am having a problem with it being hidden due to this:

$node->content['sell_price']['#access'] = $node->content['sell_price']['#access'] && $user->uid;

and how could I extend this feature for teaser listings as well?

Posts: 62
Joined: 09/12/2007

carl0s wrote:
Hi all.

Sorry for being that newbie. I just tried that solution, but i copypasted that code into uc_product.module and it didn't work.
Should i change instead of '#access'?
Or shouldn't i paste it into that module?

help pls.

c

Same question, its just copypaste the code? what more need to be changed? I create a custom module, activate it and nothing happen. I just want to hide the add to cart button for anonymous users.

thanks

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

carl0s and diego, I think the problem is that pages are cached for anonymous users, so they are still seeing the old nodes. If you empty out your cache database tables, they should see what they are supposed to.

Posts: 62
Joined: 09/12/2007

Lyle, i try empty the cache using the devel module, but the button is still there, i check the database and empty all cache tables and the button continues to show.

Let's see if i don't forget anything

1- Create custom.module with the function custom_nodeapi()
2- Create a info file
3- Activate the module
4- Empty out cache database tables

obs: I have my own node-product.tpl and use the catalog module with grid display.

thanks

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

I finally realized that I had forgotten to specify the "view" op in my code, but that shouldn't have affected too much. Instead, it'll be the product template file that you have.I'd have to see it to figure out why it's not using the values from hook_nodeapi().

Posts: 62
Joined: 09/12/2007

Hope you can find whats wrong.

thanks

AttachmentSize
node-product.tpl_.php_.txt1.75 KB
Posts: 2008
Joined: 08/07/2007
AdministratoreLiTe!

Your template file isn't checking the access parameter of the add to cart element, so it's displaying it all the time. Change this line:

<?php
  $botao_comprar
= $node->content['add_to_cart']['#value'];
?>

to
<?php
 
if ($node->content['add_to_cart']['#access']){
   
$botao_comprar = $node->content['add_to_cart']['#value'];
  }
?>

Posts: 62
Joined: 09/12/2007

Thanks Lyle, it works just fine. What you think of add this feature in the next release of ubercart?

Posts: 17
Joined: 05/03/2008

I can't make this work. Can anyone please help?

Posts: 25
Joined: 05/23/2008

Lyle,

I'm replacing the "Add to cart" button on my product page with text for anonymous users using:

<?php
function buy_memberships_nodeapi(&$node, $op, $arg3, $arg4){
  global
$user;
  if (
$user->uid == 0 && $op = 'view') {
   
$node->content['add_to_cart']['#value'] = t('You must <a href="login">log in</a> or <a href="user/register">create an account</a> to purchase family memberships.');
  }
}
?>

Works OK, except the text 'You must log in...' appears on many, but not all pages. Can't seem to limit it to just the product page.