custom/plugins/NrbnSenertec/src/Subscriber/PaymentAndShippingAddressSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NrbnSenertec\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  7. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  8. use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class PaymentAndShippingAddressSubscriber implements EventSubscriberInterface{
  11.     private EntityRepository                $customerRepository;
  12.     private AccountService                  $accountService;
  13.     private AbstractContextSwitchRoute      $salesChannelContextSwitcher;
  14.     public function __construct(
  15.         EntityRepository                    $customerRepository,
  16.         AccountService                      $accountService,
  17.         AbstractContextSwitchRoute          $salesChannelContextSwitcher
  18.     ) {
  19.         $this->customerRepository          $customerRepository;
  20.         $this->accountService              $accountService;
  21.         $this->salesChannelContextSwitcher $salesChannelContextSwitcher;
  22.     }
  23.     public static function getSubscribedEvents(): array {
  24.         return [
  25.             CartConvertedEvent::class => 'ChangePaymentAndShippingToStandard',
  26.         ];
  27.     }
  28.     
  29.     public function ChangePaymentAndShippingToStandard(CartConvertedEvent $event): void
  30.     {
  31.         $data = new RequestDataBag();
  32.         $data->set("shippingMethodId""9744837b0bc04b6fbb2207351a959240");
  33.         $this->salesChannelContextSwitcher->switchContext($data$event->getSalesChannelContext());
  34.         $customer $event->getSalesChannelContext()->getCustomer();
  35.         $this->customerRepository->update([[
  36.             "id" => $customer->getId(),
  37.             "customFields" => [
  38.                 "activeShippingAddress"   => $customer->getDefaultShippingAddress(),
  39.                 "activeShippingAddressId" => $customer->getDefaultShippingAddressId(),
  40.             ]
  41.         ]], $event->getContext());
  42.     }
  43. }