src/Controller/CheckoutController.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Controller;
  15. use App\Form\DeliveryAddressFormType;
  16. use App\Website\Navigation\BreadcrumbHelperService;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  18. use Pimcore\Controller\FrontendController;
  19. use Pimcore\Model\DataObject\OnlineShopOrder;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. class CheckoutController extends FrontendController
  26. {
  27.     /**
  28.      * @Route("/checkout-address", name="shop-checkout-address")
  29.      *
  30.      * @param Factory $factory
  31.      * @param Request $request
  32.      * @param BreadcrumbHelperService $breadcrumbHelperService
  33.      * @param Factory $ecommerceFactory
  34.      *
  35.      * @return Response|RedirectResponse
  36.      */
  37.     public function checkoutAddressAction(Factory $factoryRequest $requestBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactory)
  38.     {
  39.         $cartManager $factory->getCartManager();
  40.         $cart $cartManager->getOrCreateCartByName('cart');
  41.         $checkoutManager $factory->getCheckoutManager($cart);
  42.         $deliveryAddress $checkoutManager->getCheckoutStep('deliveryaddress');
  43.         $deliveryAddressDataArray $this->fillDeliveryAddressFromCustomer($deliveryAddress->getData());
  44.         $form $this->createForm(DeliveryAddressFormType::class, $deliveryAddressDataArray, []);
  45.         $form->handleRequest($request);
  46.         $breadcrumbHelperService->enrichCheckoutPage();
  47.         if ($request->getMethod() == Request::METHOD_POST) {
  48.             $address = new \stdClass();
  49.             $formData $form->getData();
  50.             foreach ($formData as $key => $value) {
  51.                 $address->{$key} = $value;
  52.             }
  53.             // save address if we have no errors
  54.             if (count($form->getErrors()) === 0) {
  55.                 // commit step
  56.                 $checkoutManager->commitStep($deliveryAddress$address);
  57.                 //TODO remove this - only needed, because one step only is not supported by the framework right now
  58.                 $confirm $checkoutManager->getCheckoutStep('confirm');
  59.                 $checkoutManager->commitStep($confirmtrue);
  60.                 return $this->redirectToRoute('shop-checkout-payment');
  61.             }
  62.         }
  63.         $trackingManager $ecommerceFactory->getTrackingManager();
  64.         $trackingManager->trackCheckoutStep($deliveryAddress$cart1);
  65.         return $this->render('checkout/checkout_address.html.twig', [
  66.             'cart' => $cart,
  67.             'form' => $form->createView(),
  68.         ]);
  69.     }
  70.     /**
  71.      * @param $deliveryAddress
  72.      *
  73.      * @return array|null
  74.      */
  75.     protected function fillDeliveryAddressFromCustomer($deliveryAddress)
  76.     {
  77.         $user $this->getUser();
  78.         $deliveryAddress = (array) $deliveryAddress;
  79.         if ($user) {
  80.             if ($deliveryAddress === null) {
  81.                 $deliveryAddress = [];
  82.             }
  83.             $params = ['email''firstname''lastname''street''zip''city''countryCode'];
  84.             foreach ($params as $param) {
  85.                 if (empty($deliveryAddress[$param])) {
  86.                     $deliveryAddress[$param] = $user->{'get' ucfirst($param)}();
  87.                 }
  88.             }
  89.         }
  90.         return $deliveryAddress;
  91.     }
  92.     /**
  93.      * @Route("/checkout-completed", name="shop-checkout-completed")
  94.      *
  95.      * @param SessionInterface $session
  96.      * @param Factory $ecommerceFactory
  97.      *
  98.      * @return Response
  99.      */
  100.     public function checkoutCompletedAction(SessionInterface $sessionFactory $ecommerceFactory)
  101.     {
  102.         $orderId $session->get('last_order_id');
  103.         $order OnlineShopOrder::getById($orderId);
  104.         $trackingManager $ecommerceFactory->getTrackingManager();
  105.         $trackingManager->trackCheckoutComplete($order);
  106.         return $this->render('checkout/checkout_completed.html.twig', [
  107.             'order' => $order,
  108.             'hideBreadcrumbs' => true
  109.         ]);
  110.     }
  111.     /**
  112.      * @param Request $request
  113.      *
  114.      * @return Response
  115.      */
  116.     public function confirmationMailAction(Request $request)
  117.     {
  118.         $order $request->get('order');
  119.         if ($request->get('order-id')) {
  120.             $order OnlineShopOrder::getById($request->get('order-id'));
  121.         }
  122.         return $this->render('checkout/confirmation_mail.html.twig', [
  123.             'order' => $order,
  124.             'ordernumber' => $request->get('ordernumber')
  125.         ]);
  126.     }
  127. }