custom/plugins/KmPitchPrint/src/Subscribers/PitchPrintSubscriber.php line 131

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace KmPitchPrint\Subscribers;
  3. #use App\Product;
  4. use KmPitchPrint\Services\ProductPitchPrintService;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. #use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. #use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  8. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  17. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  18. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  19. use Shopware\Core\Checkout\Customer\Event\CustomerWishlistLoaderCriteriaEvent;
  20. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use KmPitchPrint\Events\AfterWishlistDeleteEvent;
  23. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  24. use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoadedEvent;
  25. use Symfony\Contracts\EventDispatcher\Event;
  26. class PitchPrintSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var SalesChannelRepositoryInterface
  30.      */
  31.     private $productRepository;
  32.     private $orderLineItemRepository;
  33.     /**
  34.      * @var CartService
  35.      */
  36.     private $cartService;
  37.     /**
  38.      * @var RequestStack
  39.      */
  40.     private $requestStrack;
  41.     /**
  42.      * @var ProductPitchPrintService
  43.      */
  44.     private $productPitchPrintService;
  45.     /**
  46.      * @var EntityRepositoryInterface
  47.      */
  48.     private $pitchPrintProductRepository;
  49.     //public function __construct(SalesChannelRepositoryInterface $productRepository, CartService $cartService)
  50.     public function __construct(
  51.         SalesChannelRepositoryInterface $productRepository,
  52.         EntityRepositoryInterface $orderLineItemRepository,
  53.         EntityRepositoryInterface $pitchPrintProductRepository,
  54.         CartService $cartService,
  55.         ProductPitchPrintService $productPitchPrintService,
  56.         RequestStack $requestStack
  57.     )
  58.     {
  59.         $this->orderLineItemRepository $orderLineItemRepository;
  60.         $this->pitchPrintProductRepository $pitchPrintProductRepository;
  61.         $this->productRepository $productRepository;
  62.         $this->cartService $cartService;
  63.         $this->productPitchPrintService $productPitchPrintService;
  64.         $this->requestStrack $requestStack;
  65.     }
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             CartConvertedEvent::class =>'afterCartConverted',
  70.             #BeforeLineItemAddedEvent::class => 'onBeforeLineItemAdded',
  71.             #GuestWishlistPageletLoadedEvent::class => 'guestWishlistPageletLoaded',
  72.             CheckoutOrderPlacedEvent::class => 'checkoutOrderPlacedEvent',
  73.             CustomerWishlistLoaderCriteriaEvent::class =>'customerWishlistLoaderCriteriaEvent',
  74.             AfterWishlistDeleteEvent::class => 'deletePitchPrintProject',
  75.             WishListPageProductCriteriaEvent::class => 'wishListPageProductCriteriaEvent',
  76.             GuestWishlistPageLoadedEvent::class=> 'wishListPageProductCriteriaEvent',
  77.         ];
  78.     }
  79.     /**
  80.      * @param WishListPageProductCriteriaEvent $event
  81.      * @return void
  82.      */
  83.     public function wishListPageProductCriteriaEventWishListPageProductCriteriaEvent $event){
  84.         $event->getCriteria()->addAssociation('media');
  85.         $event->getCriteria()->getAssociation('media')->addSorting(new FieldSorting('position'));
  86.     }
  87.     /**
  88.      * @param $criteria CustomerWishlistLoaderCriteriaEvent
  89.      * @return void
  90.      */
  91.     public function customerWishlistLoaderCriteriaEvent(CustomerWishlistLoaderCriteriaEvent $criteria){
  92.         $criteria->getCriteria()->addAssociation('productoptions');
  93.         $criteria->getCriteria()->addAssociation('pitchPrintProduct');
  94.         $criteria->getCriteria()->addFilter(new EqualsFilter('pitchPrintProduct.cookieId',
  95.             $this->requestStrack->getCurrentRequest()->cookies->get('pp_id')));
  96.         $criteria->getCriteria()->addAssociations([
  97.             'productoptions.group',
  98.             'productoptions.option',
  99.             'productoptions.group.productoptionsgroup',
  100.             'productoptions.group.productoptionsgroup.parent',
  101.             'productoptions.option.productoptionsoption',
  102.             //'productoptions.option.productoptionsoption.media'
  103.         ]);
  104.     }
  105.     public function checkoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event):void{
  106.         /*
  107.         $order = $event->getOrder();
  108.         $i = 1;
  109.         foreach ($order->getLineItems() as $lineItem){
  110.             if ($lineItem->getType() == 'product' || $lineItem->getType() == 'product-with-options'){
  111.                 $this->orderLineItemRepository->upsert([
  112.                     [
  113.                         'id'=>$lineItem->getId(),
  114.                         'humanReadableId'=>[
  115.                             'lineItemId' => $lineItem->getId(),
  116.                             'readableId' => $i,
  117.                             'orderNumber' => $order->getOrderNumber()
  118.                         ]
  119.                     ]
  120.                 ],$event->getContext());
  121.             }
  122.             $i++;
  123.         }*/
  124.         $this->requestStrack->getCurrentRequest()->cookies->remove('pp_id');
  125.     }
  126.     public function guestWishlistPageletLoaded(GuestWishlistPageletLoadedEvent $event): void{
  127.         $products $event->getPagelet()->getSearchResult()->getProducts();
  128.         if($event->getRequest()->cookies->get('pp_id')){
  129.             $cart $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
  130.             $lineItems $cart->getLineItems();
  131.             $productMatches $products->filter(function(SalesChannelProductEntity $product) use ($lineItems){
  132.                 foreach($lineItems as $lineItem ){
  133.                     return $lineItem->getReferencedId() == $product->getId();
  134.                 }
  135.             });
  136.             foreach($productMatches as $productMatch){
  137.                 dump($productMatch->getCover());
  138.             }
  139.             #dump($cart->getLineItems()->getReferenceIds());exit;
  140.         }
  141.     }
  142.     public function onBeforeLineItemAdded(BeforeLineItemAddedEvent $event):void
  143.     {
  144.         return;
  145.         $lineItem $event->getLineItem();
  146.         $previews $lineItem->getPayload()['features']['pitch_print']['previews'];
  147.         $criteria = New Criteria([$event->getLineItem()->getId()]);
  148.         /**
  149.          * @var SalesChannelProductEntity
  150.          */
  151.         $product $this->productRepository->search($criteria$event->getSalesChannelContext())->first();
  152.         #dump($product->getCover()->getMedia());exit;
  153.         $i=0;
  154.         foreach ($product->getCover()->getMedia() as $mediaEntity) {
  155.             //replace only previews, if the mediaItems are more, skip the rest
  156.             if (isset($previews[$i])) {
  157.                 $samplePreview $previews[$i];
  158.             } else {
  159.                 break;
  160.             }
  161.             foreach ($mediaEntity->getMedia()->getThumbnails() as $thumbnail) {
  162.                 $thumbnail->setUrl($samplePreview);
  163.             }
  164.             $mediaEntity->getMedia()->setUrl($samplePreview);
  165.             $i++;
  166.         }
  167.         $lineItem->setCover($product->getCover()->getMedia());
  168.     }
  169.     public function deletePitchPrintProject(AfterWishlistDeleteEvent $event): void{
  170.         $ppProjectId $event->getPpProject()->getProjectId();
  171.         $swProjectId $event->getPpProject()->getId();
  172.     }
  173.     /**
  174.      * adds productId to product-type: 'product-with-options' (by default it's null, product_id is required for KM_PP (python))
  175.      * @param CartConvertedEvent $event
  176.      * @return void
  177.      */
  178.     public function afterCartConverted(CartConvertedEvent $event){
  179.         $data $event->getConvertedCart();
  180.         $lineItems = [];
  181.         foreach($data['lineItems'] as $lineItem){
  182.             if ($lineItem['type']=='product-with-options'){
  183.                 foreach($data['lineItems'] as $pLineItem){
  184.                     if(isset($pLineItem['parentId'])){
  185.                         if($pLineItem['type'] == 'product' && $pLineItem['parentId'] == $lineItem['id'] ){
  186.                             $lineItem['productId'] = $pLineItem['productId'];
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.             $lineItems[] = $lineItem;
  192.         }
  193.         $data['lineItems'] = $lineItems;
  194.         $event->setConvertedCart($data);
  195.     }
  196.     /**
  197.      * @param ProductPageLoadedEvent $event
  198.      * @return void
  199.      */
  200.     public function productPageLoaded(ProductPageLoadedEvent $event){
  201.         $product $event->getPage()->getProduct();
  202.         foreach($product->getPrice() as $price){
  203.             /**
  204.              * @var \Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price
  205.              */
  206.             $price->setGross($price->getGross()*$product->getMinPurchase());
  207.             $price->setNet($price->getNet()*$product->getMinPurchase());
  208.         }
  209.         #dump($product->getPrice());exit;
  210.     }
  211. }