From 26f47ef6b6d9730086d075b5f47ff7f1e68d57ba Mon Sep 17 00:00:00 2001
From: vpshah86 <vpshah86@gmail.com>
Date: Thu, 19 May 2016 17:14:27 +0530
Subject: [PATCH] Issue #2317249 by vpshah86:
 warning_invalid_argument_supplied_for_foreach_in_element_children.

---
 commerce_webform.module | 60 +++++++++++++++++++++++++------------------------
 1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/commerce_webform.module b/commerce_webform.module
index 0d8df7c..06cf29e 100644
--- a/commerce_webform.module
+++ b/commerce_webform.module
@@ -560,35 +560,37 @@ function commerce_webform_form_views_form_commerce_cart_form_default_alter(&$for
   // Go through all items in the basket and note if they are webform
   // products. If they cannot have their quantities changed, remove
   // the quantity edit textfield.
-  foreach (element_children($form['edit_quantity']) as $id) {
-    // If this is a commerce webform item, check if it is mandatory.
-    $line_item_id = $form['edit_quantity'][$id]['#line_item_id'];
-    $line_item = commerce_line_item_load($line_item_id);
-    if ($line_item->type == 'commerce_webform') {
-      if (!in_array('_commerce_webform_cart_validate', $form['#validate']) && $line_item->data['commerce_webform']['choose_quantity'] && $line_item->data['commerce_webform']['required']) {
-        // This is a commerce_webform line item with variable quantity
-        // and is required, add a validation function to stop it from
-        // being set to 0.
-        array_unshift($form['#validate'], '_commerce_webform_cart_validate');
-      }
-
-      $nid = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_webform_nid->value();
-      if (!isset($webforms[$nid])) {
-        $webforms[$nid] = array(
-          'count' => 0,
-          'title' => node_load($nid)->title,
-        );
-      }
-      $webforms[$nid]['count']++;
-
-      if (!$line_item->data['commerce_webform']['choose_quantity']) {
-        // Remove the ability to change the quantity.
-        $form['edit_quantity'][$id]['#type'] = 'value';
-        $form['edit_quantity'][$id]['quantity'] = array(
-          '#markup' => '<p>' . $form['edit_quantity'][$id]['#default_value'] . '</p>',
-        );
-      }
-    }
+  if($form['edit_quantity']) {
+	  foreach (element_children($form['edit_quantity']) as $id) {
+		// If this is a commerce webform item, check if it is mandatory.
+		$line_item_id = $form['edit_quantity'][$id]['#line_item_id'];
+		$line_item = commerce_line_item_load($line_item_id);
+		if ($line_item->type == 'commerce_webform') {
+		  if (!in_array('_commerce_webform_cart_validate', $form['#validate']) && $line_item->data['commerce_webform']['choose_quantity'] && $line_item->data['commerce_webform']['required']) {
+			// This is a commerce_webform line item with variable quantity
+			// and is required, add a validation function to stop it from
+			// being set to 0.
+			array_unshift($form['#validate'], '_commerce_webform_cart_validate');
+		  }
+
+		  $nid = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_webform_nid->value();
+		  if (!isset($webforms[$nid])) {
+			$webforms[$nid] = array(
+			  'count' => 0,
+			  'title' => node_load($nid)->title,
+			);
+		  }
+		  $webforms[$nid]['count']++;
+
+		  if (!$line_item->data['commerce_webform']['choose_quantity']) {
+			// Remove the ability to change the quantity.
+			$form['edit_quantity'][$id]['#type'] = 'value';
+			$form['edit_quantity'][$id]['quantity'] = array(
+			  '#markup' => '<p>' . $form['edit_quantity'][$id]['#default_value'] . '</p>',
+			);
+		  }
+		}
+	  }
   }
 
   // Go through each of the delete buttons on the form. If they
-- 
1.9.4.msysgit.1

