25 replies [Last post]
Miso's picture
Offline
Joined: 08/10/2007
Juice: 134
Was this information Helpful?

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?

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Hide price unless Logged in?

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.

Miso's picture
Offline
Joined: 08/10/2007
Juice: 134
Re: Re: Hide price unless Logged in?

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?

incaic's picture
Offline
Joined: 10/13/2007
Juice: 116
Sorry, still a noob ...

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!

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Sorry, still a noob ...

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.

incaic's picture
Offline
Joined: 10/13/2007
Juice: 116
Thanks

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?

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Thanks

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.

incaic's picture
Offline
Joined: 10/13/2007
Juice: 116
Re: Re: Thanks

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

carl0s's picture
Offline
Joined: 11/12/2007
Juice: 2
tried but maybe too newbie for that.

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

Miso's picture
Offline
Joined: 08/10/2007
Juice: 134
Re: tried but maybe too newbie for that.

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?

diegohermes's picture
Offline
Joined: 09/12/2007
Juice: 147
carl0s wrote:Hi all. Sorry
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

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: carl0s wrote:Hi all. Sorry

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.

diegohermes's picture
Offline
Joined: 09/12/2007
Juice: 147
Re: Re: carl0s wrote:Hi all. Sorry

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

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Re: Re: carl0s wrote:Hi all. Sorry

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().

diegohermes's picture
Offline
Joined: 09/12/2007
Juice: 147
Re: Re: Re: Re: carl0s wrote:Hi all. Sorry

Hope you can find whats wrong.

thanks

AttachmentSize
node-product.tpl_.php_.txt 1.75 KB
Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Re: Re: Re: Re: carl0s wrote:Hi all. Sorry

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'];
  }
?>
diegohermes's picture
Offline
Joined: 09/12/2007
Juice: 147
Re: Re: Re: Re: Re: Re: carl0s wrote:Hi all. Sorry

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

ubernewbie's picture
Offline
Joined: 05/03/2008
Juice: 43
Re: Re: Re: Re: Re: Re: Re: carl0s wrote:Hi all. Sorry

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

PaulW (not verified)
PaulW's picture
Lyle, I'm replacing the "Add

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.

gsw
gsw's picture
Offline
Joined: 09/29/2008
Juice: 2
Hey, Lyle... Also very new

Hey, Lyle...

Also very new at this. I think I have more or less an idea of what to do here, but I'd really appreciate it if you could put this in baby terms for me. This is an extremely important feature that my client has requested. Could you give me step by step instructions on how to accomplish this...?

Thanks in advance.

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Lyle, I'm replacing the "Add

Can't seem to limit it to just the product page.
Check node->type = 'product'?

K.A
K.A's picture
Offline
Joined: 06/30/2011
Juice: 7
on Catalog

Im sorry to bring this back from the dead
I have got this to work perfectly.
Is there a way to get it to hide the price from the Catalog view also?

end user's picture
Offline
Joined: 01/11/2008
Juice: 1738
Miso wrote:How would I pull
Miso wrote:

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?

This might work http://www.ubercart.org/contrib/4790

I used this on my custom node page for a site I build. You'll have to change the retailer and authenticated to whatever roles you want to see different message.

<?php
global $user;
if (
in_array('retailer',$user->roles)) {
print
$node->content['add_to_cart']["#value"];
} elseif (
in_array('authenticated user',$user->roles))  {
print
"You are currently browsing as an unverified user" ;
}
else {
print
"<a href=\"http://www.******.com/user\">Retailer Login To Order and View Wholesale Prices</a>" ;
}
?>

You could do something like this

<?php
global $user;
if (
in_array('authenticated',$user->roles)) {
print
$node->content['add_to_cart']["#value"];
}
else {
print
"<a href=\"http://www.yoursite.com/user\">LOG IN TO VIEW PRICING</a>" ;
}
?>
tassoman@drupal.org's picture
Offline
Joined: 02/26/2008
Juice: 97
What about the teaser?

That works for the content, but .. Is #access available also for teaser value and views fields?

alexJohnston's picture
Offline
Bug Finder
Joined: 08/28/2009
Juice: 14
uc_field_access for Drupal 6 & theme-level solution

I'm going to hand this over to the uc_field_access developer, but I used the 3 easy steps to upgrade a D5 module to D6 and created this (operational albeit hardly tested) D6 module. The only thing that I know it lacks is a .install file which I have no idea how to author. It works fine for me even without the .install, though the 'schema module' still shows the tables as "UNKNOWN".

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Also the following code works for me to hide unless the user is authenticated. Just change "authenticated user" to whatever role you want them to have before they can see the product information and it will be hidden. This hides the whole section of product information but can easily be altered to just hide the price.

<?php
 
global $user;
  if (
in_array('authenticated user', array_values($user->roles))) { ?>

           
  <div id="product-details" class="clear">
    <div id="field-group">
      <?php print $fusion_uc_weight; ?>
      <?php print $fusion_uc_dimensions; ?>
      <?php print $fusion_uc_model; ?>
      <?php print $fusion_uc_list_price; ?>
      <?php print $fusion_uc_sell_price; ?>
      <?php print $fusion_uc_cost; ?>
    </div>
    <div id="price-group">
      <?php print $fusion_uc_display_price; ?>
      <?php print $fusion_uc_add_to_cart; ?>
    </div>
  </div><!-- /product-details -->
           
<?php } else {?>
       
  <!-- HTML for users not signed in -->
  <strong>Log in to see pricing & shipping information.</strong>

<?php } ?>

I'm using the module and theme code in tandem. It would be nice if not only this functionality (hide price if not logged in) but separate price tiers by role were added to UberCart core (or at least a full, clean module for Ubercart 2).

Cheers!

AttachmentSize
Untested/Unverified uc_field_access module for Drupal 6 2.84 KB

~~~~~~~~~~~~~
Alex Johnston
~~~~~~~~~~~~~

thiokol's picture
Offline
Joined: 05/07/2009
Juice: 69
Re: uc_field_access for Drupal 6 & theme-level solution

This doesnt hide the price when a product is added to the cart, how can I hide it there too?