1 reply [Last post]
edonovan's picture
Offline
Joined: 02/22/2012
Juice: 15
Was this information Helpful?

Using Ubercart 2.x for Drupal 6 (2.4 to be exact, but I think the latest 2.7 would be the same), I'm trying to figure out how I can get the SKU for a product kit to show in the admin's notification of an order.

I understand that the product kit is just a collection of individual prices, but the client's accountant enters the order into QuickBooks based off the receipt email, and her job would be made much easier if the SKU for the product kit showed up, since that's what defines the price point at which it was sold, and thus what product it is in QuickBooks.

Is there an existing module for Ubercart, or an available token, which would permit me to add this to the email template, or will I need to code it myself?

edonovan's picture
Offline
Joined: 02/22/2012
Juice: 15
Re: Adding the Product Kit SKU to an Admin Order Notification Te

I ended up writing a module to implement this myself. The only thing I'm not happy about is the need to use a session variable. Does anyone have ideas on an alternative approach so that this might be able to go in Ubercart core?

<?php
/* In install file: */

function uc_pk_receipt_schema() {
 
$schema['uc_order_product_kit'] = array(
   
'description' => 'Stores record of the product kits ordered as part of an order.',
   
'fields' => array(
     
'order_id' => array(
       
'description' => 'The {uc_order}.order_id of the order.',
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE,
       
'default' => 0,
      ),
     
'product_id' => array(
       
'description' => 'The {uc_products}.nid of the product contained in the kit.',
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE,
       
'default' => 0,
      ),
    ),
   
'primary key' => array('order_id', 'product_id'),
   );
   return
$schema;
}

/* Rest in module file: */

/**
* Implements hook_form_alter().
* Adds a new submit handler for the add to cart form for a product kit,
* in order to log the product kits that have been purchased.
*/

function uc_pk_receipt_form_alter(&$form, &$form_state, $form_id) {
 
// Add a new submit handler for the add to cart form for a product kit
 
if(strpos($form_id, 'uc_product_kit_add_to_cart') !== FALSE) {
   
$form['#submit'][] = 'uc_pk_receipt_add_to_cart_form_submit';
  }
}

/**
* Custom submit handler to add the product kit nids to an order.
*/
function uc_pk_receipt_add_to_cart_form_submit($form, &$form_state) {
 
// Add a session variable to track the nid of the product kit item.
 
if(!isset($_SESSION['product_kit_nids'])) {
   
$_SESSION['product_kit_nids'] = array();
  }
 
$_SESSION['product_kit_nids'][] = $form_state['values']['nid'];
}

/**
* Implements hook_order().
*/
function uc_pk_receipt_order($op, &$arg1, $arg2) {
  global
$user;
  if(
$op == 'save' && !empty($arg1->order_id)) {
    if(isset(
$_SESSION['product_kit_nids']) && is_array($_SESSION['product_kit_nids'])) {
      foreach(
$_SESSION['product_kit_nids'] as $nid) {
       
$record = (object) array('order_id' => $arg1->order_id, 'product_id' => $nid);
       
drupal_write_record('uc_order_product_kit', $record);
      }
      unset(
$_SESSION['product_kit_nids']);
    }
  }
  elseif(
$op == 'load') {
   
$product_kit_nids = array();
   
$result = db_query('SELECT order_id, product_id FROM {uc_order_product_kit} WHERE order_id = %d', $arg1->order_id);
    while(
$record = db_fetch_array($result)) {
     
$product_kit_nids['nid'] = $record['product_id'];
    }
    if(
count($product_kit_nids > 0)) {
     
$arg1->product_kit_nids = $product_kit_nids;
    }
  }
}

/**
* Implements MODULENAME_preprocess_uc_order().
* Adds a variable for the product kits.
*/
function uc_pk_receipt_preprocess_uc_order(&$variables) {
 
$order = $variables['order'];
  if(isset(
$order->product_kit_nids) && is_array($order->product_kit_nids)) {
   
$product_kit_names = array();
    foreach(
$order->product_kit_nids as $nid) {
     
$node = node_load($nid);
      if(!empty(
$node->title)) {
       
$product_kit_names[$nid] = $node->title;
      }
    }
    if(isset(
$product_kit_names) && count($product_kit_names) > 0) {
     
$variables['pk_names'] = theme('item_list', $product_kit_names);
    }
  }
}
?>

How to use this in template:

<?php if(!empty($pk_names)): ?>
<p>Purchased Product Kits:</p>
<?php print $pk_names; ?>
<?php endif; ?>