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):



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




Also is there an easier way to do this buy 5 get one free?