Subscriber.Add in Ubercart Checkout Form

I'm using Ubercart on a Drupal site and I'd like users to be able to subscribe to our Campaign Monitor email list through the Ubercart checkout form.

I've built a custom module that is successfully adding the Newsletter Subscribe checkbox to the form. I'm using variables from the Ubercart form & calling Subscriber.Add through a SOAP Request as documented in the API. Everything appears to be working fine, but users aren't being added to my particular subscriber list.

I've checked the $data, $headers, & $url for the drupal_http_request and everything is populating as expected. My Campaign Monitor apikey & listid are correct as well... I've double checked many times. In the code below I've inserted x's because I wasn't sure if it was sensitive information or not. :)

Please take a look at this code & if anyone could point me in the right direction, that would be great!

function uc_foo_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'uc_cart_checkout_form') {
  // Add a checkbox which lets users subscribe to the newsletter & add submit handler to handle back-end Campaign Monitor adding code.
    $form['panes']['customer']['subscribe_to_newsletter'] = array(
      '#type' => 'checkbox',
      '#title' => t('Subscribe to our newsletter to receive news, updates, and sales announcements.'),
      '#description' => t('Every newsletter contains a link which allows you to easily unsubscribe.'),
      '#default_value' => 0,
    $form['#submit'][] = 'uc_foo_cm_add';

function uc_foo_cm_add($form, &$form_state){
        //If checkbox is selected, add an IF statement to add them to campaign & return a confirmation message.
            $cm['apikey'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
            $cm['listid'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Campaign Monitor List ID
              $cm['email'] = $form_state['values']['panes']['customer']['primary_email'];
              $cm['name'] = $form_state['values']['panes']['billing']['billing_first_name'].' '.$form_state['values']['panes']['billing']['billing_last_name'];
              $cm['sitename'] = 'xxxxxx';  
            $url = "http://{$cm['sitename']}";
              $headers = array(
                'Content-Type' => 'text/xml; charset=utf-8',
                'SOAPAction'   => '""',

              $data = '<?xml version="1.0" encoding="utf-8"?>'
                    . '<soap:Envelope xmlns:xsi=""'
                    . 'xmlns:xsd=""'
                    . 'xmlns:soap="">'
                    . '<soap:Body>'
                    . '<Subscriber.Add xmlns="">'
                    . '<ApiKey>'.$cm['apikey'].'</ApiKey>'
                    . '<ListID>'.$cm['listid'].'</ListID>'
                    . '<Email>'.$cm['email'].'</Email>'
                    . '<Name>'.$cm['name'].'</Name>'
                    . '</Subscriber.Add>'
                    . '</soap:Body>'
                    . '</soap:Envelope>';
            drupal_http_request($url, $headers, 'POST', $data);
            drupal_set_message('You have been added to our newsletter subscriber list!');
kcornelius, 8 years ago

I found a solution to my own problem. Instead of calling the sitename into the $url, I needed to simply call! (No sitename needed.) Silly, but I'm so happy it's solved.

Mathew Mathew, 8 years ago

Good spotting! Glad you got it all working now.

Get in touch with us on Twitter:
We're also on Facebook:
jonty17, 7 years ago


I don't suppose you had considered releasing the code as a module had you?? This would be very useful to a lot of people! I've had a look through the code and tried to roll it up into a module, but I'm a bit dumb at this and not having a lot of success. Even if what you have only partially works it would be a good starting point for anybody wanting this functionality.

See why 200,000 companies worldwide love Campaign Monitor.

From Australia to Zimbabwe, and everywhere in between, companies count on Campaign Monitor for email campaigns that boost the bottom line.

Get started for free