1

I'm working on a new site built with Drupal 6, and I have to program a specific action on the node pages.
I have a page that shows the product information for users. Following the information, I have a button labeled "order now"; when this button is clicked, two scenarios are possible:

  1. if the user is not logged in, he is redirect to the login form
  2. if the user is logged in, an e-mail is sent to the administrator with the current product information and the information of the user who clicked the button
2

Rules Module can do this for you.

0

You can just use a submit callback for the button and check the current user with global $user;. Just add $form['button']['#submit'] = array('mymodule_submit_callback'); to the button's array and use the callback to do your work.

function mymodule_submit_callback($form, &$form_state) {
  global $user;
  if ($user->uid == 0) {
    // Send the user to the login page if user is anonymous.
    drupal_goto('user');
  }
  else {
    drupal_mail(); // Send an email to the administrator.
  }
}

So, here you have access to the user that clicked the button as well as any form data in $form_state['values']. Check out the drupal_mail API reference for information on how use this function to send mail.

Alternatively, if you want to just provide a link you could also just use a menu callback.

function mymodule_menu() {
  $items['path/to/callback/%user'] = array(
    'title' => t('Send mail'),
    'type' => MENU_CALLBACK,
    'page callback' => 'mymodule_mail_callback',
    'page arguments' => array(3),
  );
  return $items;
}

function mymodule_mail_callback($user) {
  // Check if this is an anonymous user and redirect if necessary.
  if ($user->uid == 0) {
    drupal_goto('user');
  }
  else {
    drupal_mail(); // Etc.
  }
}

Then, the link on the node form can be done like this

function mymodule_form_alter($form, &$form_state, $form_id) {
  // Ensure this is the form you want with a conditional.
  global $user;
  // Add your "Order now" link to the form, linking to your menu callback.
  $form['mail_link'] = array('#value' => l(t('Order now'), 'path/to/callback/' . $user->uid));
  return $form;
}

Alternatively, you could disclude the user ID from the menu callback path and just get the global $user variable from within the menu callback itself.

0

Check out the Drupal Commerce module, or simply try the Commerce Kickstart installation profile.

It is a complete commerce solution which is highly integrated with the Rules module, so you can easily define what you described in your question.

  • thanks for your quick reply, but i don't like to install a hole complex module for that because it's not an e-commerce web site , i'd like a very simple hook or cutom module to run this function. – NoOneElse Sep 11 '11 at 1:33

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.