Hiding CCK field in Product View

Posts: 17
Joined: 05/03/2008

Hi,

I posted a topic a few minutes ago, but have just removed it, as it was inaccurate.

In case anyone has read the topic, this is the actual situation and what I want to achieve.

I have found the solution to what I need to do, but when trying to implement it, it isn't working out. I've been going at this problem for over 12 hours straight.

I have created 2 CCK fields with wholesale prices. I wish to hide these prices when users are not logged in by using a custom module. I also wish to display a helper text with the prices, when users are logged in.

Here is the code I have tried:

<?php
 
function uc_product_nodeapi(&$node, $op, $arg3, $arg4){
    global
$user;
    switch (
$op){
      case
'view':
$node->field_wholesale_price_single[0]['view'] =  $node->field_wholesale_price_single[0]['view'] && $user->uid;

$node->content['field_wholesale_price_multiple']['#weight'] = $node->content['field_wholesale_price_multiple']['#weight'] && $user->uid;
break;
    }
  }
?>

I can enable this module, but it is not having any effect.

I have also customised my product display page with the file: node-product-tpl.php, with the code:

<div id="product_title"><h2><?php print $title ?></h2></div>
<div id="product_image"><?php print $node->picture ?> <?php print $node->content['image']['#value'] ?></div>

<div id="product_code">Code: <?php print $node->field_code[0]['view']; ?></div>
<div id="product_brand"><p>Brand: <?php print $node->taxonomy[16]->name; ?></p></div>
<div id="product_manufacturer"><p>Manufactured In: <?php print $node->field_manufactured_in[0]['view']; ?></p></div>
<div id="product_description"><?php print $node->content['body']['#value'];  ?></div>
<div id="product_image_info">Click Image to Enlarge</div>
<div id="product_pdf"><?php print $node->content['files']['#value']; ?></div>
<div id="product_price">(Effective 01/08/2008. May be subject to change without notice. Price shown excludes GST)<?php print $node->content['field_wholesale_price_single']['#value']; ?> <?php print $node->content['field_wholesale_price_multiple']['#value']; ?></div>

Can anyone help?

Thanks.

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

I think you want to use '#access' instead of 'view'. I had thought that 'view' was the actual content being displayed on the screen.

Posts: 17
Joined: 05/03/2008

Thanks Lyle,
Been away for a while. Thanks for your help.

Tried replacing 'view' with 'access', and now it shows/hides the field title, but not the field value. I have uploaded the site to a web server: http://www.dtoomey.com/nam/node/1

Posts: 17
Joined: 05/03/2008

Can't get past this one. Would anyone be able to give some further clues as to how this can be accomplished?

Posts: 17
Joined: 05/03/2008

I've found a solution to this, for non-programmers like myself.

The problem had been solved with a drupal module called: 'cck field permissions'

http://drupal.org/project/cck_field_perms

Posts: 17
Joined: 05/03/2008

I think I spoke too soon.

The module works perfectly, but if you've customized the product view using node-product.tpl.php (as I have), this overrides the permissions.

I'm back to square one now.

Posts: 15
Joined: 06/07/2008

Hello Newb cousin!

You may be able to do it in your theme. In your node-whatever.tpl.php file you could have an if statement that would display the field only if the current user's uid isn't 0.

Posts: 17
Joined: 05/03/2008

Hi Newbstah,

Many thanks. This was the way to do it, for sure.

I got some help over at #drupal-ubercart (a great irc community).

I'm going to list the steps here in the hope that it'll make someone else's life a bit easier.

I'm very thin on the ground with PHP and can only really alter working examples to achieve what I need to do.

Anyway, to get this working, I followed these steps:

• Add a CCK field to product page from: Administer > Content Management > Content Types

• Add a product and populate all your fields, including your added custom CCK field

• Place this code at the end of your node-product.tpl.php page:

<pre>
<?php // this will print the $node array at the bottom of each node take it out when finished ?>
<?php var_dump($node); ?>
</pre>

You'll find your node-product.tpl.php page in your working theme directory. This page can be customized to alter and theme your product pages.

Load a product page in your browser. At the end of the page you'll notice drupal has spat out a lot of raw PHP code. Through some trial and error, you need to pick up the PHP code which represents your custom CCK field.

Here's an example: $node->field_wholesale_details[0]['view']

• To display, theme or alter the display position of this field, you can use CSS and DIVs on your tpl.php page

• To hide this field based on whether or not a user is logged in, you can surround it with this code:

<?php
global $user;
if(
$user->uid != 0){
  print
$node->field_wholesale_details[0]['view'];
} else {
  print
"Log in to view wholesale information";
}
?>

As I understand it, the user id (uid) of 0 is assigned to anonymous users. So as per the above code, the field will only be displayed to users 1 >.

This is probably a rough way of doing this. I ideally wanted to create a custom module to achieve this, but I simply could not get it to work, after many weeks of effort and research.

Thanks to acm over at #drupal-ubercart for your help.