CartController.php 3.0KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use App\Form\Frontend\OrderProductsType;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\OrderProductInterface;
  7. use Lc\ShopBundle\Context\OrderUtilsInterface;
  8. use Lc\ShopBundle\Context\ProductFamilyInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Security;
  12. class CartController extends BaseController
  13. {
  14. protected $orderUtils ;
  15. protected $productFamilyRepository ;
  16. protected $orderProductRepository ;
  17. protected $productFamily ;
  18. protected $orderProducts = [] ;
  19. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils)
  20. {
  21. parent::__construct($em, $security, $merchantUtils);
  22. $this->orderUtils = $orderUtils ;
  23. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  24. $this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
  25. }
  26. public function addProductFamily(Request $request)
  27. {
  28. $return = [] ;
  29. $data = $request->request->all() ;
  30. if(isset($data['order_products']['id_product_family'])) {
  31. $idProductFamily = $data['order_products']['id_product_family'] ;
  32. $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  33. if($this->productFamily) {
  34. $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
  35. $form->handleRequest($request);
  36. if ($form->isSubmitted() && $form->isValid()) {
  37. $orderShop = $this->orderUtils->getOrderShopCurrent() ;
  38. $data = $form->getData() ;
  39. foreach($data as $orderProduct) {
  40. if($orderProduct instanceof OrderProductInterface) {
  41. $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
  42. if($orderProduct->getQuantityorder() > 0) {
  43. $this->orderProducts[] = $orderProduct ;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return new JsonResponse($return) ;
  51. }
  52. }