Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/pages/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,6 @@
- [Create a boolean field attribute](/tutorials/admin/custom-boolean-field-attribute.md)
- [Create a dropdown field attribute](/tutorials/admin/custom-dropdown-field-attribute.md)
- [Create a multiselect field attribute](/tutorials/admin/custom-multiselect-field-attribute.md)

- [Frontend development](/tutorials/frontend/index.md)
- [Customize checkout](/tutorials/frontend/custom-checkout/index.md)
- [Add a new checkout step](/tutorials/frontend/custom-checkout/add-new-step.md)
Expand Down
22 changes: 13 additions & 9 deletions src/pages/tutorials/admin/custom-boolean-field-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ keywords:

# Add a custom boolean attribute

This tutorial describes how a developer can create a custom boolean (Yes/No) attribute for the Customer entity using code. This will reflect in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.
This tutorial describes how you create a custom boolean (Yes/No) attribute for the Customer entity using code. The attribute appears in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.

This Customer attribute will be used to store a simple Yes/No flag on a customer record, as an example. It will be created as an EAV attribute in a data patch. The EAV model allows a developer to add custom functionality to the entities without modifying the core databases and schemas. Data patches are run just once, so this code will create the custom attribute and will never run again, which could cause issues. This tutorial also implements `PatchRevertableInterface`, which allows the attribute to be cleanly removed by running `bin/magento setup:rollback`.
Use this customer attribute as an example to store a simple Yes/No flag on a customer record. You create it as an EAV attribute in a data patch. The EAV model lets you extend entities without changing core database schemas. Data patches run once, so this code creates the attribute on the first run only. This tutorial also implements `PatchRevertableInterface`, so you can remove the attribute by running `bin/magento setup:rollback`.

## Code
## Data patch implementation

The following sections show the PHP you add to your module.

### Create the data patch class

Create a data patch class called `AddCustomerAttributeBoolean` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. This makes the application execute the data patch automatically when `bin/magento setup:upgrade` is run. This class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`. Adding the revertable interface requires implementing a `revert()` method that removes the attribute when the patch is rolled back.
Create a data patch class called `AddCustomerAttributeBoolean` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. Magento runs this data patch automatically when you run `bin/magento setup:upgrade`. This class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`. Adding the revertable interface requires implementing a `revert()` method that removes the attribute when the patch is rolled back.

```php
<?php
Expand Down Expand Up @@ -89,7 +91,9 @@ public function __construct(

There are five steps in developing a data patch. All the steps below are written inside the `apply` method.

1. Starting and ending the setup execution. This turns off foreign key checks and sets the SQL mode.
1. Start and end the setup execution.

This turns off foreign key checks and sets the SQL mode.

```php
$this->moduleDataSetup->getConnection()->startSetup();
Expand Down Expand Up @@ -149,7 +153,7 @@ There are five steps in developing a data patch. All the steps below are written
| `is_filterable_in_grid` | `true` - Filterable in the customer grid |
| `is_searchable_in_grid` | `false` - Not searchable in the customer grid (boolean fields are filtered, not searched) |

1. Add attribute to an attribute set and group.
1. Add the attribute to an attribute set and group.

There is only one attribute set and group for the customer entity. The default attribute set ID is a constant defined in the `CustomerMetadataInterface` interface and setting the attribute group ID to null makes the application use the default attribute group ID for the customer entity.

Expand Down Expand Up @@ -182,7 +186,7 @@ There are five steps in developing a data patch. All the steps below are written

Unlike the text field attribute, which is visible only in the Admin, the boolean attribute is also registered for the storefront registration and account edit forms. Adjust the `used_in_forms` values to match the visibility requirements of your project.

1. Gracefully handle exceptions.
1. Handle exceptions in a try/catch block.

```php
try {
Expand Down Expand Up @@ -217,7 +221,7 @@ public function revert(): void
}
```

### Implement rest of the interface
### Implement the rest of the interface

This data patch does not have any other patch as a dependency, and this data patch was not renamed earlier, so both `getDependencies` and `getAliases` can return an empty array.

Expand All @@ -241,7 +245,7 @@ Run `bin/magento setup:upgrade` from the project root to execute the newly added

![Custom attribute in the customer form](../../images/tutorials/custom-attribute-customer-form.png)

- The attribute is displayed in the customer grid and can be filtered using a Yes/No dropdown.
- The attribute is displayed in the customer grid and can be filtered using a Yes/No dropdown menu.

![Custom attribute in the customer grid](../../images/tutorials/custom-attribute-customer-grid.png)

Expand Down
26 changes: 15 additions & 11 deletions src/pages/tutorials/admin/custom-dropdown-field-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ keywords:

# Add a custom dropdown attribute

This tutorial describes how a developer can create a custom dropdown (select) attribute for the Customer entity using code. This will reflect in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.
This tutorial describes how you create a custom dropdown (select) attribute for the Customer entity using code. The attribute appears in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.

Use a select attribute when you want administrators (and optionally customers) to choose a value from a controlled set of options — for example, a customer tier (Silver/Gold/Platinum) or an internal segmentation flag. The attribute will be created as an EAV attribute in a data patch. This tutorial also implements `PatchRevertableInterface`, which allows the attribute to be cleanly removed by running `bin/magento setup:rollback`.
Use a select attribute when you want administrators (and optionally customers) to choose a value from a controlled set of options. Examples include a customer tier (Silver, Gold, Platinum) or an internal segmentation flag. You create the attribute as an EAV attribute in a data patch. This tutorial also implements `PatchRevertableInterface`, so you can remove the attribute by running `bin/magento setup:rollback`.

## Code
## Data patch implementation

The following sections show the PHP you add to your module.

### Create the data patch class

Create a data patch class called `AddCustomerAttributeOptions` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. This makes the application execute the data patch automatically when `bin/magento setup:upgrade` is run. Unlike the text field and boolean attribute tutorials, this class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`. Adding the revertable interface requires implementing a `revert()` method that removes the attribute when the patch is rolled back.
Create a data patch class called `AddCustomerAttributeOptions` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. Magento runs this data patch automatically when you run `bin/magento setup:upgrade`. Unlike the text field and boolean attribute tutorials, this class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`. Adding the revertable interface requires implementing a `revert()` method that removes the attribute when the patch is rolled back.

```php
<?php
Expand Down Expand Up @@ -61,7 +63,7 @@ The dependencies to the data patch are injected using constructor DI and are lis
- `\Magento\Customer\Model\ResourceModel\Attribute` aliased as `AttributeResource` for saving the attribute after adding custom data to it.
- `\Psr\Log\LoggerInterface` for logging exceptions thrown during the execution.

Note that unlike previous tutorials, the dropdown patch stores `$customerSetupFactory` directly rather than instantiating `$customerSetup` in the constructor. This is because `revert()` also needs to create a `CustomerSetup` instance, so the factory is reused across both methods.
Unlike the earlier tutorials, the dropdown patch stores `$customerSetupFactory` directly rather than instantiating `$customerSetup` in the constructor. The `revert()` method also needs a `CustomerSetup` instance, so the factory is reused across both methods.

```php
/**
Expand Down Expand Up @@ -89,7 +91,9 @@ public function __construct(

There are five steps in developing a data patch. All the steps below are written inside the `apply` method.

1. Starting and ending the setup execution. This turns off foreign key checks and sets the SQL mode.
1. Start and end the setup execution.

This turns off foreign key checks and sets the SQL mode.

```php
$this->moduleDataSetup->getConnection()->startSetup();
Expand Down Expand Up @@ -134,7 +138,7 @@ There are five steps in developing a data patch. All the steps below are written
| --- | --- |
| `label` | `Customer Custom Attribute Options` - Label for displaying the attribute value |
| `type` | `int` - Stores the selected option ID as an integer |
| `input` | `select` - Renders as a dropdown in the customer form |
| `input` | `select` - Renders as a dropdown menu in the customer form |
| `source` | Provides the list of selectable options |
| `required` | `false` - Attribute will be an optional field in the customer form |
| `position` | `444` - Sort order in the customer form |
Expand All @@ -145,7 +149,7 @@ There are five steps in developing a data patch. All the steps below are written
| `is_filterable_in_grid` | `true` - Filterable in the customer grid |
| `is_searchable_in_grid` | `false` - Not searchable in the customer grid (dropdown fields are filtered by option, not free-text searched) |

1. Add attribute to an attribute set and group.
1. Add the attribute to an attribute set and group.

There is only one attribute set and group for the customer entity. The default attribute set ID is a constant defined in the `CustomerMetadataInterface` interface and setting the attribute group ID to null makes the application use the default attribute group ID for the customer entity.

Expand Down Expand Up @@ -176,7 +180,7 @@ There are five steps in developing a data patch. All the steps below are written
$this->attributeResource->save($attribute);
```

1. Gracefully handle exceptions.
1. Handle exceptions in a try/catch block.

```php
try {
Expand Down Expand Up @@ -211,7 +215,7 @@ public function revert(): void
}
```

### Implement rest of the interface
### Implement the rest of the interface

This data patch does not have any other patch as a dependency, and this data patch was not renamed earlier, so both `getDependencies` and `getAliases` can return an empty array.

Expand All @@ -235,7 +239,7 @@ Run `bin/magento setup:upgrade` from the project root to execute the newly added

![Custom attribute in the customer form](../../images/tutorials/custom-attribute-customer-form.png)

- The attribute is displayed in the customer grid and can be filtered using a dropdown of available options.
- The attribute is displayed in the customer grid and can be filtered using a dropdown menu of available options.

![Custom attribute in the customer grid](../../images/tutorials/custom-attribute-customer-grid.png)

Expand Down
20 changes: 12 additions & 8 deletions src/pages/tutorials/admin/custom-multiselect-field-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ keywords:

# Add a custom multiselect attribute

This tutorial describes how a developer can create a custom multiselect attribute for the Customer entity using code. This will reflect in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.
This tutorial describes how you create a custom multiselect attribute for the Customer entity using code. The attribute appears in both the [Customer Grid](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/manage-account) and the [Customer Form](https://experienceleague.adobe.com/en/docs/commerce-admin/customers/customer-accounts/manage/update-account) in the Admin.

Use a multiselect attribute when you need to store multiple simultaneous values for a single customer field — for example, eligible shipping methods, allowed sales channels, or subscription preferences. Unlike the [dropdown attribute](custom-dropdown-field-attribute.md), which stores a single selected option ID as an integer, a multiselect attribute stores a comma-separated list of option IDs as a `varchar` value, handled by the `ArrayBackend` backend model. This tutorial also implements `PatchRevertableInterface`, which allows the attribute to be cleanly removed by running `bin/magento setup:rollback`.
Use a multiselect attribute when you need to store multiple simultaneous values for a single customer field. Examples include eligible shipping methods, allowed sales channels, or subscription preferences. Unlike the [dropdown attribute](custom-dropdown-field-attribute.md), a dropdown stores one selected option ID as an integer. A multiselect stores a comma-separated list of option IDs in a `varchar` value. Magento uses the `ArrayBackend` backend model to manage that storage. This tutorial also implements `PatchRevertableInterface`, so you can remove the attribute by running `bin/magento setup:rollback`.

## Code
## Data patch implementation

The following sections show the PHP code you add to your module.

### Create the data patch class

Create a data patch class called `AddCustomerAttributeMultipleOptions` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. This makes the application execute the data patch automatically when `bin/magento setup:upgrade` is run. This class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`.
Create a data patch class called `AddCustomerAttributeMultipleOptions` under the `\ExampleCorp\Customer\Setup\Patch\Data` namespace. Magento runs this data patch automatically when you run `bin/magento setup:upgrade`. This class implements both `\Magento\Framework\Setup\Patch\DataPatchInterface` and `\Magento\Framework\Setup\Patch\PatchRevertableInterface`.

```php
<?php
Expand Down Expand Up @@ -89,7 +91,9 @@ public function __construct(

There are five steps in developing a data patch. All the steps below are written inside the `apply` method.

1. Starting and ending the setup execution. This turns off foreign key checks and sets the SQL mode.
1. Start and end the setup execution.

This turns off foreign key checks and sets the SQL mode.

```php
$this->moduleDataSetup->getConnection()->startSetup();
Expand Down Expand Up @@ -147,7 +151,7 @@ There are five steps in developing a data patch. All the steps below are written
| `is_filterable_in_grid` | `true` - Filterable in the customer grid |
| `is_searchable_in_grid` | `false` - Not searchable in the customer grid (multiselect fields are filtered by option, not free-text searched) |

1. Add attribute to an attribute set and group.
1. Add the attribute to an attribute set and group.

There is only one attribute set and group for the customer entity. The default attribute set ID is a constant defined in the `CustomerMetadataInterface` interface and setting the attribute group ID to null makes the application use the default attribute group ID for the customer entity.

Expand Down Expand Up @@ -178,7 +182,7 @@ There are five steps in developing a data patch. All the steps below are written
$this->attributeResource->save($attribute);
```

1. Gracefully handle exceptions.
1. Handle exceptions in a try/catch block.

```php
try {
Expand Down Expand Up @@ -213,7 +217,7 @@ public function revert(): void
}
```

### Implement rest of the interface
### Implement the rest of the interface

This data patch does not have any other patch as a dependency, and this data patch was not renamed earlier, so both `getDependencies` and `getAliases` can return an empty array.

Expand Down