src/Controller/CheckoutController.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Model\DataObject;
  4. use Pimcore\Controller\FrontendController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\ICheckoutStep;
  7. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  8. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. // class CheckoutController extends \Website\Controller\CartAware
  11. /**
  12.      * @Route("/{_locale}/checkout")    
  13. */
  14. class CheckoutController extends CartAware
  15. {
  16.     /**
  17.      * @inheritDoc
  18.      */
  19.     public function onKernelController(FilterControllerEvent $event)
  20.     {
  21.         parent::onKernelController($event);
  22.         //navigation and breadcrumbs
  23.         $this->view->hideNav true;
  24.         $translator $this->get('translator');
  25.         /** @var callable $placeholder */
  26.         $placeholder $this->get('pimcore.templating.view_helper.placeholder');
  27.         $placeholder('addBreadcrumb')->append([
  28.             'parentId' => $this->document->getId(),
  29.             'id' => 'cart',
  30.             'label' => $translator->trans('general.mycart'),
  31.             'url' => $this->generateUrl('cart', ['action' => 'list'])
  32.         ]);
  33.         $placeholder('addBreadcrumb')->append([
  34.             'parentId' => 'cart',
  35.             'id' => 'checkout',
  36.             'label' => $translator->trans('general.checkout')
  37.         ]);
  38.         $cart $this->getCart();
  39.         $this->view->cart $cart;
  40.         $checkoutManager Factory::getInstance()->getCheckoutManager($cart);
  41.         //check, if currently active payment is available
  42.         if ($checkoutManager->hasActivePayment()) {
  43.             $url $this->generateUrl('action', [
  44.                 'controller' => 'payment',
  45.                 'action' => 'payment',
  46.                 'prefix' => strtolower($this->locale)
  47.             ]);
  48.             $event->setController(function () use ($url) {
  49.                 return new RedirectResponse($url);
  50.             });
  51.         }
  52.     }
  53.     /**
  54.      * init controller
  55.      */
  56.     /*public function init()
  57.     {
  58.         parent::init();
  59.         //navigation and breadcrumbs
  60.         $this->view->hideNav = true;
  61.         $translator = new \Pimcore\Translate\Website(Zend_Registry::get("Zend_Locale"));
  62.         $this->view->placeholder('addBreadcrumb')->append([
  63.             'parentId' => $this->document->getId()
  64.             , 'id' => "cart"
  65.             , 'label' => $translator->translate("general.mycart")
  66.             , 'url' => $this->view->url(["prefix" => $this->locale, "action" => "list"], "cart", true)
  67.         ]);
  68.         $this->view->placeholder('addBreadcrumb')->append([
  69.             'parentId' => "cart"
  70.             , 'id' => "checkout"
  71.             , 'label' => $translator->translate("general.checkout")
  72.         ]);
  73.         $cart = $this->getCart();
  74.         $this->view->cart = $cart;
  75.         $checkoutManager = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getCheckoutManager($cart);
  76.         //check, if currently active payment is available
  77.         if($checkoutManager->hasActivePayment()) {
  78.            return $this->redirect($this->view->url(["controller" => "payment", "action" => "payment", "prefix" => $this->locale], "action", true));
  79.         }
  80.     }*/
  81.     
  82.     /**
  83.      * @Route("/delivery", name="checkout")
  84.      *
  85.      * @param Request $request
  86.      * @param Factory $factory
  87.      */
  88.     public function deliveryAction(Request $request,Factory $factory)
  89.     {
  90.         // init
  91.         //$this->layout()->setLayout('cart');
  92.         //$this->enableLayout();
  93.         $params = [];
  94.         $cart $this->getCart();
  95.         $params['currentStep'] = 'delivery';
  96.         $params['locale'] = strtolower($request->getLocale());
  97.         $params['params'] = $request->request->all();
  98.         $params['preview'] = $request->get('pimcore_preview',null);
  99.         // Set Us as default country
  100.         $defaultCountry DataObject\AvailabilityCountry::getByPath('/availability-countries/us');
  101.         if(isset($this->country)){
  102.             $availableCountry $this->country;
  103.         }else{
  104.             $availableCountry $defaultCountry;
  105.             $params['country']  = $defaultCountry;
  106.         }
  107.         //$authNamespace = new Zend_Session_Namespace('Zend_Auth'); 
  108.         //todo 
  109.          $session $request->getSession();  
  110.          $authNamespace $session->getBag('espirit');
  111.          if ($authNamespace->get('customeraccount')) {
  112.              $params['otheremailaddress'] = $authNamespace->get('customeraccount')->getEmail();
  113.          }
  114.          
  115.         //if cart empty, redirect to cart list
  116.         if(count($cart->getItems()) == 0) {
  117.             //$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  118.             //return $this->redirect($urlHelper(array('action' => 'list', 'prefix' => strtolower($this->locale)), 'cart', true));
  119.             return $this->redirectToRoute('shop-cart-detail');
  120.         }
  121.         //$checkoutManager = Factory::getInstance()->getCheckoutManager( $cart );
  122.         $checkoutManager $factory->getCheckoutManager($cart);
  123.         $deliveryAddress $checkoutManager->getCheckoutStep('deliveryaddress');
  124.         //$trackingManager = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getTrackingManager();
  125.         //$trackingManager->trackCheckoutStep($deliveryAddress, $cart, 1);
  126.         $params['deliveryAddress'] = $deliveryAddress;
  127.         // check errors
  128.         //$params['errors'] = $this->getParam('error') ? explode(',', $this->getParam('error')) : array();
  129.         $params['errors']  = 0;
  130.         // update delivery address
  131.         if($request->getMethod() == 'POST')
  132.         {
  133.             $address = new \stdClass();
  134.             // save address if we have no errors
  135.             //TODO
  136.             //if(@count($params['errors']) === 0)
  137.             //{
  138.                 // Set the dataformat items
  139.                 //$authNamespace = new Zend_Session_Namespace('Zend_Auth');
  140.                 //TODO
  141.                  if($request->get('otheremailcheck'))
  142.                  {
  143.                      $authNamespace->set('otheremail',$request->get("otheremail"));
  144.                  }
  145.                  $authNamespace->set('datafileformat',$request->get("datafileformat"));
  146.                  $authNamespace->set('photographyfileformat',$request->get("photographyfileformat"));
  147.                  $authNamespace->set('photographyfiledimension',$request->get("photographyfiledimension"));
  148.                  $authNamespace->set('lifestyleassets',$request->get("lifestyleAssets"));
  149.                  $authNamespace->set('fileDownloadName',$request->get("file_download_name"));
  150.                 // commit step
  151.                 $checkoutManager->commitStep($deliveryAddress$address);
  152.                 // goto next step
  153.                 //$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  154.                //return $this->redirect($urlHelper(["action" => "confirm", "prefix" => strtolower($this->locale)], "checkout", true));
  155.                return $this->redirectToRoute('checkout-confirm');
  156.             //}
  157.             
  158.         }
  159.         $params['cart'] = $cart;
  160.         $params['deviceDetector'] = \Pimcore\Tool\DeviceDetector::getInstance();
  161.         return $this->render('Checkout/delivery.html.twig',$params);
  162.     }
  163.     /**
  164.      * @Route("/confirm", name="checkout-confirm")
  165.      *
  166.      * @param Request $request
  167.      * 
  168.      */
  169.     public function confirmAction(Request $request)
  170.     {
  171.         // init
  172.         // $authNamespace = new Zend_Session_Namespace('Zend_Auth');
  173.         $params = [];
  174.         $params['locale'] = strtolower($request->getLocale());
  175.         $params['params'] = $request->request->all();
  176.         // Set Us as default country
  177.         $defaultCountry DataObject\AvailabilityCountry::getByPath('/availability-countries/us');
  178.         if(isset($this->country)){
  179.             $availableCountry $this->country;
  180.         }else{
  181.             $availableCountry $defaultCountry;
  182.             $params['country']  = $defaultCountry;
  183.         }
  184.        //TODO
  185.         $session $request->getSession();  
  186.         $authNamespace $session->getBag('espirit');
  187.         $params['authNamespace'] = $authNamespace;
  188.        // END TODO 
  189.         //$this->layout()->setLayout('cart');
  190.         //$this->enableLayout();
  191.         $cart $this->getCart();
  192.         $params['currentStep'] = 'confirm';
  193.         $params['showSummary'] = true;
  194.         //if cart empty, redirect to cart list
  195.         //TODo
  196.         
  197.         if(count($cart->getItems()) == 0) {
  198.             //$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  199.             //return $this->redirect($urlHelper(array('action' => 'list', 'prefix' => strtolower($this->locale)), 'cart', true));
  200.             $url $this->generateUrl('shop-cart-detail');
  201.             return $this->redirect($url);
  202.         }
  203.         $checkoutManager Factory::getInstance()->getCheckoutManager$cart );
  204.         //needed for sidebar
  205.         
  206.         $deliveryAddress $checkoutManager->getCheckoutStep('deliveryaddress');
  207.         $params['deliveryAddress'] = $deliveryAddress;
  208.         $payment $checkoutManager->getPayment();
  209.         // check errors
  210.         $params['errors'] = array();
  211.         if($request->get('error') != '') {
  212.             $params['errors'] = explode(','$request->get('error'));
  213.         }
  214.         $confirmStep $checkoutManager->getCheckoutStep('confirm');
  215.         // go to payment
  216.         if($request->getMethod() == 'POST')
  217.         {
  218.             //TODO
  219.             /*if (!$authNamespace->get('customeraccount')) {
  220.                 $urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  221.                 return $this->redirect($urlHelper(["controller" => "account", "action" => "sign-in", "prefix" => strtolower($this->locale), "ns" => "confirm"], "action"));
  222.             }*/
  223.             if($request->get('agb-accepted'))
  224.             {
  225.                 $checkoutManager->commitStep($confirmSteptrue);
  226.                 //$this->downloadAction();
  227.                 //$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  228.                 //return $this->redirect($urlHelper(["controller" => "cart", "action" => "download", "prefix" => strtolower($this->locale), true], "action"));
  229.                 //if($payment) {
  230.                 //    $this->redirect($this->view->url(["controller" => "payment", "action" => "payment", "prefix" => $this->language, true], "action"));
  231.                 //} else {
  232.                 //    $order = $checkoutManager->commitOrder();
  233.                 //}
  234.         $url strtolower($this->generateUrl('cart-download'));
  235.                 return $this->redirect($url);
  236.             }
  237.             else
  238.             {
  239.                 $params['errors'][] = 'terms';
  240.             }
  241.         }
  242.         $params['cart'] = $cart;
  243.         $params['deviceDetector'] = \Pimcore\Tool\DeviceDetector::getInstance();
  244.         return $this->render('Checkout/confirm.html.twig',$params);
  245.     }
  246.     /**
  247.      * order completed
  248.      */
  249.     public function completedAction(Request $request)
  250.     {
  251.         // init
  252.         //$this->enableLayout();
  253.         $this->view->order \Pimcore\Model\DataObject\OnlineShopOrder::getById$request->get('id') );
  254.         $itemKeyUniversal \Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutManager::TRACK_ECOMMERCE_UNIVERSAL "_" $this->view->order->getOrdernumber();
  255.         $environment \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getEnvironment();
  256.         $codeUniversal $environment->getCustomItem($itemKeyUniversal);
  257.         if(!empty($codeUniversal)) {
  258.             $this->view->gacodeUniversal $codeUniversal;
  259.             $environment->removeCustomItem($itemKeyUniversal);
  260.             $environment->save();
  261.         }
  262.     }
  263.     
  264.     public function downloadAction(Request $request)
  265.     {
  266.         //$this->enableLayout();
  267.         $this->view->hideNav true;
  268.         $cart $this->getCart();
  269.         $this->view->cart $cart;
  270.         //if cart empty, redirect to cart list
  271.         if(count($cart->getItems()) == 0) {
  272.             $urlHelper \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
  273.             return $this->redirect($urlHelper(array('action' => 'list''prefix' => strtolower($this->locale)), 'cart'true));
  274.         }
  275.         $checkoutManager \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getCheckoutManager$cart );
  276.         $order $checkoutManager->commitOrder();
  277.         
  278.         
  279.         
  280.         //$authNamespace = new Zend_Session_Namespace('Zend_Auth');
  281.         $session $this->get('session');
  282.         $authNamespace $session->getBag('espirit');
  283.         $order->setcustomer($authNamespace->get('customeraccount'));
  284.         $order->setdatafileformat($authNamespace->get('datafileformat'));
  285.         $order->setotheremail($authNamespace->get('otheremail'));
  286.         $order->setphotographyfileformat($authNamespace->get('photographyfileformat'));
  287.         $order->setphotographyfiledimension($authNamespace->get('photographyfiledimension'));
  288.         $order->save();
  289.         //Send the Email
  290.         $params = array();
  291.         $params["order"] = $order;
  292.         $params["customer"] = $order->getCustomer();
  293.         $params["ordernumber"] = $order->getOrdernumber();
  294.         $mail = new \Pimcore\Mail(array("document" => "/en/emails/download-confirmation""params" => $params));
  295.         if($order->getCustomer()) {
  296.             $mail->setIgnoreDebugMode(true);
  297.             //$mail->setDefaultFrom("contact@beamsuntory.com","Product Download");
  298.             $mail->addTo($order->getCustomer()->getEmail());
  299.             if ($authNamespace->get('otheremail')) {
  300.                 $toemails explode(","$authNamespace->get('otheremail'));
  301.                 foreach ($toemails as $toemail) {
  302.                     if (!empty( $toemail )) $mail->addTo$toemail );
  303.                 }
  304.             }
  305.             $mail->send();
  306.         } else {
  307.             \Logger::err("No Customer found!");
  308.         }
  309.         // init
  310.         $this->view->order \Pimcore\Model\DataObject\OnlineShopOrder::getById$order->getId() );
  311.         $itemKeyUniversal \Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutManager::TRACK_ECOMMERCE_UNIVERSAL "_" $order->getOrdernumber();
  312.         $environment \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getEnvironment();
  313.         $codeUniversal $environment->getCustomItem($itemKeyUniversal);
  314.         if(!empty($codeUniversal)) {
  315.             $this->view->gacodeUniversal $codeUniversal;
  316.             $environment->removeCustomItem($itemKeyUniversal);
  317.             $environment->save();
  318.         }
  319.     }
  320. }