You are here

function _webform_submit_productfield in Commerce Webform 7.2

Same name and namespace in other branches
  1. 8 productfield.inc \_webform_submit_productfield()
  2. 7 productfield.inc \_webform_submit_productfield()

Implements _webform_submit_component(). This executes when the webform is submitted by the user. Convert FAPI 0/1 values into something saveable.

File

./productfield.inc, line 455

Code

function _webform_submit_productfield($component, $value) {
  $return = array();
  $multiple = $component['extra']['multiple'];
  $choose_quantity = !empty($component['extra']['choose_quantity']);
  $keep_previous_submission = FALSE;
  if (is_array($value)) {

    // First check if we are dealing with a modified submission or if
    // this is a resubmission, in which case we leave products unmodified.
    foreach ($value as $product) {
      if (!is_string($product) || is_numeric($product)) {
        $keep_previous_submission = FALSE;
        break;
      }
      else {
        $keep_previous_submission = TRUE;
      }
    }
  }
  if ($keep_previous_submission) {

    // $value is an array of previously saved products.
    $return = $value;
  }
  elseif ($multiple && $choose_quantity) {
    foreach ($value['multiple_product_quantities'] as $product_id => $quantity) {
      if ($quantity > 0) {
        $details = array(
          'product_id' => $product_id,
          'quantity' => $quantity,
          'order_id' => FALSE,
          'line_item_id' => FALSE,
          'paid' => FALSE,
        );
        $return[] = json_encode($details);
      }
    }
  }
  else {
    if (is_array($value[0])) {
      foreach ($value[0] as $id => $product_id) {
        if (!empty($product_id)) {
          $details = array(
            'product_id' => $product_id,
            'quantity' => 1,
            'order_id' => FALSE,
            'line_item_id' => FALSE,
            'paid' => FALSE,
          );
          $return[] = json_encode($details);
        }
        else {
          $return[] = 0;
        }
      }
    }
    elseif (!empty($value[0]) && is_numeric($value[0])) {

      // $value[0] is a product id.
      $details = array(
        'product_id' => $value[0],
        'quantity' => empty($value[0]) ? '0' : $value[1],
        'order_id' => FALSE,
        'line_item_id' => FALSE,
        'paid' => FALSE,
      );
      $return[] = json_encode($details);
    }
  }
  return $return;
}