Please I am new to drupal. I wish to disable the 'add to cart' & 'add to wish list' buttons for all anonymous users but this would be reactivated on registering or login. On clicking either of these buttons prio to login, a dialog box would popup (not the drupal error string) saying 'login or new account required'. These obviously would returned to normal on login. An ideas or existing modules? Thanks.

Comments

bwv’s picture

Have you looked at the permissions (user settings/permission) for the ubercart module, to see if there is the possibility to disable permission for anonymous users to purchase products, even while they can still view them?

azuokeke’s picture

Thanks for your comments but I have check ubercart settings - the closest thing that can be done from the cart is the ensure anonymous checkouts which is not exactly what I want. I have found a module uc_disable_product which enables the 'add to cart' & 'wish list' buttons to be optionally disabled but this hides the buttons completely. What I would prefer is still see the buttons but on clicking either of them, a popup message would be shown to the user to register or login first. Even if the buttons are hidden, another button would still be shown that on clicking by the user to buy the product would still be shown the same message.

bwv’s picture

I wish I could help you -- it sounds like you need some javascript for the popup.

From my own experience, I have found that potential customers are -- generally speaking -- put off from purchasing something online if they have to go through the registration process first. Its the whole concept of instant gratification, which it seems to me the internet has certainly promoted.

Anyway, good luck.

azuokeke’s picture

Thanks for your comments. Would look more into a work around.

udig’s picture

One thing I thought of is that the 'Add To Cart' button would actually be a link to the regular login screen with a destination suffix to the 'add to cart' action. I do not want to 'invent the wheel' though, if there's already some solution out there.
Did someone make any progress on this one?

jenb919’s picture

I don't know how helpful this will be to you but thought I'd reply anyway.

I was able to use the rules module to hide the "add to cart". First you must enable form events ( you might also check display form element ids), then go to the product and activate the events for the "add to cart". When you create a new rule their will be new events for that "add to cart" id. For my purposed I was hiding the "add to cart" for certain roles so I uses the event for when the form was being built to hide but if you want to redirect you might try using the submit event or validate. From my understanding there are unique ids for each "add to cart" to avoid collision so you must do this for each product.

I also found there are module uc disable store,which could be used with rules to add a message with link or display a login block.

gamelodge’s picture

If you have a module you can do a form alter.

//D6
if(!user_is_logged_in()) {
     $form['#submit'][0] = 'MYMODULE_cart_table_form_submit';
}

function MYMODULE_cart_table_form_submit($form, &$form_state) {
  drupal_set_message('Please login or create an account to purchase products');
  return drupal_goto('user',drupal_get_destination()); 
}
dunx’s picture

Another hook_form_alter solution, which removes the [Add to Cart] buttons on both the Product page and the Buy Now (catalogue list) page and tells the user up front that they need to login/register before shopping.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ( strstr($form_id, 'uc_product_add_to_cart_form_') || strstr($form_id, 'uc_catalog_buy_it_now_form_') ) {
    if(!user_is_logged_in()) {
      unset($form['submit']]);
      drupal_set_message('Because many products are linked to user accounts, you need to <a href="/user/login">login</a> or <a href="/user/register">create an account</a> before purchasing.');
    }
  }
} 
Anonymous’s picture

just a little correction for the above solution (which works perfect)

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ( strstr($form_id, 'uc_product_add_to_cart_form_') || strstr($form_id, 'uc_catalog_buy_it_now_form_') ) {
    if(!user_is_logged_in()) {
      unset($form['action']['submit']);
      drupal_set_message('Because many products are linked to user accounts, you need to <a href="/user/login">login</a> or <a href="/user/register">create an account</a> before purchasing.');
    }
  }
} 
?>
AlexanderPop’s picture

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'uc_product_add_to_cart_form_XXX':
		// hide add to cart button for anonymous users and show text
		if(!user_is_logged_in()) {
			unset($form['actions']['submit']);
			drupal_set_message('Please <a href="/user/login">login</a> or <a href="/user/register">create a free Member account</a> before purchasing a Subscription.');
			$form['html_markup'] = array('#markup' => t('<div class="login-to-buy">Please <a href="/user/login">login</a> or <a href="/user/register">create a free Member account</a> before purchasing a Subscription.</div>'));
		}
		if(user_is_logged_in()) {
			$form['actions']['submit']['#value'] = 'Subscribe Now';
			$form['actions']['submit']['#prefix'] = '<div id="form-button-wrapper">';
			$form['actions']['submit']['#suffix'] = '</div>';
		}
        break;
    }
}
hockey2112’s picture

case 'uc_product_add_to_cart_form_XXX':

How can I make this into sort of a wildcard statement so that it applies to all add to cart buttons? I can't figure out how to do that, since all of the add to cart button IDs include the node ID, and I need for this to apply to all products store-wide.

jaesperanza’s picture

Posting some basic suggestions for those looking (too little info on the subject):
- Permissions > Roles (Anonymous) - uncheck the "Purchase products (Allow user to purchase products)", this is enabled by default
- Check that all related blocks (like the Shopping Cart) is disabled as well for Anonymous
- Try contrib modules such as Ubercart Disable https://www.drupal.org/project/uc_disable

Hope this helps!