Browse Source

Frontend : ajout produit au panier

master
Guillaume 4 years ago
parent
commit
5555fd5f75
7 changed files with 163 additions and 12 deletions
  1. +8
    -0
      ShopBundle/Context/OrderUtilsInterface.php
  2. +15
    -7
      ShopBundle/Controller/Frontend/CartController.php
  3. +46
    -0
      ShopBundle/Form/DataTransformer/ProductToIdTransformer.php
  4. +23
    -2
      ShopBundle/Model/OrderProduct.php
  5. +19
    -3
      ShopBundle/Model/OrderShop.php
  6. +44
    -0
      ShopBundle/Model/Visitor.php
  7. +8
    -0
      ShopBundle/Services/OrderUtils.php

+ 8
- 0
ShopBundle/Context/OrderUtilsInterface.php View File

<?php

namespace Lc\ShopBundle\Context ;

interface OrderUtilsInterface
{

}

+ 15
- 7
ShopBundle/Controller/Frontend/CartController.php View File

namespace Lc\ShopBundle\Controller\Frontend ; namespace Lc\ShopBundle\Controller\Frontend ;


use App\Form\Frontend\OrderProductsType; use App\Form\Frontend\OrderProductsType;
use CKSource\CKFinder\Response\JsonResponse;
use App\Services\OrderUtils;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface; use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderProductInterface;
use Lc\ShopBundle\Context\OrderUtilsInterface;
use Lc\ShopBundle\Context\ProductFamilyInterface; use Lc\ShopBundle\Context\ProductFamilyInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;


class CartController extends BaseController class CartController extends BaseController
{ {
protected $orderUtils ;
protected $productFamilyRepository ; protected $productFamilyRepository ;


public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils)
{ {
parent::__construct($em, $merchantUtils); parent::__construct($em, $merchantUtils);
$this->orderUtils = $orderUtils ;
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
} }


{ {
$return = [] ; $return = [] ;
$data = $request->request->all() ; $data = $request->request->all() ;
$return = $data ;
if(isset($data['order_products']['id_product_family'])) { if(isset($data['order_products']['id_product_family'])) {
$idProductFamily = $data['order_products']['id_product_family'] ; $idProductFamily = $data['order_products']['id_product_family'] ;
$productFamily = $this->productFamilyRepository->find($idProductFamily) ; $productFamily = $this->productFamilyRepository->find($idProductFamily) ;

if($productFamily) { if($productFamily) {

$form = $this->createForm(OrderProductsType::class, ['id_product_family' => $productFamily->getId()]); $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $productFamily->getId()]);
$form->handleRequest($request); $form->handleRequest($request);


if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {

$orderShop = $this->orderUtils->getOrderShopCurrent() ;
$data = $form->getData() ;
foreach($data as $orderProduct) {
if($orderProduct instanceof OrderProductInterface) {
$this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
}
}
} }
} }
} }


return new JsonResponse($return) ;
return new JsonResponse($data) ;
} }


public function addProduct() public function addProduct()

+ 46
- 0
ShopBundle/Form/DataTransformer/ProductToIdTransformer.php View File

<?php

namespace Lc\ShopBundle\Form\DataTransformer;

use Lc\ShopBundle\Context\ProductInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ProductToIdTransformer implements DataTransformerInterface
{
private $em ;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function transform($product)
{

if (null === $product) {
return '';
}

return $product->getId();
}

public function reverseTransform($productId)
{
if (!$productId) {
return;
}

$product = $this->em->getRepository($this->em->getClassMetadata(ProductInterface::class)->getName())->find($productId);

if (null === $product) {
throw new TransformationFailedException(sprintf(
'An issue with number "%s" does not exist!',
$productId
));
}

return $product;
}
}

+ 23
- 2
ShopBundle/Model/OrderProduct.php View File

/** /**
* @ORM\MappedSuperclass() * @ORM\MappedSuperclass()
*/ */
abstract class OrderProduct extends AbstractEntity implements PriceInterface
abstract class OrderProduct implements PriceInterface
{ {
use PriceTrait ; use PriceTrait ;


{ {
if($this->getTitle()) { if($this->getTitle()) {
return $this->getTitle(); return $this->getTitle();
}else{
}
else{
return $this->getProduct()->getProductFamily()->getTitle(). ' - '.$this->getProduct()->getTitle(); return $this->getProduct()->getProductFamily()->getTitle(). ' - '.$this->getProduct()->getTitle();
} }
}

public function getTitleOrderShop()
{
$product = $this->getProduct() ;
$productFamily = $product->getProductFamily() ;
$titleProduct = $product->getTitle() ;
$titleProductFamily = $productFamily->getTitle() ;

if(strlen($titleProduct) > 0 && strlen($titleProductFamily) > 0) {
$title = $titleProductFamily.' - '.$titleProduct ;
}
else {
$title = strlen($titleProduct) ? $titleProduct : $titleProductFamily ;
}

if($productFamily->hasProductsWithVariousWeight()) {
$title .= ' - '.$product->getQuantityLabelInherited() ;
}


return $title ;
} }


public function getOrderShop(): ?OrderShop public function getOrderShop(): ?OrderShop

+ 19
- 3
ShopBundle/Model/OrderShop.php View File



namespace Lc\ShopBundle\Model; namespace Lc\ShopBundle\Model;


use App\Entity\Visitor;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* @ORM\MappedSuperclass() * @ORM\MappedSuperclass()
*/ */
abstract class OrderShop extends AbstractEntity implements FilterMerchantInterface
abstract class OrderShop implements FilterMerchantInterface
{ {


/** /**


/** /**
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserInterface", inversedBy="orders") * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserInterface", inversedBy="orders")
* @ORM\JoinColumn(nullable=false)
*/ */
protected $user; protected $user;


/**
* @ORM\ManyToOne(targetEntity="App\Entity\Visitor", inversedBy="orders")
*/
protected $visitor;

/** /**
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\AddressInterface", inversedBy="order") * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\AddressInterface", inversedBy="order")
* @ORM\JoinColumn(nullable=false)
*/ */
protected $invoiceAddress; protected $invoiceAddress;




return $this; return $this;
} }

public function getVisitor(): ?Visitor
{
return $this->visitor;
}

public function setVisitor(?Visitor $visitor): self
{
$this->visitor = $visitor;

return $this;
}
} }

+ 44
- 0
ShopBundle/Model/Visitor.php View File



namespace Lc\ShopBundle\Model; namespace Lc\ShopBundle\Model;


use App\Entity\OrderShop;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;


/** /**
*/ */
protected $totalVisit; protected $totalVisit;


/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderShop", mappedBy="visitor")
*/
protected $orders;

public function __construct()
{
$this->orders = new ArrayCollection();
}

public function getCookie(): ?string public function getCookie(): ?string
{ {
return $this->cookie; return $this->cookie;


return $this; return $this;
} }

/**
* @return Collection|OrderShop[]
*/
public function getOrders(): Collection
{
return $this->orders;
}

public function addOrder(OrderShop $order): self
{
if (!$this->orders->contains($order)) {
$this->orders[] = $order;
$order->setVisitor($this);
}

return $this;
}

public function removeOrder(OrderShop $order): self
{
if ($this->orders->contains($order)) {
$this->orders->removeElement($order);
// set the owning side to null (unless already changed)
if ($order->getVisitor() === $this) {
$order->setVisitor(null);
}
}

return $this;
}
} }

+ 8
- 0
ShopBundle/Services/OrderUtils.php View File

<?php

namespace Lc\ShopBundle\Services ;

class OrderUtils
{

}

Loading…
Cancel
Save