custom/plugins/KmPitchPrint/src/Subscribers/MoveToWishlistSubscriber.php line 100

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace KmPitchPrint\Subscribers;
  3. use KmPitchPrint\Events\MoveToWishlistEvent;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerWishlistProduct\CustomerWishlistProductEntity;
  5. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use KmPitchPrint\Services\ProductPitchPrintService;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  14. use  Swp\ProductOptionsSix\Core\Content\ProductOptions\SalesChannel\ProductOptionsAddToCart;
  15. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  16. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  17. use KmPitchPrint\Controller\ProductOptionsSix;
  18. use Shopware\Core\Checkout\Customer\Event\WishlistProductAddedEvent;
  19. use Shopware\Core\Checkout\Customer\Exception\DuplicateWishlistProductException;
  20. class MoveToWishlistSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var AbstractAddWishlistProductRoute
  24.      */
  25.     private AbstractAddWishlistProductRoute $addWishlistRoute;
  26.     /**
  27.      * @var $pitchPrintRepository SalesChannelRepository
  28.      */
  29.     private $pitchPrintRepository;
  30.     /**
  31.      * @var $productPitchPrintService ProductPitchPrintService
  32.      */
  33.     private $productPitchPrintService;
  34.     /**
  35.      * @var EntityRepository
  36.      */
  37.     private $productRepository;
  38.     /**
  39.      * @var CartService
  40.      */
  41.     private $cartService;
  42.     private $customerWishlistRepository;
  43.     /**
  44.      * @var ProductOptionsSix
  45.      */
  46.     private $productOptionsSix;
  47.     /**
  48.      * @var string
  49.      */
  50.     private $wishlistProductId;
  51.     public function __construct(
  52.         EntityRepository $pitchPrintRepository,
  53.         ProductPitchPrintService  $productPitchPrintService,
  54.         EntityRepository $productRepository,
  55.         ProductOptionsAddToCart  $productOptionsAddToCart,
  56.         CartService $cartService,
  57.         EntityRepository $customerWishlistRepository,
  58.         ProductOptionsSix $productOptionsSix,
  59.         AbstractAddWishlistProductRoute $addWishlistRoute
  60.     )
  61.     {
  62.         $this->pitchPrintRepository $pitchPrintRepository;
  63.         $this->productPitchPrintService $productPitchPrintService;
  64.         $this->productRepository $productRepository;
  65.         $this->productOptionsAddToCart $productOptionsAddToCart;
  66.         $this->cartService $cartService;
  67.         $this->customerWishlistRepository $customerWishlistRepository;
  68.         $this->productOptionsSix =$productOptionsSix;
  69.         $this->addWishlistRoute $addWishlistRoute;
  70.     }
  71.     public static function getSubscribedEvents(): array
  72.     {
  73.         return [
  74.             MoveToWishlistEvent::class => 'moveToWishlist',
  75.             WishlistProductAddedEvent::class => 'setWishlistProductId'
  76.         ];
  77.     }
  78.     public function setWishlistProductId(WishlistProductAddedEvent $event){
  79.        $this->wishlistProductId $event->getWishlistId();
  80.     }
  81.     public function moveToWishlist(MoveToWishlistEvent $event)
  82.     {
  83.         $productId $event->getProductId();
  84.         $quantity $event->getQuantity();
  85.         $context $event->getContext();
  86.         $request $event->getRequest();
  87.         $requestDataBag $event->getRequestDataBag();
  88.         $payload $event->getPayload();
  89. #dump($payload);exit;
  90.         #if(!$request->cookies->get('wishlist-enabled')){
  91.         #    return ;
  92.         #}
  93.         $cookieId $request->cookies->get('pp_id');
  94.         $caltulatedPrice $this->productOptionsSix->addOption($requestDataBag$event->getSalesChannelContext());
  95.         $caltulatedPrice json_decode($caltulatedPrice->getContent())->lineItem->price->totalPrice;
  96.         $payload['price'] = $caltulatedPrice;
  97.         $payload['qty'] = $quantity;
  98.         $criteria = new Criteria();
  99.         $criteria->addAssociation('productoptions');
  100.         $criteria->addAssociations([
  101.             'productoptions.group',
  102.             'productoptions.option',
  103.         ]);
  104.         $criteria->getAssociation('productoptions')->addSorting(new FieldSorting('option.position'FieldSorting::ASCENDING));
  105.         $criteria->addFilter(new EqualsFilter('id'$productId));
  106.         $product $this->productRepository->search($criteria$context)->first();
  107.         $productOptions $product->getExtension('productoptions');
  108.         $projectId Uuid::randomHex();
  109.         #$lineItem = null;
  110.         $ppId null;
  111.         if ($request->get('lineItemId')){
  112.             $cart $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
  113.             $cartItems $cart->getLineItems();
  114.             $lineItemId $request->get('lineItemId');
  115.             $lineItem $cartItems->filter(function(LineItem $lineItem) use ($lineItemId){
  116.                 return $lineItem->getId() == $lineItemId;
  117.             })->first();
  118.             $projectId $lineItem->getId();
  119.             $criteria = new Criteria();
  120.             $criteria->addFilter(new EqualsFilter('projectId'$projectId));
  121.             $result $this->pitchPrintRepository->search($criteria$context)->first();
  122.             $ppId $result->getId();
  123.             if(isset($result->getPayload()['previews'])){
  124.                 $payload['previews'] = $result->getPayload()['previews'];
  125.             }
  126.             $payload['price'] = $lineItem->getPrice()->getTotalPrice();
  127.         }
  128.         $payload['type'] = 'product';
  129.         if($productOptions->count() > 0){
  130.             $swOptions $request->get('productOptions');
  131.             $payload['swoptions'] = $swOptions;
  132.             $payload['type'] = 'product-with-options';
  133.         }
  134.         $insert = [
  135.             'productId' => $productId,
  136.             'cookieId' => $cookieId,
  137.             'projectId' => $projectId,
  138.             'payload' => serialize($payload),
  139.         ];
  140.         if($ppId){
  141.             $insert['id'] = $ppId;
  142.         }
  143.         $customer $event->getSalesChannelContext()->getCustomer();
  144.         if (!$customer){
  145.             $insert['guestWishlist'] = 1;
  146.         }
  147.         if (isset($product->getCustomFields()['pp_design_id__'])) {
  148.             $insert['designId'] = $product->getCustomFields()['pp_design_id__'];
  149.         }
  150.         if ($customer !== null && $request->cookies->get('pp_id')) {
  151.             try {
  152.                 $this->addWishlistRoute->add($productId$event->getSalesChannelContext(), $customer);
  153.             }catch (DuplicateWishlistProductException $exception){
  154.             }
  155.             $customerWishlistRepository $this->customerWishlistRepository;
  156.             $criteria = new Criteria();
  157.             $criteria->addAssociation('products');
  158.             $criteria->addFilter(new EqualsFilter('customerId'$customer->getId()));
  159.             $wp $customerWishlistRepository->search($criteria$context);
  160.             $wishlistProducts $wp->first()->getProducts();
  161.             $wishlistProduct $wishlistProducts->filter(function (CustomerWishlistProductEntity $customerWishlistProductEntity) use ($productId) {
  162.                 return $customerWishlistProductEntity->getProductId() == $productId;
  163.             })->first();
  164.             $insert['wishlistProductId'] = $wishlistProduct->getId();
  165.         }
  166.         return $this->pitchPrintRepository->upsert([$insert], $context);
  167.     }
  168. }