-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Vendic/feature/SWEHY-42
Hide 'Create Account' on checkout if account exists for customer
- Loading branch information
Showing
6 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) Vendic B.V https://vendic.nl/ | ||
*/ | ||
|
||
namespace Vendic\HyvaCheckoutCreateAccount\Magewire; | ||
|
||
use Magento\Checkout\Model\Session; | ||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magewirephp\Magewire\Component; | ||
|
||
class CreateAccount extends Component | ||
{ | ||
private const ACCOUNT_EXISTS = 'account_exists'; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $accountExists = false; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $listeners = ['email_address_updated' => '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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Vendic\HyvaCheckoutCreateAccount\Model\Form\Field; | ||
|
||
use Hyva\Checkout\Magewire\Checkout\AddressView\MagewireAddressFormInterface; | ||
use Hyva\Checkout\Model\Form\EntityFieldInterface; | ||
use Hyva\Checkout\Model\Form\EntityFormInterface; | ||
use Hyva\Checkout\Model\Form\EntityFormModifierInterface; | ||
use Magewirephp\Magewire\Component; | ||
|
||
/** | ||
* @copyright Copyright (c) Vendic B.V https://vendic.nl/ | ||
*/ | ||
class HideCreateAccountModifier implements EntityFormModifierInterface | ||
{ | ||
|
||
public function apply(EntityFormInterface $form): EntityFormInterface | ||
{ | ||
$form->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Hyva\Checkout\Model\Form\EntityForm\EavAttributeShippingAddressForm"> | ||
<arguments> | ||
<argument name="entityFormModifiers" xsi:type="array"> | ||
<item name="hide_create_account_modifier" xsi:type="object"> | ||
Vendic\HyvaCheckoutCreateAccount\Model\Form\Field\HideCreateAccountModifier | ||
</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** @var \Magento\Framework\Escaper $escaper */ | ||
/** @var \Magento\Framework\View\Element\Template $block */ | ||
/** @var \Hyva\Theme\Model\ViewModelRegistry $viewModels */ | ||
/** @var \Vendic\HyvaCheckoutCreateAccount\Magewire\CreateAccount $magewire */ | ||
/** @var \Vendic\HyvaCheckoutCreateAccount\ViewModel\GuestChecker $guestCheckerViewModel */ | ||
$guestCheckerViewModel = $viewModels->require(\Vendic\HyvaCheckoutCreateAccount\ViewModel\GuestChecker::class); | ||
|
||
if (!$guestCheckerViewModel->isCustomerGuest()) { | ||
return; | ||
} | ||
|
||
?> | ||
<div wire:model="accountExists"> | ||
<?php if (!$magewire->accountExists): ?> | ||
<div class="flex gap-x-4"> | ||
<div class="flex items-center"> | ||
<?= $block->getChildHtml('checkbox') ?> | ||
</div> | ||
<div class="flex-1 gap-y-2"> | ||
<label for="create-account"><?= $escaper->escapeHtml(__('Create Account')) ?></label> | ||
</div> | ||
</div> | ||
<?php endif; ?> | ||
</div> |