0

I need to display the standard Add to Cart button for all logged in users, but anonymous users should instead see text and a link asking them to create an account or login in order to be able to add to cart. The code below works great, but it is applicable to individually-specific products only; I need it to apply to all products site-wide. How would I modify this code to accomplish this, given the fact that all Ubercart add to cart buttons contain the node ID and are thus all unique? the example shows the code applying to the product with node ID "3".

function uc_disable_cart_button_for_anonymous_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'uc_product_add_to_cart_form_3':
        // 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> in order to proceed.');
            $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> in order to proceed.</div>'));
        }
        if(user_is_logged_in()) {
            $form['actions']['submit']['#value'] = 'Add to Order';
            $form['actions']['submit']['#prefix'] = '<div id="form-button-wrapper">';
            $form['actions']['submit']['#suffix'] = '</div>';
        }
        break;
    }
}

Thanks!

0

I think I've got the solution. Any feedback would be greatly appreciated.

<?php
function uc_disable_cart_button_for_anonymous_form_alter(&$form, &$form_state, $form_id) {
    /* Overrides the Add to Cart form text*/
    if ( !empty($form['nid']) ) {
      $node = $form['nid']['#value'];
    }
    else {
      $node = 0;
    }
    switch ($form_id) {
        case (($form_id == 'uc_product_add_to_cart_form_'.$node)):
        // hide add to cart button for anonymous users and show text
        if(!user_is_logged_in()) {
            unset($form['actions']['submit']);
            $form['html_markup'] = array('#markup' => t('<div class="login-to-buy">Please login or apply for a Member account in order to proceed.<br><br><a href="/user/login" class="btn">Log In</a> &nbsp; <a href="/user/register" class="btn">Apply for Member account</a></div>'));
        }
        if(user_is_logged_in()) {
            $form['actions']['submit']['#value'] = 'Add to Order';
            $form['actions']['submit']['#prefix'] = '<div id="form-button-wrapper">';
            $form['actions']['submit']['#suffix'] = '</div>';
        }
        break;
    }

    /* Overrides the Buy It Now form text*/
    if ( !empty($form['nid']) ) {
      $node = $form['nid']['#value'];
    }
    else {
      $node = 0;
    }
    switch ($form_id) {
        case (($form_id == 'uc_catalog_buy_it_now_form_'.$node)):
        // hide buy it now button for anonymous users and show text
        if(!user_is_logged_in()) {
            unset($form['actions']['submit']);
            $form['html_markup'] = array('#markup' => t('<div class="login-to-buy">Please login or apply for a Member account in order to proceed.<br><br><a href="/user/login" class="btn">Log In</a> &nbsp; <a href="/user/register" class="btn">Apply for Member account</a></div>'));
        }
        if(user_is_logged_in()) {
            $form['actions']['submit']['#value'] = 'Add to Order';
            $form['actions']['submit']['#prefix'] = '<div id="form-button-wrapper">';
            $form['actions']['submit']['#suffix'] = '</div>';
        }
        break;
    }
}

The main drawback that I see is that the quantity and attribute fields are still displayed, but that could be good or bad based on use-case. I'd prefer to hide them, so I may try to figure that out.

  • How are you displaying 'buy it now' form on product pages? Is this hard-coded in product templates? In my opinion its easier to put the logged-in/not-logged-in conditional inside product templates. Is there a reason this would not work for your implementation? – erier Feb 11 '18 at 16:24

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.