/** * Add one of an item to a cart (Product Page) * * @see the addtocart function within AddProductForm class * @param SS_HTTPRequest $request * @param AjaxHTTPResponse $response * @param Buyable $buyable [optional] * @param int $quantity [optional] * @param AddProductForm $form [optional] */ public function updateAddProductFormResponse(&$request, &$response, $buyable, $quantity, $form) { if ($request->isAjax()) { if (!$response) { $response = $this->owner->getController()->getResponse(); } $response->removeHeader('Content-Type'); $response->addHeader('Content-Type', 'application/json; charset=utf-8'); $shoppingcart = ShoppingCart::curr(); $shoppingcart->calculate(); // recalculate the shopping cart $data = array('id' => (string) $buyable->ID, 'internalItemID' => $buyable->InternalItemID, 'title' => $buyable->Title, 'url' => $buyable->URLSegment, 'categories' => $buyable->getCategories()->column('Title'), 'addLink' => $buyable->addLink(), 'removeLink' => $buyable->removeLink(), 'removeallLink' => $buyable->removeallLink(), 'setquantityLink' => $buyable->Item()->setquantityLink(), 'message' => array('content' => $form->Message(), 'type' => $form->MessageType())); $form->clearMessage(); // include totals if required if ($shoppingcart) { $data['subTotal'] = $shoppingcart->SubTotal(); $data['grandTotal'] = $shoppingcart->GrandTotal(); } $this->owner->extend('updateAddProductFormResponseShopJsonResponse', $data, $request, $response, $buyable, $quantity, $form); $response->setBody(json_encode($data)); } }
/** * @param SS_HTTPRequest $request * @param AjaxHTTPResponse $response * @param AddProductForm $form [optional] */ public function updateCartFormResponse(&$request, &$response, $form = null) { if ($request->isAjax()) { if (!$response) { $response = $this->owner->getAjaxResponse(); } $this->setupRenderContexts($response); if (self::config()->show_ajax_messages) { $response->triggerEvent('statusmessage', array('content' => $form->Message(), 'type' => $form->MessageType())); $form->clearMessage(); } // Because ShoppingCart::current() calculates the order once and // then remembers the total, and that was called BEFORE the product // was added, we need to recalculate again here. Under non-ajax // requests the redirect eliminates the need for this but under // ajax the total lags behind the subtotal without this. ShoppingCart::curr()->calculate(); $response->pushRegion('CartFormAjax', $this->owner, array('Editable' => true)); } }