custom/plugins/KmPitchPrint/src/Subscribers/CheckoutOrderPlacedSubscriber.php line 112

Open in your IDE?
  1. <?php
  2. namespace KmPitchPrint\Subscribers;
  3. use KmPitchPrint\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageOrderCriteriaEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  14. use Swp\ProductOptionsSix\Core\Checkout\ProductOptions\Cart\ProductOptionsCartDataCollector;
  15. use Shopware\Core\Checkout\Order\OrderEvents;
  16. use Shopware\Core\Checkout\Order\OrderEntity;
  17. use Shopware\Core\Framework\Util\HtmlSanitizer;
  18. use Doctrine\DBAL\Connection;
  19. class CheckoutOrderPlacedSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var HtmlSanitizer
  23.      */
  24.     private HtmlSanitizer $sanitizer;
  25.     /**
  26.      * @var EntityRepository
  27.      */
  28.     private EntityRepository $orderSkusRepo;
  29.     /**
  30.      * @var EntityRepository
  31.      */
  32.     private EntityRepository $orderRepo;
  33.     /**
  34.      * @var EntityRepository
  35.      */
  36.     private EntityRepository $humanReadableRepo;
  37.     /**
  38.      * @var Connection
  39.      */
  40.     private Connection $connection;
  41.     public function __construct(
  42.         EntityRepository $orderSkusRepo,
  43.         EntityRepository $orderRepo,
  44.         //EntityRepository $humanReadableRepo,
  45.         Connection       $connection,
  46.         HtmlSanitizer    $sanitizer
  47.     )
  48.     {
  49.         $this->connection $connection;
  50.         $this->orderSkusRepo $orderSkusRepo;
  51.         $this->orderRepo $orderRepo;
  52.         $this->sanitizer $sanitizer;
  53.         //$this->humanReadableRepo = $humanReadableRepo;
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             //CheckoutOrderPlacedEvent::class => 'addReadlbeItemIds',
  59.             CheckoutFinishPageOrderCriteriaEvent::class => 'constrainLineItems',
  60.             OrderEvents::ORDER_WRITTEN_EVENT => 'orderWritten'
  61.         ];
  62.     }
  63.     /**
  64.      * @param EntityWrittenEvent $event
  65.      * @return void
  66.      */
  67.     public function addReadlbeItemIds(CheckoutOrderPlacedEvent $event): void
  68.     {
  69.         $order $event->getOrder();
  70.         $i 1;
  71.         #$humanReadable = [];
  72.         foreach ($order->getLineItems() as $lineItem) {
  73.             if (in_array($lineItem->getType(), [
  74.                     LineItem::PRODUCT_LINE_ITEM_TYPE,
  75.                     ProductOptionsCartDataCollector::PRODUCT_OPTIONS_LINE_ITEM_TYPE
  76.                 ]) && $lineItem->getParentId() == null) {
  77.                 $humanReadable[] = [
  78.                     'lineItemId' => $lineItem->getId(),
  79.                     'orderNumber' => $order->getOrderNumber(),
  80.                     'readableId' => $i,
  81.                 ];
  82.                 $i++;
  83.             }
  84.         }
  85.         if (isset($humanReadable)) {
  86.             $this->humanReadableRepo->upsert($humanReadable$event->getContext());
  87.         }
  88.     }
  89.     /**
  90.      * @param EntityWrittenEvent $event
  91.      * @return void
  92.      */
  93.     public function orderWritten(EntityWrittenEvent $event): void
  94.     {
  95.         $context $event->getContext();
  96.         foreach ($event->getWriteResults() as $result) {
  97.             if ($result->getProperty('id')) {
  98.                 $order $this->orderRepo->search(
  99.                     (new Criteria())
  100.                         ->addFilter(new EqualsFilter('id'$result->getProperty('id')))
  101.                         ->addAssociation('lineItems'),
  102.                     $context
  103.                 )->first();
  104.                 if ($order) {
  105.                     $this->writeOrderSkus($order$context);
  106.                     //$this->deletePPProjects($order, $context); //don't delete the project, we need it for the "jpg-bridge"
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     private function deletePPProjects(OrderEntity $orderContext $context): void
  112.     {
  113.         $projects = [];
  114.         /*
  115.         $lineItems = $order->getLineItems()->filter(function (OrderLineItemEntity $lineItem) {
  116.             return $lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE;
  117.         });*/
  118.         /**
  119.          * @var OrderLineItemEntity $lineItem
  120.          */
  121.         foreach ($order->getLineItems() as $lineItem) {
  122.             $projects[] = $lineItem->getIdentifier();
  123.         }
  124.         $projects implode("','"$projects);
  125.         $sql = <<<SQL
  126.         DELETE FROM km_product_pitchprint WHERE project_id IN  ('$projects')
  127.         SQL;
  128.         $result $this->connection->executeStatement(
  129.             $sql
  130.         );
  131.     }
  132.     private function writeOrderSkus(OrderEntity $orderContext $context)
  133.     {
  134.         $skus = [];
  135.         $swOptions = [];
  136.         foreach ($order->getLineItems() as $lineItem) {
  137.             if (
  138.                 $lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE
  139.             ) {
  140.                 $previewUrl '#';
  141.                 if (isset($lineItem->getPayload('features')['pitch_print'])) {
  142.                     $previewUrl $lineItem->getPayloadValue('features')['pitch_print']['previews'][0];
  143.                 }
  144.                 if ($lineItem->getPayload()['productNumber'] != '*') {
  145.                     /*$skus[] = $this->sanitizer->sanitize(
  146.                         '<a href="' .$previewUrl.'" target="_blank">'.$lineItem->getPayload()['productNumber'].'</a>');*/
  147.                     #$skus[] = htmlspecialchars('<a href="' .$previewUrl.'" target="_blank">'.$lineItem->getPayload()['productNumber'].'</a>');
  148.                     $skus[] = '<a href="' $previewUrl '" target="_blank">' $lineItem->getPayload()['productNumber'] . '</a>';
  149.                 }
  150.             }
  151.             if ($lineItem->getType() == ProductOptionsCartDataCollector::PRODUCT_OPTIONS_OPTION_LINE_ITEM_TYPE) {
  152.                 $swOptions[] = $lineItem->getLabel();
  153.             }
  154.         }
  155.         if (!empty($skus)) {
  156.             $skus implode('; '$skus);
  157.             $swOptions implode('; 'array_unique($swOptions));
  158.             $this->orderSkusRepo->create([
  159.                 [
  160.                     'orderId' => $order->getId(),
  161.                     'skus' => $skus,
  162.                     'swOptions' => $swOptions,
  163.                 ]
  164.             ], $context);
  165.         }
  166.         //$this->deletePPProjects($order, $context);
  167.     }
  168.     /**
  169.      * @param CheckoutOrderPlacedEvent $event
  170.      * @return void
  171.      */
  172.     public function checkoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event): void
  173.     {
  174.         $order $event->getOrder();
  175.         $context $event->getContext();
  176.         $this->writeOrderSkus($order$context);
  177.     }
  178.     /**
  179.      * @param CheckoutFinishPageOrderCriteriaEvent $event
  180.      * @return void
  181.      */
  182.     public function constrainLineItems(CheckoutFinishPageOrderCriteriaEvent $event): void{
  183.         $event->getCriteria()->getAssociation('lineItems')->addFilter(
  184.             new MultiFilter(MultiFilter::CONNECTION_OR, [
  185.                 new EqualsFilter('type'LineItem::PRODUCT_LINE_ITEM_TYPE),
  186.                 new EqualsFilter('type'ProductOptionsCartDataCollector::PRODUCT_OPTIONS_LINE_ITEM_TYPE),
  187.                 //new EqualsFilter('type', LineItem::PROMOTION_LINE_ITEM_TYPE)
  188.             ])
  189.         );
  190.         $event->getCriteria()->getAssociation('lineItems')->addFilter(new EqualsFilter('parentId'null));
  191.  
  192.     }
  193. }