From 0c44dd3048c634561f92e7874b40988f797a3049 Mon Sep 17 00:00:00 2001 From: Zaahed Yaqubi Date: Thu, 7 Dec 2023 19:00:05 +0100 Subject: [PATCH] Hide 'Create Account' on checkout if account exists for customer --- Magewire/CreateAccount.php | 49 +++++++++++++++++++ .../Form/Field/HideCreateAccountModifier.php | 35 +++++++++++++ etc/frontend/di.xml | 14 ++++++ .../layout/hyva_checkout_components.xml | 6 ++- view/frontend/templates/create-account.phtml | 21 -------- .../templates/magewire/create-account.phtml | 26 ++++++++++ 6 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 Magewire/CreateAccount.php create mode 100644 Model/Form/Field/HideCreateAccountModifier.php create mode 100644 etc/frontend/di.xml delete mode 100644 view/frontend/templates/create-account.phtml create mode 100644 view/frontend/templates/magewire/create-account.phtml diff --git a/Magewire/CreateAccount.php b/Magewire/CreateAccount.php new file mode 100644 index 0000000..29e8686 --- /dev/null +++ b/Magewire/CreateAccount.php @@ -0,0 +1,49 @@ + 'hideIfAccountExists']; + + public function __construct( + private CustomerRepositoryInterface $customerRepository, + private Session $checkoutSession + ) { + } + + public function mount(): void + { + $this->accountExists = $this->checkoutSession->getData(self::ACCOUNT_EXISTS) ?? false; + } + + public function hideIfAccountExists(string $email): void + { + try { + $this->customerRepository->get($email); + $this->accountExists = true; + $this->checkoutSession->setData(self::ACCOUNT_EXISTS, true); + } catch (NoSuchEntityException) { + $this->accountExists = false; + $this->checkoutSession->setData(self::ACCOUNT_EXISTS, false); + } + } +} diff --git a/Model/Form/Field/HideCreateAccountModifier.php b/Model/Form/Field/HideCreateAccountModifier.php new file mode 100644 index 0000000..fce206d --- /dev/null +++ b/Model/Form/Field/HideCreateAccountModifier.php @@ -0,0 +1,35 @@ +registerModificationListener( + 'hide_create_account_if_already_exists', + 'form:shipping:email:updated', + function ( + EntityFormInterface $form, + EntityFieldInterface $field, + MagewireAddressFormInterface $addressComponent + ) { + /** @var Component $addressComponent */ + $addressComponent->emit('email_address_updated', $field->getValue()); + return $form; + } + ); + + return $form; + } +} diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml new file mode 100644 index 0000000..eae1b11 --- /dev/null +++ b/etc/frontend/di.xml @@ -0,0 +1,14 @@ + + + + + + + Vendic\HyvaCheckoutCreateAccount\Model\Form\Field\HideCreateAccountModifier + + + + + + diff --git a/view/frontend/layout/hyva_checkout_components.xml b/view/frontend/layout/hyva_checkout_components.xml index f00c8d9..76997e1 100644 --- a/view/frontend/layout/hyva_checkout_components.xml +++ b/view/frontend/layout/hyva_checkout_components.xml @@ -5,8 +5,12 @@ + + + Vendic\HyvaCheckoutCreateAccount\Magewire\CreateAccount + + diff --git a/view/frontend/templates/create-account.phtml b/view/frontend/templates/create-account.phtml deleted file mode 100644 index 90002ee..0000000 --- a/view/frontend/templates/create-account.phtml +++ /dev/null @@ -1,21 +0,0 @@ -require(\Vendic\HyvaCheckoutCreateAccount\ViewModel\GuestChecker::class); - -if (!$guestCheckerViewModel->isCustomerGuest()) { - return; -} -?> -
-
- getChildHtml('checkbox') ?> -
-
- -
-
diff --git a/view/frontend/templates/magewire/create-account.phtml b/view/frontend/templates/magewire/create-account.phtml new file mode 100644 index 0000000..6922e39 --- /dev/null +++ b/view/frontend/templates/magewire/create-account.phtml @@ -0,0 +1,26 @@ +require(\Vendic\HyvaCheckoutCreateAccount\ViewModel\GuestChecker::class); + +if (!$guestCheckerViewModel->isCustomerGuest()) { + return; +} + +?> +
+ accountExists): ?> +
+
+ getChildHtml('checkbox') ?> +
+
+ +
+
+ +