Skip to content

Commit

Permalink
Hide 'Create Account' on checkout if account exists for customer
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaahed committed Dec 7, 2023
1 parent a024dfd commit 0c44dd3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 22 deletions.
49 changes: 49 additions & 0 deletions Magewire/CreateAccount.php
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);
}
}
}
35 changes: 35 additions & 0 deletions Model/Form/Field/HideCreateAccountModifier.php
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;
}
}
14 changes: 14 additions & 0 deletions etc/frontend/di.xml
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>

6 changes: 5 additions & 1 deletion view/frontend/layout/hyva_checkout_components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
<referenceContainer name="checkout.section.quote-actions">
<container name="create-account.wrapper" htmlTag="div" htmlClass="mt-4">
<block name="create-account"
template="Vendic_HyvaCheckoutCreateAccount::create-account.phtml"
ifconfig="hyva_themes_checkout/new_customer/enable">
<arguments>
<argument name="magewire" xsi:type="object">
Vendic\HyvaCheckoutCreateAccount\Magewire\CreateAccount
</argument>
</arguments>
<block name="create-account.checkbox" as="checkbox">
<arguments>
<argument name="magewire" xsi:type="object">
Expand Down
21 changes: 0 additions & 21 deletions view/frontend/templates/create-account.phtml

This file was deleted.

26 changes: 26 additions & 0 deletions view/frontend/templates/magewire/create-account.phtml
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>

0 comments on commit 0c44dd3

Please sign in to comment.