src/Service/CartHelper.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. class CartHelper {
  4.     /**
  5.      * constructor
  6.      *
  7.      * @param \App\Repository\CourseRepository $courseRepository
  8.      * @param \App\Repository\VoucherRepository $voucherRepository
  9.      * @param \App\Repository\OrderVoucherRepository $orderVoucherRepository
  10.      * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  11.      * @param \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $params
  12.      * @param \Doctrine\ORM\EntityManagerInterface $entityManager
  13.      */
  14.     public function __construct(
  15.         \Symfony\Component\Security\Core\Security $security,
  16.         \App\Repository\CourseRepository $courseRepository,
  17.         \App\Repository\VoucherRepository $voucherRepository,
  18.         \App\Repository\OrderVoucherRepository $orderVoucherRepository,
  19.         \Symfony\Component\HttpFoundation\RequestStack $requestStack,
  20.         \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $params,
  21.         \Doctrine\ORM\EntityManagerInterface $entityManager
  22.     ) {
  23.         $this->security $security;
  24.         $this->requestStack $requestStack;
  25.         $this->params $params;
  26.         $this->courseRepository $courseRepository;
  27.         $this->voucherRepository $voucherRepository;
  28.         $this->orderVoucherRepository $orderVoucherRepository;
  29.         $this->entityManager $entityManager;
  30.     }
  31.     /**
  32.      * Find Valid Voucher for user by code
  33.      *
  34.      * @param string $code
  35.      * @return \App\Entity\Voucher|void
  36.      */
  37.     public function checkValidVoucherCode(
  38.         string $code
  39.     ) {
  40.         $voucher $this->voucherRepository->findOneBy(['code' => $code]);
  41.         if ($voucher) :
  42.             if (!$voucher->getIsEnabled()) return false;
  43.             $dateNow = new \DateTime();
  44.             if ($voucher->getValidFrom() > $dateNow) return false;
  45.             if ($voucher->getValidUntil() < $dateNow) return false;
  46.             if ($voucher->getVoucherProductType() == \App\DBAL\Types\VoucherProductType::SELECTED_PRODUCTS) :
  47.                 $found false;
  48.                 $cart $this->getCart();
  49.                 $cartIDs = [];
  50.                 foreach ($cart['products'] as $product) :
  51.                     $cartIDs[] = $product['id'];
  52.                 endforeach;
  53.                 foreach ($voucher->getCourses() as $product) :
  54.                     if (in_array($product->getID(), $cartIDs)) $found true;
  55.                 endforeach;
  56.                 if (!$found) return false;
  57.             endif;
  58.             $user $this->security->getUser();
  59.             if ($voucher->getVoucherType() == \App\DBAL\Types\VoucherType::ONE_TOTAL) :
  60.                 $exist $this->orderVoucherRepository->findVoucherUsed($voucher);
  61.                 if ($exist) :
  62.                     return false;
  63.                 endif;
  64.             elseif ($voucher->getVoucherType() == \App\DBAL\Types\VoucherType::ONE_PER_USER) :
  65.                 $exist $this->orderVoucherRepository->findVoucherUsed($voucher$user);
  66.                 if ($exist) :
  67.                     return false;
  68.                 endif;
  69.             endif;
  70.             return $voucher;
  71.         endif;
  72.         return false;
  73.     }
  74.     /**
  75.      * Update Session Cart
  76.      *
  77.      * @return void
  78.      */
  79.     private function updateSessionCart() {
  80.         $session $this->requestStack->getSession();
  81.         $products_in_cart $session->get('cart', []);
  82.         $products = [];
  83.         $totalQty 0;
  84.         $subtotal 0.00;
  85.         if ($products_in_cart) :
  86.             $courses $this->courseRepository->findBy(["id" => array_keys($products_in_cart), "isEnabled" => true]);
  87.             foreach ($courses as $product) :
  88.                 $products[] = [
  89.                     "id" => $product->getID(),
  90.                     "title" => $product->getTitle(),
  91.                     "price" => $product->getPriceCost(),
  92.                     "final" => $product->getPriceFinal(),
  93.                     "qty" => (int)$products_in_cart[$product->getID()],
  94.                 ];
  95.                 $totalQty += (int)$products_in_cart[$product->getID()];
  96.                 $subtotal += (float)$product->getPriceFinal() * (int)$products_in_cart[$product->getID()];
  97.             endforeach;
  98.         endif;
  99.         $session->set('minicart', [
  100.             "products" => $products,
  101.             "subtotal" => $subtotal,
  102.             "qty" => $totalQty
  103.         ]);
  104.     }
  105.     /**
  106.      * Add Product to cart
  107.      *
  108.      * @param \App\Entity\Course|null $course
  109.      * @param Int $quantity
  110.      * @return void
  111.      */
  112.     public function addProduct(?\App\Entity\Course $courseInt $quantity) {
  113.         $session $this->requestStack->getSession();
  114.         $cart $session->get('cart', []);
  115.         if ($course && $quantity 0) :
  116.             if (array_key_exists($course->getID(), $cart)) :
  117.                 //$cart[$course->getID()] += $quantity;
  118.                 $cart[$course->getID()] = 1;
  119.             else :
  120.                 //$cart[$course->getID()] = $quantity;
  121.                 $cart[$course->getID()] = 1;
  122.             endif;
  123.             $session->set('cart'$cart);
  124.             $this->updateSessionCart();
  125.         endif;
  126.     }
  127.     /**
  128.      * Remore product from cart
  129.      *
  130.      * @param \App\Entity\Course|null $course
  131.      * @return void
  132.      */
  133.     public function removeProduct(?\App\Entity\Course $course) {
  134.         $session $this->requestStack->getSession();
  135.         $cart $session->get('cart', []);
  136.         if ($course) :
  137.             if (array_key_exists($course->getID(), $cart)) :
  138.                 unset($cart[$course->getID()]);
  139.                 $session->set('cart'$cart);
  140.                 $this->updateSessionCart();
  141.             endif;
  142.         endif;
  143.     }
  144.     /**
  145.      * Add Voucher to cart
  146.      *
  147.      * @param \App\Entity\Voucher|null $voucher
  148.      * @return void
  149.      */
  150.     public function addVoucher(?\App\Entity\Voucher $voucher) {
  151.         $session $this->requestStack->getSession();
  152.         $sessionVoucher $session->get('voucher'null);
  153.         if ($voucher) :
  154.             if ($sessionVoucher) :
  155.                 $sessionVoucher = [
  156.                     "id" => $voucher->getID(),
  157.                     "title" => $voucher->getTitle(),
  158.                     "code" => $voucher->getCode()
  159.                 ];
  160.                 $session->set('voucher'$sessionVoucher);
  161.                 $this->updateSessionCart();
  162.             else :
  163.                 $sessionVoucher = [
  164.                     "id" => $voucher->getID(),
  165.                     "title" => $voucher->getTitle(),
  166.                     "code" => $voucher->getCode()
  167.                 ];
  168.                 $session->set('voucher'$sessionVoucher);
  169.                 $this->updateSessionCart();
  170.             endif;
  171.         endif;
  172.     }
  173.     /**
  174.      * Remove Voucher from cart
  175.      *
  176.      * @param \App\Entity\Voucher|null $voucher
  177.      * @return void
  178.      */
  179.     public function removeVoucher(?\App\Entity\Voucher $voucher) {
  180.         $session $this->requestStack->getSession();
  181.         $sessionVoucher $session->get('voucher'null);
  182.         if ($voucher && $sessionVoucher) :
  183.             if (isset($sessionVoucher['id']) && $sessionVoucher['id'] == $voucher->getID()) :
  184.                 $session->remove('voucher');
  185.                 $this->updateSessionCart();
  186.             endif;
  187.         endif;
  188.     }
  189.     /**
  190.      * Set unique cart session id
  191.      *
  192.      * @param string $uniqueID
  193.      * @return void
  194.      */
  195.     public function setUniqueID(string $uniqueID) {
  196.         $session $this->requestStack->getSession();
  197.         $session->set('uniqueCartID'$uniqueID);
  198.     }
  199.     /**
  200.      * Clear Current Cart
  201.      *
  202.      * @return void
  203.      */
  204.     public function clearCart() {
  205.         $session $this->requestStack->getSession();
  206.         $session->remove('uniqueCartID');
  207.         $session->remove('voucher');
  208.         $session->remove('cart');
  209.         $session->remove('minicart');
  210.     }
  211.     public function getVoucher() {
  212.         $session $this->requestStack->getSession();
  213.         $sessionVoucher $session->get('voucher'null);
  214.         $voucher $sessionVoucher?$this->checkValidVoucherCode($sessionVoucher['code']):null;
  215.         $voucherData null;
  216.         if ($voucher) :
  217.             $voucherData = [
  218.                 'id' => $voucher->getID(),
  219.                 'code' => $voucher->getCode(),
  220.                 'value' => $voucher->getDiscountValue(),
  221.                 'type' => $voucher->getDiscountType(),
  222.                 'for' => $voucher->getDiscountFor(),
  223.                 'productType' => $voucher->getVoucherProductType(),
  224.                 'products' => []
  225.             ];
  226.             if ($voucher->getVoucherProductType() == \App\DBAL\Types\VoucherProductType::SELECTED_PRODUCTS) :
  227.                 foreach ($voucher->getCourses() as $course) :
  228.                     $voucherData['products'][] = $course->getID();
  229.                 endforeach;
  230.             endif;
  231.         endif;
  232.         return $voucherData;
  233.     }
  234.     /**
  235.      * Get Current Cart
  236.      *
  237.      * @return array
  238.      */
  239.     public function getCart() {
  240.         $session $this->requestStack->getSession();
  241.         $products_in_cart $session->get('cart', []);
  242.         $products = [];
  243.         $subtotal 0.00;
  244.         $totalQty 0;
  245.         if ($products_in_cart) :
  246.             $courses $this->courseRepository->findBy(["id" => array_keys($products_in_cart), "isEnabled" => true]);
  247.             foreach ($courses as $product) :
  248.                 $products[] = [
  249.                     "id" => $product->getID(),
  250.                     "title" => $product->getTitle(),
  251.                     "price" => $product->getPriceCost(),
  252.                     "final" => $product->getPriceFinal(),
  253.                     "qty" => (int)$products_in_cart[$product->getID()]
  254.                 ];
  255.                 $totalQty += (int)$products_in_cart[$product->getID()];
  256.                 $subtotal += (float)$product->getPriceFinal() * (int)$products_in_cart[$product->getID()];
  257.             endforeach;
  258.         endif;
  259.         $uniqueID $session->get('uniqueCartID'null);
  260.         return [
  261.             "uniqueID" => $uniqueID,
  262.             "products" => $products,
  263.             "subtotal" => $subtotal,
  264.             "qty" => $totalQty
  265.         ];
  266.     }
  267. }