/** * Builds frontend main menu. * * @param Request $request * * @return ItemInterface */ public function createMainMenu(Request $request) { $menu = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'nav nav-pills'))); if ($this->cartProvider->hasCart()) { $cart = $this->cartProvider->getCart(); $cartTotals = array('items' => $cart->countItems(), 'total' => $cart->getTotal()); } else { $cartTotals = array('items' => 0, 'total' => 0); } $menu->addChild('cart', array('route' => 'sylius_cart_summary', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total'])))), 'labelAttributes' => array('icon' => 'icon-shopping-cart icon-large')))->setLabel($this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total'])))); if ($this->securityContext->getToken() && $this->securityContext->isGranted('ROLE_USER')) { $route = $this->request === null ? '' : $this->request->get('_route'); if (1 === preg_match('/^(sylius_account)|(fos_user)/', $route)) { $menu->addChild('shop', array('route' => 'sylius_homepage', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.account.shop')), 'labelAttributes' => array('icon' => 'icon-th icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.account.shop')); } else { $menu->addChild('account', array('route' => 'sylius_account_homepage', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.account')), 'labelAttributes' => array('icon' => 'icon-user icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.account')); } $menu->addChild('logout', array('route' => 'fos_user_security_logout', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.logout')), 'labelAttributes' => array('icon' => 'icon-off icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.logout')); } else { $menu->addChild('login', array('route' => 'fos_user_security_login', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.login')), 'labelAttributes' => array('icon' => 'icon-lock icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.login')); $menu->addChild('register', array('route' => 'fos_user_registration_register', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.register')), 'labelAttributes' => array('icon' => 'icon-user icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.register')); } if ($this->securityContext->getToken() && ($this->securityContext->isGranted('ROLE_SYLIUS_ADMIN') || $this->securityContext->isGranted('ROLE_PREVIOUS_ADMIN'))) { $routeParams = array('route' => 'sylius_backend_dashboard', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.administration')), 'labelAttributes' => array('icon' => 'icon-briefcase icon-large', 'iconOnly' => false)); if ($this->securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) { $routeParams = array_merge($routeParams, array('route' => 'sylius_switch_user_return', 'routeParameters' => array('username' => $this->securityContext->getToken()->getUsername(), '_switch_user' => '_exit'))); } $menu->addChild('administration', $routeParams)->setLabel($this->translate('sylius.frontend.menu.main.administration')); } return $menu; }
/** * @param PurchaseCompleteEvent $event */ public function abandonCart(PurchaseCompleteEvent $event) { if (in_array($event->getSubject()->getState(), [PaymentInterface::STATE_PENDING, PaymentInterface::STATE_PROCESSING, PaymentInterface::STATE_COMPLETED])) { $this->cartProvider->abandonCart(); return; } $event->setResponse(new RedirectResponse($this->router->generate($this->redirectTo))); }
public function process(OrderInterface $order) { $order->setCheckoutState(OrderCheckoutStates::STATE_COMPLETED); $order->setShippingState(ShipmentInterface::STATE_PENDING); $order->setPaymentState(PaymentInterface::STATE_PENDING); $order->setState(OrderInterface::STATE_CONFIRMED); $order->setOrderedAt(new \DateTime()); $this->confirmMailer->sendMail($order); $this->cartProvider->abandonCart(); }
/** * @return OrderInterface * * @throws UnexpectedTypeException */ private function getCart() { if (!$this->cartProvider->hasCart()) { return null; } $cart = $this->cartProvider->getCart(); if (!$cart instanceof OrderInterface) { throw new UnexpectedTypeException($cart, OrderInterface::class); } return $cart; }
public function saveCart(CartEvent $event) { $cart = $event->getCart(); $errors = $this->validator->validate($cart); $valid = 0 === count($errors); if ($valid) { $this->cartManager->persist($cart); $this->cartManager->flush(); $this->cartProvider->setCart($cart); } }
/** * * @param UserInterface $user */ public function merge(UserInterface $user) { $cart = $this->cartProvider->getCart(); if (!$cart->isEmpty()) { $this->userCartProvider->setCart($cart); } else { $userCart = $this->userCartProvider->getCart(); if ($userCart !== null && !$userCart->isEmpty()) { $this->cartContext->setCurrentCartIdentifier($userCart); } } $this->em->flush(); }
/** * @param InteractiveLoginEvent $interactiveLoginEvent */ public function interactiveBlame(InteractiveLoginEvent $interactiveLoginEvent) { $cart = $this->cartProvider->getCart(); if (!$cart instanceof OrderInterface) { throw new UnexpectedTypeException($cart, 'Sylius\\Component\\Core\\Model\\OrderInterface'); } $user = $interactiveLoginEvent->getAuthenticationToken()->getUser(); if (null === $user) { return; } $cart->setCustomer($user->getCustomer()); $this->cartManager->persist($cart); $this->cartManager->flush($cart); }
/** * Builds frontend cart menu. * * @return ItemInterface */ public function createCartMenu() { $menu = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'dropdown cartMenu'))); $menu->setCurrentUri($this->request->getRequestUri()); if ($this->cartProvider->hasCart()) { $cart = $this->cartProvider->getCart(); $cartTotals = array('items' => $cart->getTotalQuantity(), 'total' => $cart->getTotal()); } else { $cartTotals = array('items' => 0, 'total' => 0); } $menu->addChild('cart', array('route' => 'sylius_cart_summary', 'linkAttributes' => ['title' => $this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total']))), 'class' => 'dropdown-toggle'], 'labelAttributes' => array('icon' => 'icon-shopping-cart icon-large')))->setLabel($this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total'])))); //TODO: add items in cart $this->eventDispatcher->dispatch(MenuBuilderEvent::FRONTEND_MAIN, new MenuBuilderEvent($this->factory, $menu)); return $menu; }
/** * Builds frontend main menu. * * @return ItemInterface */ public function createMainMenu() { $menu = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'nav nav-pills'))); $menu->setCurrentUri($this->request->getRequestUri()); if ($this->cartProvider->hasCart()) { $cart = $this->cartProvider->getCart(); $cartTotals = array('items' => $cart->getTotalQuantity(), 'total' => $cart->getTotal()); } else { $cartTotals = array('items' => 0, 'total' => 0); } $menu->addChild('cart', array('route' => 'sylius_cart_summary', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total'])))), 'labelAttributes' => array('icon' => 'icon-shopping-cart icon-large')))->setLabel($this->translate('sylius.frontend.menu.main.cart', array('%items%' => $cartTotals['items'], '%total%' => $this->currencyHelper->convertAndFormatAmount($cartTotals['total'])))); if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $route = $this->request === null ? '' : $this->request->get('_route'); if (1 === preg_match('/^(sylius_account)/', $route)) { $menu->addChild('shop', array('route' => 'sylius_homepage', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.account.shop')), 'labelAttributes' => array('icon' => 'icon-th icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.account.shop')); } else { $menu->addChild('account', array('route' => 'sylius_account_profile_show', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.account')), 'labelAttributes' => array('icon' => 'icon-user icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.account')); } $menu->addChild('logout', array('route' => 'sylius_user_security_logout', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.logout')), 'labelAttributes' => array('icon' => 'icon-off icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.logout')); } else { $menu->addChild('login', array('route' => 'sylius_user_security_login', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.login')), 'labelAttributes' => array('icon' => 'icon-lock icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.login')); $menu->addChild('register', array('route' => 'sylius_user_registration', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.register')), 'labelAttributes' => array('icon' => 'icon-user icon-large', 'iconOnly' => false)))->setLabel($this->translate('sylius.frontend.menu.main.register')); } if ($this->securityContext->isGranted('ROLE_ADMINISTRATION_ACCESS') || $this->securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) { $routeParams = array('route' => 'sylius_backend_dashboard', 'linkAttributes' => array('title' => $this->translate('sylius.frontend.menu.main.administration')), 'labelAttributes' => array('icon' => 'icon-briefcase icon-large', 'iconOnly' => false)); if ($this->securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) { $routeParams = array_merge($routeParams, array('route' => 'sylius_switch_user_return', 'routeParameters' => array('username' => $this->securityContext->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; }
/** * @param GenericEvent $event */ public function handleRestrictedZone(GenericEvent $event) { $cart = $event->getSubject(); if (!$cart instanceof CartInterface) { $cart = $this->cartProvider->getCart(); } $removed = false; foreach ($cart->getItems() as $item) { if ($this->restrictedZoneChecker->isRestricted($product = $item->getProduct(), $cart->getShippingAddress())) { $cart->removeItem($item); $removed = true; $this->session->getBag('flashes')->add('error', $this->translator->trans('sylius.cart.restricted_zone_removal', ['%product%' => $product->getName()], 'flashes')); } } if ($removed) { $this->cartManager->persist($cart); $this->cartManager->flush(); } }
/** * {@inheritdoc} */ public function resolve(CartItemInterface $item, $data) { $id = $this->resolveItemIdentifier($data); $channel = $this->channelContext->getChannel(); if (!($product = $this->productRepository->findOneBy(['id' => $id, 'channels' => $channel]))) { throw new ItemResolvingException('Requested product was not found.'); } if ($this->restrictedZoneChecker->isRestricted($product)) { throw new ItemResolvingException('Selected item is not available in your country.'); } // 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() && !$product->hasVariants()) { $item->setVariant($product->getMasterVariant()); } if (null === $item->getVariant() && $product->hasVariants()) { 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->cartProvider->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; }
/** * @return OrderInterface */ protected function getCurrentCart() { return $this->cartProvider->getCart(); }
function it_should_not_abandon_cart_if_payment_status_unknown(CartProviderInterface $cartProvider, PurchaseCompleteEvent $event, PaymentInterface $payment) { $payment->getState()->willReturn(PaymentInterface::STATE_UNKNOWN); $cartProvider->abandonCart()->shouldNotBeCalled(); $event->setResponse(new RedirectResponse('/payment'))->shouldBeCalled(); $this->abandonCart($event); }
function it_does_nothing_if_there_is_no_existing_cart_on_interactive_login(CartProviderInterface $cartProvider, InteractiveLoginEvent $interactiveLoginEvent, TokenInterface $token, UserInterface $user) { $cartProvider->hasCart()->willReturn(false); $interactiveLoginEvent->getAuthenticationToken()->willReturn($token); $token->getUser()->willReturn($user); $this->onInteractiveLogin($interactiveLoginEvent); }
/** * @return bool */ public function hasCart() { return $this->cartProvider->hasCart(); }
function let(CartProviderInterface $cartProvider, CartInterface $cart) { $cartProvider->getCart()->willReturn($cart); $this->beConstructedWith($cartProvider); }
function it_throws_exception_if_provided_cart_is_not_order(CartInterface $cart, CartProviderInterface $cartProvider, Event $event) { $cartProvider->hasCart()->willReturn(true); $cartProvider->getCart()->willReturn($cart); $this->shouldThrow(new UnexpectedTypeException($cart->getWrappedObject(), OrderInterface::class))->during('recalculateCartWhileLogin', [$event]); }