0

I am constructing a site that will serve mainly agents (authenticated users), but should still show all of the products (without prices) for anonymous users. When logged in, only agents should see the Add-to-Cart form on the product display page, and anonymous users should instead see a link to an "Ask a Question" page.

Is this possible? I looked through the permissions table and could not find a way to prevent access to the cart form by anonymous users.

The only thing I can think of doing is customizing my node--product-display.tpl.php file:

<?php  
    if ($logged_in) {
      print render($page['content']);
  } else {
      // something else
  }
?>

I'll have to print the rest of my fields out manually, but as far as I know, this is the only way to print the Add to Cart form. I'm a bit uneasy about this, so I'm humbly looking here for another solution. thanks

6

From the link : http://www.drupalcommerce.org/node/2528 with an condition for anonymous user added.

It's taken me a while but I've finally cracked it! I've used hook_form_FORM_ID_alter() (that took me some learning and meant I had to create my own, admittedly trivial, module!). So in my module I've got the following which sets #access for the submit form to false which stops it being rendered (see http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc...).

In the future, I'll be able to extend this to disable the button on certain conditions (say when there's no stock but I want to show what combinations are possible). It would also presumably be an easy way of changing, say, the text for the button, such as "Buy" instead of "Add to cart".

  function mymodule_form_commerce_cart_add_to_cart_form_alter (&$form, &$form_state, $form_id) {
global $user;
    if ($user->uid == 0) {
    // Disable the Add to Cart submit button


  $form['submit'] = array('#access' => FALSE);
        }    
    }
  • Thanks Harshal! Let me try this out and I'll let you know – maGz Feb 10 '14 at 9:43
  • sorry for the late reply, but that worked like a charm. thanks! – maGz Feb 24 '14 at 12:43
  • I just customized this to see if the user was also in a specific role – maGz Feb 24 '14 at 12:43
  • @maGz Please share your code. It may be helpful for others. – Kevin Siji Jul 27 '16 at 11:09
  • @Kevin: you're right, although this was a while back and I'll have to look through my backups to pull that out. Will try an make some time this evening to get this for you. – maGz Jul 27 '16 at 11:14
0

I added a notice to the accepted answer.

function hook_form_commerce_cart_add_to_cart_form_alter (&$form, &$form_state, $form_id) {
  global $user;
  if ($user->uid == 0) {
    // Disable the Add to Cart submit button
    $form['submit'] = array('#access' => FALSE);
    // Display notice to login
    $form['notice'] = array(
      '#prefix' => '<div class="login-notice">',
      '#markup' => t('<a href="/user/login">Login</a> / <a href="/user/register">Register</a> to buy this product before its gone.'),
      '#suffix' => '</div>', 
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.