You are here

function _webform_submit_productfield in Commerce Webform 8

Same name and namespace in other branches
  1. 7.2 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 393

Code

function _webform_submit_productfield($component, $value) {
  $return = array();
  $multiple = $component['extra']['multiple'];
  $choose_quantity = !empty($component['extra']['choose_quantity']);
  if ($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) {
        $details = array(
          'product_id' => $product_id,
          'quantity' => 1,
          'order_id' => FALSE,
          'line_item_id' => FALSE,
          'paid' => FALSE,
        );
        $return[] = json_encode($details);
      }
    }
    else {
      $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;
}