0

I need to load the order object and run the init function when the order status is updated in Ubercart instead of using hook_menu(). How do I hook into the "Order status gets updated" action? The order state should be "Payment Received."

Does it have something to do with the following code:

function uc_order_rules_event_info() {
  $events['uc_order_status_update'] = array(
    'label' => t('Order status gets updated'),
    'group' => t('Order'),
    'variables' => array(
        'order' => array(
            'type' => 'uc_order',
            'label' => t('Original order'),
        ),
        'updated_order' => array(
            'type' => 'uc_order',
            'label' => t('Updated order'),
        ),
    ),
);


function hemo_menu() {
  $items['add_listing'] = array(
    'page callback' => 'init',
  );

  return $items;
}

function init() {
  foreach ($order->products as $product) {
    $array = data_array($product->data['attributes']);

    foreach ($array as $property => $value) {
      $product->$property = $value;
    }

    $product->primary_email = $order->primary_email;
    create_directory_listing($product);
  }
}

function data_array($arr) {
  $array = array();

  foreach ($arr as $key => $value) {
    foreach ($value as $key2 => $value2) {
      $chars = array(" ", ",", "'", "(", ")", "__");
      $key = str_replace($chars, "_", strtolower($key));
      $value2 = ucwords($value2);
      $array += array("$key" => $value2);
    }
  }
  return $array;
}

function create_directory_listing($product) {
  $node = new stdClass();
  $node->type = 'directory_listing';
  node_object_prepare($node);
  $node->language = LANGUAGE_NONE;
  $node->uid = $product->order_uid;
  $node->field_order_product_id[$node->language][0]['value'] = $product->order_product_id;
  $node->field_order_id[$node->language][0]['value'] = $product->order_id;

  if (isset($product->attendee_name)) {
    $node->title = $product->attendee_name;
  }
  if (isset($product->institution)) {
    $node->field_institution[$node->language][0]['value'] = $product->institution;
  }
  if (isset($product->lab_group)) {
    $node->field_lab_group[$node->language][0]['value'] = $product->lab_group;
  }
  if (isset($product->address)) {
    $node->field_address1[$node->language][0]['value'] = $product->address;
  }
  if (isset($product->address2)) {
    $node->field_address2[$node->language][0]['value'] = $product->address2;
  }
  if (isset($product->city)) {
    $node->city[$node->language][0]['value'] = $product->city;
  }
  if (isset($product->state_or_region)) {
    $node->field_zone[$node->language][0]['value'] = $product->state_or_region;
  }
  if (isset($product->country)) {
    $node->field_country[$node->language][0]['value'] = $product->country;
  }
  if (isset($product->phone)) {
    $node->field_phone[$node->language][0]['value'] = $product->phone;
  }
  if (isset($product->primary_email)) {
    $node->field_email[$node->language][0]['value'] = $product->primary_email;
  }
  if (isset($product->zip_or_postal_code)) {
    $node->field_postal[$node->language][0]['value'] = $product->zip_or_postal_code;
  }
}

1 Answer 1

2

I believe you're looking for hook_uc_order() which runs for several operations, including the update operation which is:

Called when an order's status is changed.

Assuming your module is called "hemo" you code might look something like this:

function hemo_uc_order($op, $order, $arg2) {
  if ($op == 'update') {
    if ($order->order_status == 'payment_received') {
      // Do something with the $order object.
    }
  }
}

Remember to clear Drupal's cache once you've implemented the new hook or it might not get registered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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