/** * {@inheritdoc} */ public function load(CustomerInterface $customer) { $basket = $this->getFromSession($customer); if (!$basket) { $basket = $this->basketManager->create(); $basket->setLocale($customer->getLocale()); $basket->setCurrency($this->currencyDetector->getCurrency()); } $basket->setCustomer($customer); $this->basketBuilder->build($basket); return $basket; }
/** * Write a basket element, this method is used by both POST and PUT action methods. * * @param int $basketId A Sonata ecommerce basket identifier * @param Request $request A Symfony Request service * @param int $elementId A Sonata ecommerce basket element identifier * * @return \FOS\RestBundle\View\View|FormInterface */ protected function handleWriteBasketElement($basketId, $request, $elementId = null) { $basket = $this->getBasket($basketId); $this->basketBuilder->build($basket); $productId = $request->request->get('productId'); $product = $this->getProduct($productId); $productPool = $basket->getProductPool(); $code = $productPool->getProductCode($product); $productDefinition = $productPool->getProduct($code); $basketElement = $elementId ? $this->getBasketElement($elementId) : $this->basketElementManager->create(); $basketElement->setProductDefinition($productDefinition); $form = $this->formFactory->createNamed(null, 'sonata_basket_api_form_basket_element', $basketElement, array('csrf_protection' => false)); $form->bind($request); if ($form->isValid()) { $basketElement = $form->getData(); if ($elementId) { $this->basketElementManager->save($basketElement); } else { $basket->addBasketElement($basketElement); $this->basketManager->save($basket); } $view = \FOS\RestBundle\View\View::create($basket); $serializationContext = SerializationContext::create(); $serializationContext->setGroups(array('sonata_api_read')); $serializationContext->enableMaxDepthChecks(); $view->setSerializationContext($serializationContext); return $view; } return $form; }
/** * {@inheritdoc} */ public function load(CustomerInterface $customer) { $basket = $this->session->get($this->getSessionVarName($customer)); if (!$basket) { $basket = $this->session->get($this->getSessionVarName()); if (!$basket) { $basket = $this->basketManager->create(); $basket->setLocale($customer->getLocale()); $basket->setCurrency($this->currencyDetector->getCurrency()); } } $basket->setCustomer($customer); $this->basketBuilder->build($basket); // always clone the basket so it can be only saved by calling // the save method return clone $basket; }