/**
* Handles form submission
* @param array $data
* @return bool|\SS_HTTPResponse
*/
public function addtocart(array $data)
{
$groupedProduct = $this->getController()->data();
if (empty($data) || empty($data['Product']) || !is_array($data['Product'])) {
$this->sessionMessage(_t('GroupedCartForm.EMPTY', 'Please select at least one product.'), 'bad');
$this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
return $response ? $response : $this->controller->redirectBack();
}
$cart = ShoppingCart::singleton();
foreach ($data['Product'] as $id => $prodReq) {
if (!empty($prodReq['Quantity']) && $prodReq['Quantity'] > 0) {
$prod = Product::get()->byID($id);
if ($prod && $prod->exists()) {
$saveabledata = !empty($this->saveablefields) ? Convert::raw2sql(array_intersect_key($data, array_combine($this->saveablefields, $this->saveablefields))) : $prodReq;
$buyable = $prod;
if (isset($prodReq['Attributes'])) {
$buyable = $prod->getVariationByAttributes($prodReq['Attributes']);
if (!$buyable || !$buyable->exists()) {
$this->sessionMessage("{$prod->InternalItemID} is not available with the selected options.", "bad");
$this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
return $response ? $response : $this->controller->redirectBack();
}
}
if (!$cart->add($buyable, (int) $prodReq['Quantity'], $saveabledata)) {
$this->sessionMessage($cart->getMessage(), $cart->getMessageType());
$this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this);
return $response ? $response : $this->controller->redirectBack();
}
}
}
}
$this->extend('updateGroupCartResponse', $this->request, $response, $groupedProduct, $data, $this);
return $response ? $response : ShoppingCart_Controller::direct($cart->getMessageType());
}