Buy 5 get one free - promotions.php error

Could someone help with this?



I have set up a “buy 5 get one free” promotion. All worked ok (except for the extremely annoying manual setup of each lot of 5 for each product) while inventory was not optioned. Now inventory is optioned, I get error "Warning: Invalid argument supplied for foreach() in /var/www/vhosts/mysite/httpdocs/core/fn.promotions.php on line 388". Here is the existing code (starting line 377):


<br />
elseif ($bonus['bonus'] == 'free_products') {<br />
  foreach ($bonus['value'] as $p_data) {<br />
   $product_data = array (<br />
	$p_data['product_id'] => array (<br />
	 'amount' => $p_data['amount'],<br />
	 'product_id' => $p_data['product_id'],<br />
	 'extra' => array (<br />
	  'exclude_from_calculate' => true,<br />
	  'aoc' => empty($p_data['product_options']),<br />
	  'saved_options_key' => $bonus['promotion_id'] . '_' . $p_data['product_id'],<br />
	 )<br />
	),<br />
   );<br />
   if (!empty($cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']])) {<br />
	$product_data[$p_data['product_id']]['product_options'] = $cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']];<br />
   } elseif (!empty($p_data['product_options'])) {<br />
	$product_data[$p_data['product_id']]['product_options'] = $p_data['product_options'];<br />
   }<br />
   $existing_products = array_keys($cart['products']);<br />
   if ($ids = fn_add_product_to_cart($product_data, $cart, $auth)) {<br />
	$new_products = array_diff(array_keys($cart['products']), $existing_products);<br />
	if (!empty($new_products)){<br />
	 $hash = array_pop($new_products);<br />
	}else{<br />
	 $hash = key($ids);<br />
	}<br />
	$_cproduct = fn_get_cart_product_data($hash, $cart['products'][$hash], true, $cart, $auth, !empty($new_products) ? 0 : $p_data['amount']);<br />
	if (!empty($_cproduct)) {<br />
	 $cart_products[$hash] = $_cproduct;<br />
	}<br />
   }<br />
  }<br />
}<br />

```<br />
<br />
Also is there an easier way to do this buy 5 get one free?