src/Storefront/Controller/AddressController.php line 55

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Checkout\Cart\Order\Transformer\CustomerTransformer;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Exception\AddressNotFoundException;
  8. use Shopware\Core\Checkout\Customer\Exception\CannotDeleteDefaultAddressException;
  9. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeCustomerProfileRoute;
  10. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractDeleteAddressRoute;
  11. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractListAddressRoute;
  12. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractUpsertAddressRoute;
  13. use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Log\Package;
  17. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  18. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  19. use Shopware\Core\Framework\Uuid\Uuid;
  20. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  21. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  22. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  23. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  24. use Shopware\Storefront\Page\Address\AddressEditorModalStruct;
  25. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedHook;
  26. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoader;
  27. use Shopware\Storefront\Page\Address\Listing\AddressBookWidgetLoadedHook;
  28. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedHook;
  29. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoader;
  30. use Symfony\Component\HttpFoundation\RedirectResponse;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. /**
  35.  * @internal
  36.  */
  37. #[Route(defaults: ['_routeScope' => ['storefront']])]
  38. #[Package('storefront')]
  39. class AddressController extends StorefrontController
  40. {
  41.     private const ADDRESS_TYPE_BILLING 'billing';
  42.     private const ADDRESS_TYPE_SHIPPING 'shipping';
  43.     /**
  44.      * @internal
  45.      */
  46.     public function __construct(private readonly AddressListingPageLoader $addressListingPageLoader, private readonly AddressDetailPageLoader $addressDetailPageLoader, private readonly AccountService $accountService, private readonly AbstractListAddressRoute $listAddressRoute, private readonly AbstractUpsertAddressRoute $updateAddressRoute, private readonly AbstractDeleteAddressRoute $deleteAddressRoute, private readonly AbstractChangeCustomerProfileRoute $updateCustomerProfileRoute)
  47.     {
  48.     }
  49.     #[Route(path'/account/address'name'frontend.account.address.page'options: ['seo' => false], defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  50.     public function accountAddressOverview(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  51.     {
  52.         $page $this->addressListingPageLoader->load($request$context$customer);
  53.         $this->hook(new AddressListingPageLoadedHook($page$context));
  54.         return $this->renderStorefront('@Storefront/storefront/page/account/addressbook/index.html.twig', ['page' => $page]);
  55.     }
  56.     #[Route(path'/account/address/create'name'frontend.account.address.create.page'options: ['seo' => false], defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  57.     public function accountCreateAddress(Request $requestRequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  58.     {
  59.         $page $this->addressDetailPageLoader->load($request$context$customer);
  60.         $this->hook(new AddressDetailPageLoadedHook($page$context));
  61.         return $this->renderStorefront('@Storefront/storefront/page/account/addressbook/create.html.twig', [
  62.             'page' => $page,
  63.             'data' => $data,
  64.         ]);
  65.     }
  66.     #[Route(path'/account/address/{addressId}'name'frontend.account.address.edit.page'options: ['seo' => false], defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  67.     public function accountEditAddress(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  68.     {
  69.         $page $this->addressDetailPageLoader->load($request$context$customer);
  70.         $this->hook(new AddressDetailPageLoadedHook($page$context));
  71.         return $this->renderStorefront('@Storefront/storefront/page/account/addressbook/edit.html.twig', ['page' => $page]);
  72.     }
  73.     #[Route(path'/account/address/default-{type}/{addressId}'name'frontend.account.address.set-default-address'defaults: ['_loginRequired' => true], methods: ['POST'])]
  74.     public function switchDefaultAddress(string $typestring $addressIdSalesChannelContext $contextCustomerEntity $customer): RedirectResponse
  75.     {
  76.         if (!Uuid::isValid($addressId)) {
  77.             throw new InvalidUuidException($addressId);
  78.         }
  79.         $success true;
  80.         try {
  81.             if ($type === self::ADDRESS_TYPE_SHIPPING) {
  82.                 $this->accountService->setDefaultShippingAddress($addressId$context$customer);
  83.             } elseif ($type === self::ADDRESS_TYPE_BILLING) {
  84.                 $this->accountService->setDefaultBillingAddress($addressId$context$customer);
  85.             } else {
  86.                 $success false;
  87.             }
  88.         } catch (AddressNotFoundException) {
  89.             $success false;
  90.         }
  91.         return new RedirectResponse(
  92.             $this->generateUrl('frontend.account.address.page', ['changedDefaultAddress' => $success])
  93.         );
  94.     }
  95.     #[Route(path'/account/address/delete/{addressId}'name'frontend.account.address.delete'options: ['seo' => false], defaults: ['_loginRequired' => true], methods: ['POST'])]
  96.     public function deleteAddress(string $addressIdSalesChannelContext $contextCustomerEntity $customer): Response
  97.     {
  98.         $success true;
  99.         if (!$addressId) {
  100.             throw new MissingRequestParameterException('addressId');
  101.         }
  102.         try {
  103.             $this->deleteAddressRoute->delete($addressId$context$customer);
  104.         } catch (InvalidUuidException AddressNotFoundException CannotDeleteDefaultAddressException) {
  105.             $success false;
  106.         }
  107.         return new RedirectResponse($this->generateUrl('frontend.account.address.page', ['addressDeleted' => $success]));
  108.     }
  109.     #[Route(path'/account/address/create'name'frontend.account.address.create'options: ['seo' => false], defaults: ['_loginRequired' => true], methods: ['POST'])]
  110.     #[Route(path'/account/address/{addressId}'name'frontend.account.address.edit.save'options: ['seo' => false], defaults: ['_loginRequired' => true], methods: ['POST'])]
  111.     public function saveAddress(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  112.     {
  113.         /** @var RequestDataBag $address */
  114.         $address $data->get('address');
  115.         try {
  116.             $this->updateAddressRoute->upsert(
  117.                 $address->get('id'),
  118.                 $address->toRequestDataBag(),
  119.                 $context,
  120.                 $customer
  121.             );
  122.             return new RedirectResponse($this->generateUrl('frontend.account.address.page', ['addressSaved' => true]));
  123.         } catch (ConstraintViolationException $formViolations) {
  124.         }
  125.         if (!$address->get('id')) {
  126.             return $this->forwardToRoute('frontend.account.address.create.page', ['formViolations' => $formViolations]);
  127.         }
  128.         return $this->forwardToRoute(
  129.             'frontend.account.address.edit.page',
  130.             ['formViolations' => $formViolations],
  131.             ['addressId' => $address->get('id')]
  132.         );
  133.     }
  134.     #[Route(path'/widgets/account/address-book'name'frontend.account.addressbook'options: ['seo' => true], defaults: ['XmlHttpRequest' => true'_loginRequired' => true'_loginRequiredAllowGuest' => true], methods: ['POST'])]
  135.     public function addressBook(Request $requestRequestDataBag $dataBagSalesChannelContext $contextCustomerEntity $customer): Response
  136.     {
  137.         $viewData = new AddressEditorModalStruct();
  138.         $params = [];
  139.         try {
  140.             $this->handleChangeableAddresses($viewData$dataBag$context$customer);
  141.             $this->handleAddressCreation($viewData$dataBag$context$customer);
  142.             $this->handleAddressSelection($viewData$dataBag$context$customer);
  143.             $page $this->addressListingPageLoader->load($request$context$customer);
  144.             $this->hook(new AddressBookWidgetLoadedHook($page$context));
  145.             $viewData->setPage($page);
  146.             $this->handleCustomerVatIds($dataBag$context$customer);
  147.         } catch (ConstraintViolationException $formViolations) {
  148.             $params['formViolations'] = $formViolations;
  149.             $params['postedData'] = $dataBag->get('address');
  150.         } catch (\Exception) {
  151.             $viewData->setSuccess(false);
  152.             $viewData->setMessages([
  153.                 'type' => self::DANGER,
  154.                 'text' => $this->trans('error.message-default'),
  155.             ]);
  156.         }
  157.         if ($request->get('redirectTo') || $request->get('forwardTo')) {
  158.             return $this->createActionResponse($request);
  159.         }
  160.         $params array_merge($params$viewData->getVars());
  161.         $response $this->renderStorefront(
  162.             '@Storefront/storefront/component/address/address-editor-modal.html.twig',
  163.             $params
  164.         );
  165.         $response->headers->set('x-robots-tag''noindex');
  166.         return $response;
  167.     }
  168.     private function handleAddressCreation(
  169.         AddressEditorModalStruct $viewData,
  170.         RequestDataBag $dataBag,
  171.         SalesChannelContext $context,
  172.         CustomerEntity $customer
  173.     ): void {
  174.         /** @var DataBag|null $addressData */
  175.         $addressData $dataBag->get('address');
  176.         if ($addressData === null) {
  177.             return;
  178.         }
  179.         $response $this->updateAddressRoute->upsert(
  180.             $addressData->get('id'),
  181.             $addressData->toRequestDataBag(),
  182.             $context,
  183.             $customer
  184.         );
  185.         $addressId $response->getAddress()->getId();
  186.         $addressType null;
  187.         if ($viewData->isChangeBilling()) {
  188.             $addressType self::ADDRESS_TYPE_BILLING;
  189.         } elseif ($viewData->isChangeShipping()) {
  190.             $addressType self::ADDRESS_TYPE_SHIPPING;
  191.         }
  192.         // prepare data to set newly created address as customers default
  193.         if ($addressType) {
  194.             $dataBag->set('selectAddress', new RequestDataBag([
  195.                 'id' => $addressId,
  196.                 'type' => $addressType,
  197.             ]));
  198.         }
  199.         $viewData->setAddressId($addressId);
  200.         $viewData->setSuccess(true);
  201.         $viewData->setMessages(['type' => 'success''text' => $this->trans('account.addressSaved')]);
  202.     }
  203.     private function handleChangeableAddresses(
  204.         AddressEditorModalStruct $viewData,
  205.         RequestDataBag $dataBag,
  206.         SalesChannelContext $context,
  207.         CustomerEntity $customer
  208.     ): void {
  209.         $changeableAddresses $dataBag->get('changeableAddresses');
  210.         if ($changeableAddresses === null) {
  211.             return;
  212.         }
  213.         $viewData->setChangeShipping((bool) $changeableAddresses->get('changeShipping'));
  214.         $viewData->setChangeBilling((bool) $changeableAddresses->get('changeBilling'));
  215.         $addressId $dataBag->get('id');
  216.         if (!$addressId) {
  217.             return;
  218.         }
  219.         $viewData->setAddress($this->getById($addressId$context$customer));
  220.     }
  221.     /**
  222.      * @throws CustomerNotLoggedInException
  223.      * @throws InvalidUuidException
  224.      */
  225.     private function handleAddressSelection(
  226.         AddressEditorModalStruct $viewData,
  227.         RequestDataBag $dataBag,
  228.         SalesChannelContext $context,
  229.         CustomerEntity $customer
  230.     ): void {
  231.         $selectedAddress $dataBag->get('selectAddress');
  232.         if ($selectedAddress === null) {
  233.             return;
  234.         }
  235.         $addressType $selectedAddress->get('type');
  236.         $addressId $selectedAddress->get('id');
  237.         if (!Uuid::isValid($addressId)) {
  238.             throw new InvalidUuidException($addressId);
  239.         }
  240.         $success true;
  241.         try {
  242.             if ($addressType === self::ADDRESS_TYPE_SHIPPING) {
  243.                 $address $this->getById($addressId$context$customer);
  244.                 $customer->setDefaultShippingAddress($address);
  245.                 $this->accountService->setDefaultShippingAddress($addressId$context$customer);
  246.             } elseif ($addressType === self::ADDRESS_TYPE_BILLING) {
  247.                 $address $this->getById($addressId$context$customer);
  248.                 $customer->setDefaultBillingAddress($address);
  249.                 $this->accountService->setDefaultBillingAddress($addressId$context$customer);
  250.             } else {
  251.                 $success false;
  252.             }
  253.         } catch (AddressNotFoundException) {
  254.             $success false;
  255.         }
  256.         if ($success) {
  257.             $this->addFlash(self::SUCCESS$this->trans('account.addressDefaultChanged'));
  258.         } else {
  259.             $this->addFlash(self::DANGER$this->trans('account.addressDefaultNotChanged'));
  260.         }
  261.         $viewData->setSuccess($success);
  262.     }
  263.     private function getById(string $addressIdSalesChannelContext $contextCustomerEntity $customer): CustomerAddressEntity
  264.     {
  265.         if (!Uuid::isValid($addressId)) {
  266.             throw new InvalidUuidException($addressId);
  267.         }
  268.         $criteria = new Criteria();
  269.         $criteria->addFilter(new EqualsFilter('id'$addressId));
  270.         $criteria->addFilter(new EqualsFilter('customerId'$customer->getId()));
  271.         $address $this->listAddressRoute->load($criteria$context$customer)->getAddressCollection()->get($addressId);
  272.         if (!$address) {
  273.             throw new AddressNotFoundException($addressId);
  274.         }
  275.         return $address;
  276.     }
  277.     private function handleCustomerVatIds(RequestDataBag $dataBagSalesChannelContext $contextCustomerEntity $customer): void
  278.     {
  279.         if (!$dataBag->has('vatIds')) {
  280.             return;
  281.         }
  282.         $newVatIds $dataBag->get('vatIds')->all();
  283.         $oldVatIds $customer->getVatIds() ?? [];
  284.         if (!array_diff($newVatIds$oldVatIds) && !array_diff($oldVatIds$newVatIds)) {
  285.             return;
  286.         }
  287.         $dataCustomer CustomerTransformer::transform($customer);
  288.         $dataCustomer['vatIds'] = $newVatIds;
  289.         $dataCustomer['accountType'] = $customer->getCompany() === null CustomerEntity::ACCOUNT_TYPE_PRIVATE CustomerEntity::ACCOUNT_TYPE_BUSINESS;
  290.         $newDataBag = new RequestDataBag($dataCustomer);
  291.         $this->updateCustomerProfileRoute->change($newDataBag$context$customer);
  292.     }
  293. }