custom/plugins/OkeonlineKejeDeliveryAddress/src/Storefront/Subscriber/CartConvertedEventSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Okeonline\KejeDeliveryAddress\Storefront\Subscriber;
  4. use Okeonline\KejeDeliveryDatePicker\Core\Checkout\Cart\DeliveryDateCartProcessor;
  5. use Okeonline\KejeDeliveryDatePicker\OkeonlineKejeDeliveryDatePicker;
  6. use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
  7. use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
  8. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  9. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  10. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  11. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class CartConvertedEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var RequestStack
  21.      */
  22.     private $request;
  23.     /**
  24.      * @var EntityRepository
  25.      */
  26.     private $customerAddressRepository;
  27.     private $shipToCode null;
  28.     public function __construct(
  29.         RequestStack $request,
  30.         EntityRepository $customerAddressRepository
  31.     ) {
  32.         $this->request $request;
  33.         $this->customerAddressRepository $customerAddressRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CartConvertedEvent::class => 'setCustomFieldsFromForm'
  39.         ];
  40.     }
  41.     public function setCustomFieldsFromForm(CartConvertedEvent $event)
  42.     {
  43.         // Whatever happends, we always need to set:
  44.         $currentConvertedCart $event->getConvertedCart();
  45.         $currentConvertedCart['customFields']['customAddress'] = false;
  46.         $currentConvertedCart['customFields']['trackTraceEmailaddress'] = null;
  47.         $currentConvertedCart['customFields']['oo_order_delivery_instructions'] = null;
  48.         // conditionally we will set it to true
  49.         // First, capture and save track&Trace E-mailaddress
  50.         if ($this->request->getCurrentRequest()->request->has('trackTraceEmailaddress') && !is_null($this->request->getCurrentRequest()->request->get('trackTraceEmailaddress')))
  51.             $currentConvertedCart['customFields']['trackTraceEmailaddress'] = $this->request->getCurrentRequest()->request->get('trackTraceEmailaddress');
  52.         if ($this->request->getCurrentRequest()->request->has('deliveryInstructions') && !is_null($this->request->getCurrentRequest()->request->get('deliveryInstructions')))
  53.             $currentConvertedCart['customFields']['oo_order_delivery_instructions'] = $this->request->getCurrentRequest()->request->get('deliveryInstructions');
  54.         $event->setConvertedCart($currentConvertedCart);
  55.         // If no current delivery is set, we can not work further
  56.         if (
  57.             !$event->getCart()->getDeliveries()
  58.             || !$event->getCart()->getDeliveries()->first()
  59.             || !$event->getCart()->getDeliveries()->first() instanceof Delivery
  60.         )
  61.             return;
  62.         // get current delivery
  63.         $currentDelivery $event->getCart()->getDeliveries()->first();
  64.         if (
  65.             (
  66.                 !$this->request->getCurrentRequest()->request->has('selectedCustomAddress')
  67.             )
  68.             &&
  69.             (
  70.                 !$this->request->getCurrentRequest()->request->has('hasCustomAddress')
  71.                 || !$this->request->getCurrentRequest()->request->has('customAddress')
  72.                 || !is_array($this->request->getCurrentRequest()->request->get('customAddress'))
  73.             )
  74.         )
  75.             return;
  76.         // If selectedCustomAddress is set, use that to get the address from database and use data
  77.         // Elseif hasCustomAddress is set, continue with custom fiilled-in address
  78.         // Else, do nothing
  79.         if (
  80.             ($this->request->getCurrentRequest()->request->has('selectedCustomAddress') && !empty($this->request->getCurrentRequest()->request->get('selectedCustomAddress')))
  81.             &&
  82.             !$this->request->getCurrentRequest()->request->has('hasCustomAddress')
  83.         ) {
  84.             $customAddressData $this->request->getCurrentRequest()->request->get('selectedCustomAddress');
  85.             // get newly created entity
  86.             /** @var CustomerAddressEntity $newAddress */
  87.             $criteria = new Criteria([$customAddressData]);
  88.             $selectedAddress $this->customerAddressRepository->search($criteria$event->getContext())->first();
  89.             if (!$selectedAddress instanceof CustomerAddressEntity)
  90.                 return;
  91.             $convertedCartDeliveryShippingAddress = [
  92.                 'id' => $selectedAddress->getId(),
  93.                 'salutationId' => $selectedAddress->getSalutationId(),
  94.                 'firstName' => $selectedAddress->getFirstName(),
  95.                 'lastName' => $selectedAddress->getLastName(),
  96.                 'street' => $selectedAddress->getStreet(),
  97.                 'zipcode' => $selectedAddress->getZipcode(),
  98.                 'city' => $selectedAddress->getCity(),
  99.                 'company' => $selectedAddress->getCompany(),
  100.                 'phoneNumber' => $selectedAddress->getPhoneNumber(),
  101.                 'countryId' => $selectedAddress->getCountryId(),
  102.             ];
  103.             $this->shipToCode $selectedAddress->getCustomFields()['oo_customer_address_ship_to_code'] ?? null;
  104.             // re-get the cart
  105.             $currentConvertedCart $event->getConvertedCart();
  106.             // first: get current default address as billing address and add it to 'addresses'
  107.             $currentConvertedCart['addresses'][0] = $currentConvertedCart['deliveries'][0]['shippingOrderAddress'];
  108.             // second: get the generated billingAddressId, and replace it to above setted address
  109.             $currentConvertedCart['addresses'][0]['id'] = $currentConvertedCart['billingAddressId'];
  110.             // than, add custom address to deliveries
  111.             $currentConvertedCart['deliveries'][0]['shippingOrderAddress'] = $convertedCartDeliveryShippingAddress;
  112.             // and also add it to the addresses...
  113.             $currentConvertedCart['addresses'][1] = $convertedCartDeliveryShippingAddress;
  114.             $currentConvertedCart['customFields']['customAddress'] = $this->request->getCurrentRequest()->request->get('hasCustomAddress') === 'on';
  115.         } elseif ($this->request->getCurrentRequest()->request->has('hasCustomAddress')) {
  116.             $customAddressData $this->request->getCurrentRequest()->request->get('customAddress');
  117.             $street = ($customAddressData['street'] ?? '') . ' ' . ($customAddressData['housenumber'] ?? '') . ' ' . ($customAddressData['housenumber-addition'] ?? '');
  118.             $street trim($street);
  119.             // get address information from request stack
  120.             $customerId $this->getCurrentCustomerIdFromDelivery($currentDelivery);
  121.             $countryId $customAddressData['countryId'] ?? $this->getCurrentCountryIdFromDelivery($currentDelivery);
  122.             $salutationId $this->getCurrentSalutationIdFromDelivery($currentDelivery);
  123.             $firstName $customAddressData['firstName'] ?? '';
  124.             $lastName $customAddressData['lastName'] ?? '';
  125.             $zipcode $customAddressData['zipcode'] ?? '';
  126.             if (empty($zipcode))
  127.                 $zipcode '-';
  128.             $city $customAddressData['city'] ?? '';
  129.             $company $customAddressData['company'] ?? null;
  130.             $phoneNumber $customAddressData['phoneNumber'] ?? null;
  131.             $street $street ?? '';
  132.             // add address to customer
  133.             $customerAddressId Uuid::randomHex();
  134.             $convertedCartDeliveryShippingAddress = [
  135.                 'id' => $customerAddressId,
  136.                 'salutationId' => $salutationId,
  137.                 'firstName' => $firstName,
  138.                 'lastName' => $lastName,
  139.                 'street' => $street,
  140.                 'zipcode' => $zipcode,
  141.                 'city' => $city,
  142.                 'company' => $company ?? null,  // not required
  143.                 'phoneNumber' => $phoneNumber ?? null,  // not required
  144.                 'countryId' => $countryId,
  145.             ];
  146.             $this->shipToCode null;
  147.             // re-get the cart
  148.             $currentConvertedCart $event->getConvertedCart();
  149.             // first: get current default address as billing address and add it to 'addresses'
  150.             $currentConvertedCart['addresses'][0] = $currentConvertedCart['deliveries'][0]['shippingOrderAddress'];
  151.             // second: get the generated billingAddressId, and replace it to above setted address
  152.             $currentConvertedCart['addresses'][0]['id'] = $currentConvertedCart['billingAddressId'];
  153.             // than, add custom address to deliveries
  154.             $currentConvertedCart['deliveries'][0]['shippingOrderAddress'] = $convertedCartDeliveryShippingAddress;
  155.             // and also add it to the addresses...
  156.             $currentConvertedCart['addresses'][1] = $convertedCartDeliveryShippingAddress;
  157.             $currentConvertedCart['customFields']['customAddress'] = $this->request->getCurrentRequest()->request->get('hasCustomAddress') === 'on';
  158.         } else {
  159.             $criteria = new Criteria([$event->getCart()->getDeliveries()->getAddresses()->first()->getId()]);
  160.             $selectedAddress $this->customerAddressRepository->search($criteria$event->getContext())->first();
  161.             if ($selectedAddress instanceof CustomerAddressEntity)
  162.                 $this->shipToCode $selectedAddress->getCustomFields()['oo_customer_address_ship_to_code'] ?? null;
  163.             $currentConvertedCart['customFields']['ship_to_code'] = $this->shipToCode;
  164.         }
  165.         $currentConvertedCart['customFields']['ship_to_code'] = $this->shipToCode;
  166.         // the complete $currentConvertedCart will be added to $orderRepository->insert() in further process.
  167.         $event->setConvertedCart($currentConvertedCart);
  168.     }
  169.     private function getCurrentCustomerIdFromDelivery(Delivery $delivery)
  170.     {
  171.         return $delivery->getLocation()->getAddress()->getCustomerId();
  172.     }
  173.     private function getCurrentSalutationIdFromDelivery(Delivery $delivery)
  174.     {
  175.         return $delivery->getLocation()->getAddress()->getSalutationId();
  176.     }
  177.     private function getCurrentCountryIdFromDelivery(Delivery $delivery)
  178.     {
  179.         return $delivery->getLocation()->getAddress()->getCountryId();
  180.     }
  181. }