custom/plugins/mmeesSlackNotifier/src/Subscriber/orderSubscriber.php line 84

Open in your IDE?
  1. <?php
  2. /**
  3.  * Subscriber to listen to order events
  4.  *
  5.  * @copyright Copyright © 2020 e-mmer. All rights reserved.
  6.  * @author    maurits@e-mmer.nl
  7.  */
  8. declare(strict_types=1);
  9. namespace Mmeester\SlackNotifier\Subscriber;
  10. use Mmeester\SlackNotifier\Entity\Order\OrderRepository;
  11. use Mmeester\SlackNotifier\Helper\CurrencyHelper;
  12. use Mmeester\SlackNotifier\Helper\SettingsHelper;
  13. use Mmeester\SlackNotifier\Helper\SlackHelper;
  14. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class orderSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var Client
  22.      */
  23.     private $client;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $orderRepository;
  28.     /**
  29.      * @var CurrencyHelper
  30.      */
  31.     private $currency;
  32.     /**
  33.      * @var Slack
  34.      */
  35.     private $slack;
  36.     /**
  37.      * @var SettingsHelper
  38.      */
  39.     private $settings;
  40.     /**
  41.      * orderSubscriber constructor.
  42.      *
  43.      * @param OrderRepository $orderRepository
  44.      * @param CurrencyHelper  $currency
  45.      * @param SlackHelper           $slack
  46.      */
  47.     public function __construct(
  48.         OrderRepository $orderRepository,
  49.         CurrencyHelper $currency,
  50.         SlackHelper $slack,
  51.         SettingsHelper $settings
  52.     )
  53.     {
  54.         $this->orderRepository $orderRepository;
  55.         $this->currency $currency;
  56.         $this->slack $slack;
  57.         $this->settings $settings;
  58.     }
  59.     /**
  60.      * @return array|string[]
  61.      */
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  66.         ];
  67.     }
  68.     /**
  69.      * @param CheckoutOrderPlacedEvent $event
  70.      */
  71.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  72.     {
  73.         $salesChannelId =  $event->getSalesChannelId();
  74.         if($this->settings->isOrderNotification("orderPlaced"$salesChannelId) === true) {
  75.             $order $event->getOrder();
  76.             if ($order) {
  77.                 $shipment null;
  78.                 $customer $order->getOrderCustomer();
  79.                 $shipments $order->getDeliveries()->getShippingMethods()->getElements();
  80.                 foreach($shipments as $ship) {
  81.                     $shipment $ship;
  82.                     break;
  83.                 }
  84.                 $slackItems "";
  85.                 $items $order->getLineItems();
  86.                 foreach($items as $item) {
  87.                     $slackItems .= $item->getQuantity(). "x ".$item->getLabel()."\n";
  88.                 }
  89.                 if($shipment !== null) {
  90.                     $slackMsg = [
  91.                         "blocks" => [
  92.                             [
  93.                                 "type" => "section",
  94.                                 "text" => [
  95.                                     "type" => "mrkdwn",
  96.                                     "text" => "*New order (" $order->getOrderNumber() . ") placed*\nCustomer *" $customer->getFirstName() . " " $customer->getLastName() . "* <" $customer->getEmail() . ">"
  97.                                 ]
  98.                             ],
  99.                             [
  100.                                 "type" => "section",
  101.                                 "fields" => [
  102.                                     [
  103.                                         "type" => "mrkdwn",
  104.                                         "text" => "*Order:*\n" $slackItems
  105.                                     ],
  106.                                 ]
  107.                             ],
  108.                             [
  109.                                 "type" => "section",
  110.                                 "fields" => [
  111.                                     [
  112.                                         "type" => "mrkdwn",
  113.                                         "text" => "*Total:*\n" $this->currency->formatForSlack($order->getAmountTotal(),
  114.                                                 'EUR')
  115.                                     ],
  116.                                     [
  117.                                         "type" => "mrkdwn",
  118.                                         "text" => "*Shipment:*\n" $shipment->getName()
  119.                                     ]
  120.                                 ]
  121.                             ],
  122.                             [
  123.                                 "type" => "actions",
  124.                                 "elements" => [
  125.                                     [
  126.                                         "type" => "button",
  127.                                         "style" => "primary",
  128.                                         "text" => [
  129.                                             "type" => "plain_text",
  130.                                             "text" => "View order"
  131.                                         ],
  132.                                         "url" => "https://" $_SERVER['SERVER_NAME'] . "/admin#/sw/order/detail/" $order->getId()
  133.                                     ]
  134.                                 ]
  135.                             ]
  136.                         ]
  137.                     ];
  138.                     $this->slack->sendMessage($slackMsg$salesChannelId);
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }