custom/plugins/OrderWatcher/src/Subscriber/OrderSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace OrderWatcher\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Content\Product\ProductEvents;
  12. class OrderSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $orderRepo;
  15.     public function __construct(
  16.         EntityRepositoryInterface $orderRepo
  17.     )
  18.     {
  19.         $this->orderRepo $orderRepo;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  24.         return [
  25.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  26.             OrderEvents::ORDER_LOADED_EVENT  => 'onOrderLoaded',
  27.         ];
  28.     }
  29.     public function onOrderLoaded(EntityLoadedEvent $event)
  30.     {
  31. //        foreach ( $event->getEntities() as $e)
  32. //        {
  33. //            $e->addExtension('string', []);
  34. //        }
  35.     }
  36.     public function onOrderWritten(EntityWrittenEvent $event)
  37.     {
  38.         $ids    $event->getIds();
  39.         $orders = [];
  40.         foreach ($ids as $id)
  41.         {
  42.             $criteria = new Criteria([$id]);
  43.             $criteria->addAssociation('deliveries.shippingMethod')
  44.                 ->addAssociation('deliveries.shippingOrderAddress.country')
  45.                 ->addAssociation('transactions.paymentMethod')
  46.                 ->addAssociation('lineItems')
  47.                 ->addAssociation('currency')
  48.                 ->addAssociation('products')
  49.                 ->addAssociation('addresses');
  50.             $orders[] = $this->orderRepo->search($criteriaContext::createDefaultContext())->getEntities();
  51.             self::processOrders($orders);
  52.         }
  53.     }
  54.     /**
  55.      * @param $orders
  56.      *
  57.      * @return void
  58.      */
  59.     public static function processOrders($orders)
  60.     {
  61.         $orders json_decode(json_encode($orders));
  62.         foreach ($orders[0] as $order)
  63.         {
  64.             $csvHeader "Artikelnummer;Menge;Kommentar;Name;Händlerpreis pro Artikel;Verkaufspreis pro Artikel;Verkaufspreis pro Artikel + MwSt.;Händlerpreis Gesamt;Verkaufspreis Gesamt;Verkaufspreis + MwSt. Gesamt;\n";
  65.             $items     $order->lineItems;
  66.             $csv       '';
  67.             $id       $order->orderNumber;
  68.             $lastName $order->orderCustomer->lastName;
  69.             $time     date('Ymd'strtotime($order->orderDateTime));
  70.             foreach ($items as $item)
  71.             {
  72.                 $tax + ((int) $item->price->calculatedTaxes[0]->taxRate 100);
  73.                 $productNumber $item->payload->productNumber ?? '___keine___';
  74.                 $quantity      $item->price->quantity;
  75.                 $comment       '';
  76.                 $name          str_replace(':'','$item->label);
  77.                 $sellerPrice   0;
  78.                 $grossPrice    $item->price->unitPrice;
  79.                 $netPrice      round($grossPrice $tax2);
  80.                 $sellerTotal   0;
  81.                 $grossTotal    round($item->price->totalPrice2);
  82.                 $netTotal      $grossTotal $tax;
  83.                 $csv .= $productNumber ';' $quantity ';' $comment ';' $name ';' self::formatNumber($sellerPrice) . ';' self::formatNumber($netPrice) . ';' self::formatNumber($grossPrice) . ';' self::formatNumber($sellerTotal) . ';' self::formatNumber($netTotal) . ';' self::formatNumber($grossTotal) . ";\n";
  84.             }
  85.             file_put_contents(getenv('PUBLICDIR').'intern/orders/' $id.'_'$time '_' $lastName '.csv'$csvHeader $csv);
  86.         }
  87.     }
  88.     public static function formatNumber($number): string
  89.     {
  90.         return str_replace('.'',', (string) sprintf('%.2f'$number)) . ' €';
  91.     }
  92. }