/** * 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()); }
/** * Adds a given product to the cart. If a hidden field is passed * (ValidateVariant) then simply a validation of the user including that * product is done and the users cart isn't actually changed. * * @return mixed */ public function addtocart($data, $form) { if ($variation = $this->getBuyable($data)) { $quantity = isset($data['Quantity']) && is_numeric($data['Quantity']) ? (int) $data['Quantity'] : 1; $cart = ShoppingCart::singleton(); // if we are in just doing a validation step then check if ($this->request->requestVar('ValidateVariant')) { $message = ''; $success = false; try { $success = $variation->canPurchase(null, $data['Quantity']); } catch (ShopBuyableException $e) { $message = get_class($e); // added hook to update message $this->extend('updateVariationAddToCartMessage', $e, $message, $variation); } $ret = array('Message' => $message, 'Success' => $success, 'Price' => $variation->dbObject('Price')->TrimCents()); $this->extend('updateVariationAddToCartAjax', $ret, $variation, $form); return json_encode($ret); } if ($cart->add($variation, $quantity)) { $form->sessionMessage("Successfully added to cart.", "good"); } else { $form->sessionMessage($cart->getMessage(), $cart->getMessageType()); } } else { $variation = null; $form->sessionMessage("That variation is not available, sorry.", "bad"); //validation fail } $this->extend('updateVariationAddToCart', $form, $variation); $request = $this->getRequest(); $this->extend('updateVariationFormResponse', $request, $response, $variation, $quantity, $form); return $response ? $response : ShoppingCart_Controller::direct(); }
public function addtocart($data, $form) { if ($buyable = $this->getBuyable($data)) { $cart = ShoppingCart::singleton(); $saveabledata = !empty($this->saveablefields) ? Convert::raw2sql(array_intersect_key($data, array_combine($this->saveablefields, $this->saveablefields))) : $data; $quantity = isset($data['Quantity']) ? (int) $data['Quantity'] : 1; $cart->add($buyable, $quantity, $saveabledata); if (!ShoppingCart_Controller::config()->direct_to_cart_page) { $form->SessionMessage($cart->getMessage(), $cart->getMessageType()); } ShoppingCart_Controller::direct($cart->getMessageType()); } }
public function addtocart($data, $form) { if ($buyable = $this->getBuyable($data)) { $cart = ShoppingCart::singleton(); $request = $this->getRequest(); $order = $cart->current(); if ($request && $request->isAjax() && $order) { ShopTools::install_locale($order->Locale); } $saveabledata = !empty($this->saveablefields) ? Convert::raw2sql(array_intersect_key($data, array_combine($this->saveablefields, $this->saveablefields))) : $data; $quantity = isset($data['Quantity']) ? (int) $data['Quantity'] : 1; $cart->add($buyable, $quantity, $saveabledata); if (!ShoppingCart_Controller::config()->direct_to_cart_page) { $form->SessionMessage($cart->getMessage(), $cart->getMessageType()); } $this->extend('updateAddToCart', $form, $buyable); $this->extend('updateAddProductFormResponse', $request, $response, $buyable, $quantity, $form); return $response ? $response : ShoppingCart_Controller::direct($cart->getMessageType()); } }