function it_throws_cart_not_found_exception_if_currency_code_is_undefined(CartContextInterface $cartContext, ShopperContextInterface $shopperContext, ChannelInterface $channel, OrderInterface $cart) { $cartContext->getCart()->willReturn($cart); $shopperContext->getChannel()->willReturn($channel); $shopperContext->getCurrencyCode()->willThrow(CurrencyNotFoundException::class); $this->shouldThrow(CartNotFoundException::class)->during('getCart'); }
function it_throws_handle_exception_if_cart_was_not_found(CartContextInterface $cartContext, ObjectManager $cartManager) { $cartContext->getCart()->willThrow(CartNotFoundException::class); $cartManager->persist(Argument::any())->shouldNotBeCalled(); $cartManager->flush()->shouldNotBeCalled(); $this->shouldThrow(HandleException::class)->during('handle', ['en_GB']); }
function its_cart_contexts_can_have_priority(CartContextInterface $firstCartContext, CartContextInterface $secondCartContext, CartInterface $cart) { $firstCartContext->getCart()->shouldNotBeCalled(); $secondCartContext->getCart()->willReturn($cart); $this->addContext($firstCartContext, -1); $this->addContext($secondCartContext, 0); $this->getCart()->shouldReturn($cart); }
function it_handles_cart_currency_code_change(CartContextInterface $cartContext, OrderUpdaterInterface $orderUpdater, EventDispatcherInterface $eventDispatcher, EntityManagerInterface $orderManager, OrderInterface $cart) { $cartContext->getCart()->willReturn($cart); $cart->setCurrencyCode('USD')->shouldBeCalled(); $orderUpdater->update($cart)->shouldBeCalled(); $orderManager->persist($cart)->shouldBeCalled(); $orderManager->flush()->shouldBeCalled(); $eventDispatcher->dispatch(SyliusCartEvents::CART_CHANGE, Argument::type(CartEvent::class))->shouldBeCalled(); $this->handle('USD'); }
/** * @return OrderInterface * * @throws UnexpectedTypeException */ private function getCart() { try { $cart = $this->cartContext->getCart(); } catch (CartNotFoundException $exception) { return null; } if (!$cart instanceof OrderInterface) { throw new UnexpectedTypeException($cart, OrderInterface::class); } return $cart; }
/** * {@inheritdoc} */ public function handle($code) { try { /** @var OrderInterface $cart */ $cart = $this->cartContext->getCart(); $cart->setLocaleCode($code); $this->cartManager->persist($cart); $this->cartManager->flush(); } catch (CartNotFoundException $exception) { throw new HandleException(self::class, 'Sylius was unable to find the cart.', $exception); } }
/** * @param Event $event * * @throws UnexpectedTypeException */ public function recalculateCartWhileLogin(Event $event) { try { $cart = $this->cartContext->getCart(); } catch (CartNotFoundException $exception) { return; } if (!$cart instanceof OrderInterface) { throw new UnexpectedTypeException($cart, OrderInterface::class); } $this->orderProcessor->process($cart); }
/** * {@inheritdoc} */ public function handle($code) { try { /** @var OrderInterface $cart */ $cart = $this->cartContext->getCart(); $cart->setCurrencyCode($code); $this->exchangeRateUpdater->update($cart); $this->orderManager->persist($cart); $this->orderManager->flush(); $this->eventDispatcher->dispatch(SyliusCartEvents::CART_CHANGE, new CartEvent($cart)); } catch (CartNotFoundException $exception) { throw new HandleException(self::class, 'Sylius was unable to find the cart.', $exception); } }
/** * Builds frontend main menu. * * @return ItemInterface */ public function createMainMenu() { $menu = $this->factory->createItem('root', ['childrenAttributes' => ['class' => 'nav nav-pills']]); $menu->setCurrentUri($this->request->getRequestUri()); $cart = $this->cartContext->getCart(); $cartTotals = ['items' => $cart->getTotalQuantity(), 'total' => $cart->getTotal()]; $menu->addChild('cart', ['route' => 'sylius_cart_summary', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.cart', ['%items%' => $cartTotals['items'], '%total%' => $this->priceHelper->convertAndFormatAmount($cartTotals['total'])])], 'labelAttributes' => ['icon' => 'icon-shopping-cart icon-large']])->setLabel($this->translate('sylius.frontend.menu.main.cart', ['%items%' => $cartTotals['items'], '%total%' => $this->priceHelper->convertAndFormatAmount($cartTotals['total'])])); if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $route = $this->request === null ? '' : $this->request->get('_route'); if (1 === preg_match('/^(sylius_account)/', $route)) { $menu->addChild('shop', ['route' => 'sylius_homepage', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.account.shop')], 'labelAttributes' => ['icon' => 'icon-th icon-large', 'iconOnly' => false]])->setLabel($this->translate('sylius.frontend.menu.account.shop')); } else { $menu->addChild('account', ['route' => 'sylius_account_profile_show', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.account')], 'labelAttributes' => ['icon' => 'icon-user icon-large', 'iconOnly' => false]])->setLabel($this->translate('sylius.frontend.menu.main.account')); } $menu->addChild('logout', ['route' => 'sylius_user_security_logout', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.logout')], 'labelAttributes' => ['icon' => 'icon-off icon-large', 'iconOnly' => false]])->setLabel($this->translate('sylius.frontend.menu.main.logout')); } else { $menu->addChild('login', ['route' => 'sylius_user_security_login', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.login')], 'labelAttributes' => ['icon' => 'icon-lock icon-large', 'iconOnly' => false]])->setLabel($this->translate('sylius.frontend.menu.main.login')); $menu->addChild('register', ['route' => 'sylius_user_registration', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.register')], 'labelAttributes' => ['icon' => 'icon-user icon-large', 'iconOnly' => false]])->setLabel($this->translate('sylius.frontend.menu.main.register')); } if ($this->authorizationChecker->isGranted('ROLE_ADMINISTRATION_ACCESS') || $this->authorizationChecker->isGranted('ROLE_PREVIOUS_ADMIN')) { $routeParams = ['route' => 'sylius_backend_dashboard', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.administration')], 'labelAttributes' => ['icon' => 'icon-briefcase icon-large', 'iconOnly' => false]]; if ($this->authorizationChecker->isGranted('ROLE_PREVIOUS_ADMIN')) { $routeParams = array_merge($routeParams, ['route' => 'sylius_switch_user_return', 'routeParameters' => ['username' => $this->tokenStorage->getToken()->getUsername(), '_switch_user' => '_exit']]); } $menu->addChild('administration', $routeParams)->setLabel($this->translate('sylius.frontend.menu.main.administration')); } $this->eventDispatcher->dispatch(MenuBuilderEvent::FRONTEND_MAIN, new MenuBuilderEvent($this->factory, $menu)); return $menu; }
/** * Tries to initialize cart if there is data in storage. */ private function initializeCart() { if (null === $this->cart) { $cartIdentifier = $this->context->getCurrentCartIdentifier(); if ($cartIdentifier) { $this->cart = $this->getCartByIdentifier($cartIdentifier); } } }
/** * @param FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { if (!$event->isMasterRequest()) { return; } /** @var Request $request */ $request = $event->getRequest(); // Hacky hack. Until there is a better solution. if (!$this->isHtmlRequest($request)) { return; } try { $cart = $this->cartContext->getCart(); } catch (CartNotFoundException $exception) { return; } if (null !== $cart && null !== $cart->getId() && null !== $cart->getChannel()) { $request->getSession()->set(sprintf('%s.%s', $this->sessionKeyName, $cart->getChannel()->getCode()), $cart->getId()); } }
/** * {@inheritdoc} */ public function getCart() { if (null !== $this->cart) { return $this->cart; } /** @var OrderInterface $cart */ $cart = $this->cartContext->getCart(); try { $cart->setChannel($this->shopperContext->getChannel()); } catch (ChannelNotFoundException $exception) { throw new CartNotFoundException('Sylius was not able to prepare the cart properly', $exception); } try { $cart->setCurrencyCode($this->shopperContext->getCurrencyCode()); } catch (CurrencyNotFoundException $exception) { throw new CartNotFoundException($exception); } $cart->setCustomer($this->shopperContext->getCustomer()); $this->cart = $cart; return $cart; }
/** * Tries to initialize cart if there is data in storage. * If not, returns new instance from resourceFactory * * @return CartInterface */ private function provideCart() { $cartIdentifier = $this->cartContext->getCurrentCartIdentifier(); if ($cartIdentifier !== null) { $cart = $this->cartRepository->find($cartIdentifier); if ($cart !== null) { return $cart; } } $cart = $this->cartFactory->createNew(); $this->cartContext->setCurrentCartIdentifier($cart); return $cart; }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if (!$event->isMasterRequest()) { return; } $request = $event->getRequest(); if (!$this->requestMatcher->matches($request)) { return; } /** @var OrderInterface $order */ $order = $this->cartContext->getCart(); if ($order->isEmpty()) { $event->setResponse(new RedirectResponse($this->urlGenerator->generate('sylius_shop_cart_summary'))); } $stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH); if ($stateMachine->can($this->getRequestedTransition($request))) { return; } if (null !== ($referer = $this->getReferer($request))) { $event->setResponse(new RedirectResponse($referer)); return; } $event->setResponse(new RedirectResponse($this->urlGenerator->generateForOrderCheckoutState($order))); }
/** * {@inheritdoc} */ public function resolve(CartItemInterface $item, $data) { $id = $this->resolveItemIdentifier($data); $channel = $this->channelContext->getChannel(); if (!($product = $this->productRepository->findOneByIdAndChannel($id, $channel))) { throw new ItemResolvingException('Requested product was not found.'); } // We use forms to easily set the quantity and pick variant but you can do here whatever is required to create the item. $form = $this->formFactory->create('sylius_cart_item', $item, ['product' => $product]); $form->submit($data); // If our product has no variants, we simply set the master variant of it. if (null === $item->getVariant() && 1 === $product->getVariants()->count()) { $item->setVariant($this->variantResolver->getVariant($product)); } if (null === $item->getVariant() && 1 > $product->getVariants()->count()) { throw new ItemResolvingException('Please select variant'); } $variant = $item->getVariant(); // If all is ok with form, quantity and other stuff, simply return the item. if (!$form->isValid() || null === $variant) { throw new ItemResolvingException('Submitted form is invalid.'); } $cart = $this->cartContext->getCart(); $quantity = $item->getQuantity(); $context = ['quantity' => $quantity]; if (null !== ($customer = $cart->getCustomer())) { $context['groups'] = $customer->getGroups()->toArray(); } $item->setUnitPrice($this->priceCalculator->calculate($variant, $context)); foreach ($cart->getItems() as $cartItem) { if ($cartItem->equals($item)) { $quantity += $cartItem->getQuantity(); break; } } if (!$this->availabilityChecker->isStockSufficient($variant, $quantity)) { throw new ItemResolvingException('Selected item is out of stock.'); } return $item; }
function it_initializes_cart_while_validating_existence_and_if_there_is_identifier_in_storage(CartContextInterface $cartContext) { $cartContext->getCurrentCartIdentifier()->willReturn(666); $this->hasCart()->shouldReturn(true); }
function it_does_nothing_if_cannot_find_cart(CartContextInterface $cartContext, OrderProcessorInterface $orderProcessor, Event $event) { $cartContext->getCart()->willThrow(CartNotFoundException::class); $orderProcessor->process(Argument::any())->shouldNotBeCalled(); $this->recalculateCartWhileLogin($event); }
function it_does_nothing_if_there_is_no_existing_cart_on_interactive_login(CartContextInterface $cartContext, InteractiveLoginEvent $interactiveLoginEvent, TokenInterface $token, ShopUserInterface $user) { $cartContext->getCart()->willThrow(CartNotFoundException::class); $interactiveLoginEvent->getAuthenticationToken()->willReturn($token); $token->getUser()->willReturn($user); $this->onInteractiveLogin($interactiveLoginEvent); }
/** * @return OrderInterface */ protected function getCurrentCart() { return $this->cartContext->getCart(); }
function let(CartContextInterface $cartContext, OrderInterface $cart) { $cartContext->getCart()->willReturn($cart); $this->beConstructedWith($cartContext); }
/** * @return CartInterface|null */ public function getCurrentCart() { return $this->cartContext->getCart(); }