You are here

function uc_product_kit_cart_display in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_product_kit/uc_product_kit.module \uc_product_kit_cart_display()

Implementation of Ubercart's hook_cart_display().

Displays either the kit as a whole, or each individual product based on the store configuration. Each product in the cart that was added by uc_product_kit was also given a unique kit id in order to help prevent collisions. The side effect is that identical product kits are listed separately if added separately. The customer may still change the quantity of kits like other products.

Parameters

$item: An item in the shopping cart.

Return value

A form element array to be processed by uc_cart_view_form().

File

uc_product_kit/uc_product_kit.module, line 691
The product kit module for Übercart.

Code

function uc_product_kit_cart_display($item) {
  static $elements = array();
  static $products;
  $unique_id = $item->data['unique_id'];
  $kit = node_load($item->data['kit_id']);

  //print '<pre>'. print_r($kit, true) .'</pre>';
  if ($kit->mutable == UC_PRODUCT_KIT_MUTABLE) {
    return uc_product_cart_display($item);
  }
  else {
    if (!isset($products[$unique_id])) {

      // Initialize table row
      $element = array();
      $element['nid'] = array(
        '#type' => 'value',
        '#value' => $kit->nid,
      );
      $element['module'] = array(
        '#type' => 'value',
        '#value' => 'uc_product_kit',
      );
      $element['remove'] = array(
        '#type' => 'checkbox',
      );
      if ($kit->mutable == UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST) {
        $element['options'] = array(
          '#value' => '<div class="item-list"><ul class="cart-options">' . "\n",
        );
      }
      $element['title'] = array(
        '#value' => l($kit->title, 'node/' . $kit->nid),
      );
      $element['#total'] = 0;
      $element['qty'] = array(
        '#type' => 'textfield',
        '#default_value' => $item->qty / $kit->products[$item->nid]->qty,
        '#size' => 5,
        '#maxlength' => 6,
      );
      $elements[$unique_id] = $element;
    }

    // Add product specific information
    $op_names = '';
    foreach ($item->options as $option) {
      $op_names .= $option['name'] . ', ';
    }
    $op_names = substr($op_names, 0, strlen($op_names) - 2);
    if ($op_names) {
      $op_names = '-- ' . $op_names;
    }
    if (node_access('view', node_load($item->nid))) {
      $title = l($item->title, 'node/' . $item->nid);
    }
    else {
      $title = $item->title;
    }
    if ($kit->mutable == UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST) {
      $elements[$unique_id]['options']['#value'] .= '<li>' . $item->qty . ' x ' . $title . " <em>{$op_names}</em></li>\n";
    }
    $elements[$unique_id]['#total'] += $item->price * $item->qty;
    $elements[$unique_id]['data'][$item->nid] = $item;
    $products[$unique_id][] = $item->nid;

    // Check if all products in this kit have been accounted for.
    $done = true;
    foreach ($kit->products as $product) {
      if (!in_array($product->nid, $products[$unique_id])) {
        $done = false;
        break;
      }
    }
    if ($done) {
      $elements[$unique_id]['data'] = array(
        '#type' => 'value',
        '#value' => serialize($elements[$unique_id]['data']),
      );
      $elements[$unique_id]['options']['#value'] .= "</ul></div>\n";
      $element = $elements[$unique_id];
      unset($products[$unique_id]);
      unset($elements[$unique_id]);
      return $element;
    }
  }
  return array();
}