You are here

function _webform_render_product in Ubercart Webform Integration 6

Same name and namespace in other branches
  1. 7.3 components/product.inc \_webform_render_product()
  2. 7.2 components/product.inc \_webform_render_product()

Render a Webform component to be part of a form.

File

components/product.inc, line 110
Webform module product component.

Code

function _webform_render_product($component, $value = NULL) {
  $stock_description = "";
  $product_info = explode('_', $component['extra']['product'], 2);
  $node = node_load($product_info[0]);
  $sku = $product_info[1];
  if (module_exists('uc_stock')) {
    $stock_level = uc_stock_level($sku);
  }
  else {
    $stock_level = FALSE;
  }

  // Check stock levels. The product is only selectable if it is in stock.
  if ($stock_level === FALSE or intval($stock_level) > 0) {

    // Product is available.
    $product = array(
      'title' => check_plain($node->title),
      'price' => round($node->sell_price, 2),
    );
    $element = array(
      '#type' => 'textfield',
      '#title' => check_plain($component['name']),
      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
      '#weight' => $component['weight'],
      '#description' => $component['extra']['description'],
      '#default_value' => isset($value) ? check_plain($value[2]) : check_plain($component['value']),
      '#field_prefix' => t('Quantity:') . ' ',
      '#field_suffix' => theme('uc_webform_render_product', $product),
      '#required' => $component['mandatory'],
      '#pre_render' => array(
        'webform_element_title_display',
      ),
      '#disabled' => $component['extra']['disabled'],
      '#sku' => $sku,
      '#element_validate' => array(
        '_webform_render_product_validate',
      ),
    );

    // This is so that products whose quantity is disabled are added to the cart correctly.
    if ($component['extra']['disabled']) {
      $element['#value'] = isset($value) ? check_plain($value[2]) : check_plain($component['value']);
    }
    if (isset($component['extra']['width'])) {
      $element['#size'] = $component['extra']['width'];
    }
  }
  else {

    // Product is out of stock.
    $stock_description .= check_plain($node->title) . ' ' . t('is out of stock.') . '<br />';
    $element = array(
      '#type' => 'item',
      '#title' => check_plain($component['name']),
      '#default_value' => '',
      '#value' => '',
      '#description' => $stock_description . $component['extra']['description'],
    );
  }
  return $element;
}