├── i18n
├── fr_FR.csv
├── it_IT.csv
├── nl_NL.csv
├── de_DE.csv
└── es_ES.csv
├── registration.php
├── etc
├── module.xml
├── config.xml
├── adminhtml
│ └── system.xml
└── frontend
│ └── di.xml
├── composer.json
├── view
└── frontend
│ ├── layout
│ └── hyva_checkout_index_index.xml
│ └── templates
│ └── form
│ └── customer_type.phtml
├── Model
├── Config.php
└── Form
│ ├── HideBusinessFieldsForConsumers.php
│ └── AddCustomerTypeRadioButtons.php
└── README.md
/i18n/fr_FR.csv:
--------------------------------------------------------------------------------
1 | "Business","Affaires"
2 | "Private","Privé"
3 |
--------------------------------------------------------------------------------
/i18n/it_IT.csv:
--------------------------------------------------------------------------------
1 | "Business","Affari"
2 | "Private","Privato"
3 |
--------------------------------------------------------------------------------
/i18n/nl_NL.csv:
--------------------------------------------------------------------------------
1 | "Business","Zakelijk"
2 | "Private","Privé"
3 |
--------------------------------------------------------------------------------
/i18n/de_DE.csv:
--------------------------------------------------------------------------------
1 | "Business","Geschäftlich"
2 | "Private","Privat"
3 |
--------------------------------------------------------------------------------
/i18n/es_ES.csv:
--------------------------------------------------------------------------------
1 | "Business","Negocios"
2 | "Private","Privado"
3 |
--------------------------------------------------------------------------------
/registration.php:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/etc/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 | 1
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vendic/hyva-checkout-hide-business-fields",
3 | "description": "Hyvä checkout module to hide business fields for consumers",
4 | "require": {
5 | "php": ">=8.1 <8.5",
6 | "hyva-themes/magento2-hyva-checkout": "^1.3.0"
7 | },
8 | "type": "magento2-module",
9 | "autoload": {
10 | "files": [
11 | "registration.php"
12 | ],
13 | "psr-4": {
14 | "Vendic\\HyvaCheckoutHideBusinessFields\\": ""
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/view/frontend/layout/hyva_checkout_index_index.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Model/Config.php:
--------------------------------------------------------------------------------
1 | scopeConfig->isSetFlag(
20 | 'customer_type/general/enable',
21 | ScopeInterface::SCOPE_STORE
22 | );
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/etc/adminhtml/system.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 | separator-top
10 |
11 | vendic
12 | Vendic_HyvaCheckoutHideBusinessFields::configuration
13 |
15 |
16 |
18 |
19 | Magento\Config\Model\Config\Source\Yesno
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/etc/frontend/di.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 | -
8 | Vendic\HyvaCheckoutHideBusinessFields\Model\Form\AddCustomerTypeRadioButtons
9 |
10 | -
11 | Vendic\HyvaCheckoutHideBusinessFields\Model\Form\HideBusinessFieldsForConsumers
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | -
20 | Vendic\HyvaCheckoutHideBusinessFields\Model\Form\AddCustomerTypeRadioButtons
21 |
22 | -
23 | Vendic\HyvaCheckoutHideBusinessFields\Model\Form\HideBusinessFieldsForConsumers
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | - company
34 | - vat_id
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Vendic_HyvaCheckoutHideBusinessFields
2 | This module adds a customer type field to the checkout and hides the business fields when the customer type is set to "consumer". Business fields can be configered via di.xml:
3 | ```xml
4 |
5 |
7 |
8 |
9 |
10 | - company
11 | - vat_id
12 |
13 |
14 |
15 |
16 | ```
17 |
18 | ### Installation
19 | ```
20 | composer require vendic/hyva-checkout-hide-business-fields
21 | ```
22 |
23 | ### Features
24 | Allows additional customer type options like Organisation to be added to the existing Consumer and Business options.
25 |
26 | To add custom customer type options, you can modify or add the following configuration to your module’s di.xml file:
27 | ```xml
28 |
29 |
30 |
31 |
32 |
33 |
34 | -
35 |
- Organization
36 | - organization
37 |
38 |
39 |
40 |
41 |
42 |
43 | ```
--------------------------------------------------------------------------------
/Model/Form/HideBusinessFieldsForConsumers.php:
--------------------------------------------------------------------------------
1 | config->isEnabled()) {
23 | return $form;
24 | }
25 |
26 | // Initial state
27 | $form->registerModificationListener(
28 | 'hideBusinessFieldsInitially',
29 | 'form:init',
30 | [$this, 'applyHideBusinessFieldsForConsumers']
31 | );
32 |
33 | // Is triggered when the customer type is changed
34 | $form->registerModificationListener(
35 | 'hideBusinessFieldsForConsumers',
36 | sprintf('form:%s:updated', AddCustomerTypeRadioButtons::FIELD_NAME),
37 | [$this, 'applyHideBusinessFieldsForConsumers']
38 | );
39 |
40 | return $form;
41 | }
42 |
43 | public function hideBusinessFieldsInitially(EntityFormInterface $form) : void
44 | {
45 | $this->hideFields($form);
46 | }
47 |
48 | public function applyHideBusinessFieldsForConsumers(EntityFormInterface $form) : void
49 | {
50 | $customerTypeField = $form->getField(AddCustomerTypeRadioButtons::FIELD_NAME);
51 | if ($customerTypeField->getValue() === AddCustomerTypeRadioButtons::TYPE_CONSUMER) {
52 | $this->hideFields($form);
53 | return;
54 | }
55 |
56 | $this->showFields($form);
57 | }
58 |
59 | private function hideFields(EntityFormInterface $form): void
60 | {
61 | foreach ($this->businessFields as $fieldName) {
62 | $form->getField($fieldName) ? $form->getField($fieldName)->hide() : '';
63 | }
64 | }
65 |
66 | private function showFields(EntityFormInterface $form): void
67 | {
68 | foreach ($this->businessFields as $fieldName) {
69 | $form->getField($fieldName) ? $form->getField($fieldName)->show() : '';
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/view/frontend/templates/form/customer_type.phtml:
--------------------------------------------------------------------------------
1 | getData('element');
25 | $showLabel = $block->getData('show_label') ?: false;
26 | $attributes = $element->getAttributes();
27 | $customerType = $magewire->getAddress() ? $magewire->getAddress()['customer_type'] : AddCustomerTypeRadioButtons::TYPE_CONSUMER;
28 | ?>
29 |
30 |
31 | = /* @noEscape */ $element->getRenderer()->renderLabel($element) ?>
32 |
33 | = /* @noEscape */ $element->getRenderer()->renderBefore($element) ?>
34 |
35 | hasRelatives()): ?>
36 |
37 |
38 |
39 |
40 | getOptions() as $option): ?>
41 |
42 |
53 |
54 |
55 |
56 |
57 | hasRelatives()): ?>
58 | getRelatives() as $relative): ?>
59 | = /* @noEscape */ $relative->render() ?>
60 |
61 |
62 |
63 | hasRelatives()): ?>
64 |
65 |
66 |
67 | = /* @noEscape */ $element->getRenderer()->renderAfter($element) ?>
68 |
69 |
--------------------------------------------------------------------------------
/Model/Form/AddCustomerTypeRadioButtons.php:
--------------------------------------------------------------------------------
1 | 'Private', 'value' => self::TYPE_CONSUMER],
22 | ['label' => 'Business', 'value' => self::TYPE_BUSINESS],
23 | ];
24 |
25 | public function __construct(
26 | private CheckoutSession $checkoutSession,
27 | private Config $config,
28 | private array $customCustomerTypeOptions = []
29 | ) {
30 | }
31 |
32 | public function apply(EntityFormInterface $form): EntityFormInterface
33 | {
34 | if (!$this->config->isEnabled()) {
35 | return $form;
36 | }
37 |
38 | $form->registerModificationListener(
39 | 'addSelect',
40 | 'form:init',
41 | [$this, 'addInitialSelectField']
42 | );
43 |
44 | $form->registerModificationListener(
45 | 'saveSelect',
46 | sprintf('form:%s:updated', self::FIELD_NAME),
47 | [$this, 'saveSelectField']
48 | );
49 |
50 | $form->registerModificationListener(
51 | 'addWireModel',
52 | 'form:build:magewire',
53 | [$this, 'addWireModel']
54 | );
55 |
56 | return $form;
57 | }
58 |
59 | public function addWireModel(EntityFormInterface $form) : void
60 | {
61 | /** @var Input $select */
62 | $select = $form->getField(self::FIELD_NAME);
63 | $select->setAttribute('wire:model', sprintf('address.%s', self::FIELD_NAME));
64 | $select->removeAttribute('wire:model.defer');
65 | $select->removeAttribute('wire:auto-save');
66 | }
67 |
68 | public function saveSelectField(EntityFormInterface $form): void
69 | {
70 | $select = $form->getField(self::FIELD_NAME);
71 |
72 | // Let's not use magic method here, as it can potentially return the value with `getValue`
73 | $previousValue = $select->getData('previous_value');
74 | if ($select->getValue() !== $previousValue) {
75 | $this->setCustomerTypeInSession($form, $select->getValue());
76 | }
77 | }
78 |
79 | public function addInitialSelectField(EntityFormInterface $form): void
80 | {
81 | $customerTypeOptions = array_merge($this->defaultCustomerTypeOptions, $this->customCustomerTypeOptions);
82 |
83 | /** @var Input $select */
84 | $select = $form->createField(AddCustomerTypeRadioButtons::FIELD_NAME, 'select', [
85 | 'data' => [
86 | 'input' => 'select',
87 | 'is_auto_save' => false,
88 | 'label' => __('Customer Type')->render(),
89 | 'value' => self::TYPE_CONSUMER,
90 | 'position' => 0,
91 | 'options' => $customerTypeOptions,
92 | ]
93 | ]);
94 |
95 | if ($this->getCustomerTypeFromSession($form) !== null) {
96 | $select->setValue($this->getCustomerTypeFromSession($form));
97 | // Set previous value back to null so Magewire can do it's thing and populate it with the actual previous value
98 | $select->setData(\Hyva\Checkout\Model\Form\EntityFieldInterface::PREVIOUS_VALUE, null);
99 | }
100 |
101 | $form->addField($select);
102 | }
103 |
104 | private function getCustomerTypeFromSession(EntityFormInterface $form) : ?string
105 | {
106 | return $this->checkoutSession->getData($form->getNamespace() . '_' . self::FIELD_NAME);
107 | }
108 |
109 | private function setCustomerTypeInSession(EntityFormInterface $form, string $value) : void
110 | {
111 | $this->checkoutSession->setData($form->getNamespace() . '_' . self::FIELD_NAME, $value);
112 | }
113 | }
114 |
--------------------------------------------------------------------------------