<?php
declare(strict_types=1);
namespace MadSplitAddress\Subscriber;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
AccountOverviewPageLoadedEvent::class => 'AccountOverviewPageLoaded',
AccountOrderPageLoadedEvent::class => 'onAccountOrderPageLoaded',
];
}
public function AccountOverviewPageLoaded(AccountOverviewPageLoadedEvent $event): void
{
$order = $event->getPage()->getNewestOrder();
if ($order){
$streethelpers = $order->getDeliveries()->getElements();
$myStreet = '';
$myNumber = '';
$check = 0;
$count = 0;
foreach($streethelpers as &$streethelper){
$splitStreets = explode(" ", $streethelper->getShippingOrderAddress()->getStreet());
foreach ($splitStreets as &$splitStreet){
if (is_numeric($splitStreet) || $check == 1 || preg_match('~[0-9]+~', $splitStreet)) {
if($check == 1){
$myNumber = $myNumber.' ';
}
$myNumber = $myNumber.$splitStreet;
$check = 1;
}else{
if($count == 1){
$myStreet = $myStreet.' ';
}
$myStreet = $myStreet.$splitStreet;
$count = 1;
}
}
}
$order->madStreet = $myStreet;
$order->madStreetNumber = $myNumber;
}
}
public function onAccountOrderPageLoaded(AccountOrderPageLoadedEvent $event): void
{
$orders = $event->getPage()->getOrders()->getElements();
if($orders){
foreach($orders as &$order){
$streethelpers = $order->getDeliveries()->getElements();
$myStreet = '';
$myNumber = '';
$check = 0;
$count = 0;
foreach($streethelpers as &$streethelper){
$splitStreets = explode(" ", $streethelper->getShippingOrderAddress()->getStreet());
foreach ($splitStreets as &$splitStreet){
if (is_numeric($splitStreet) || $check == 1 || preg_match('~[0-9]+~', $splitStreet)) {
if($check == 1){
$myNumber = $myNumber.' ';
}
$myNumber = $myNumber.$splitStreet;
$check = 1;
}else{
if($count == 1){
$myStreet = $myStreet.' ';
}
$myStreet = $myStreet.$splitStreet;
$count = 1;
}
}
}
$order->madStreet = $myStreet;
$order->madStreetNumber = $myNumber;
}
}
}
}