# sbodak/magento2-checkout-custom-form

> Add a custom form to Magento 2 checkout on the shipping step.

`composer require sbodak/magento2-checkout-custom-form`

Canonical URL: https://packagento.com/sbodak/magento2-checkout-custom-form

## At a glance

- **Vendor**: sbodak (https://packagento.com/sbodak.md)
- **Latest version**: 2.0.0 — released 2026-04-18
- **Pricing**: Free
- **Package type**: Magento 2 module
- **Status**: active, accepting new buyers

## Installation

Packagento is licence-gated, so even free packages need a licence on a project before Composer can resolve them.

1. **Sign in or create an account** at https://packagento.com/customer/account/.

2. **Add the package to your account.** Open https://packagento.com/sbodak/magento2-checkout-custom-form and complete the free checkout. A licence is minted automatically.

3. **Create or pick a project, then activate the licence on it.**
   - Projects represent the Magento installs you deploy to. Manage them at https://packagento.com/projects/.
   - Activate the new licence on the project you'll deploy this package to. Activation is what generates the Composer credentials scoped to that project.

4. **Add the project credentials to your Magento codebase.**

   Grab the project's public + private key from https://packagento.com/projects/ (open the project, then its Credentials tab), and add them to `auth.json`:

   ```json
   {
     "http-basic": {
       "packagento.com": {
         "username": "ppk_live_...",
         "password": "psk_live_..."
       }
     }
   }
   ```

   Add the Packagento Composer repository to `composer.json`:

   ```json
   {
     "repositories": [
       { "type": "composer", "url": "https://packagento.com" }
     ]
   }
   ```

5. **Install and apply.**

   ```bash
   composer require sbodak/magento2-checkout-custom-form:*
   bin/magento setup:upgrade
   bin/magento setup:di:compile
   bin/magento cache:flush
   ```

## What it does

Add a custom form to Magento 2 checkout on the shipping step.

## README

### Overview

Adds a configurable custom form to the Magento 2 checkout (shipping step), placed above the shipping-method selection.  
The form works for both logged-in customers and guests.  
After an order is placed, all entered data is stored in the `sales_order` table and exposed via extension attributes on `OrderInterface`.  
Form data persists through page refreshes as long as the cart is active.

Fields can be individually **enabled or disabled per store view** from the admin panel.  
Optional **character-length limits** can be configured for each field.

#### Default form fields

| Field | Config key |
|---|---|
| Buyer name | `checkout_buyer_name` |
| Buyer email | `checkout_buyer_email` |
| Purchase order no. | `checkout_purchase_order_no` |
| Goods mark | `checkout_goods_mark` |
| Comment | `checkout_comment` |

#### API endpoints

| Method | Endpoint | Auth |
|--------|----------|------|
| `PUT` | `/V1/carts/mine/set-order-custom-fields` | Customer |
| `PUT` | `/V1/guest-carts/:cartId/set-order-custom-field` | Guest |

---

### Compatibility

| Module version | Magento | PHP |
|---|---|---|
| `2.0.*` | 2.4.6 – 2.4.8 | 8.1 · 8.2 · 8.3 · 8.4 |
| `1.2.*` | 2.3.x | 7.x |
| `1.1.*` | 2.1.x – 2.2.x | *(no longer supported)* |

---

### Installation

```bash
composer require sbodak/magento2-checkout-custom-form
php bin/magento module:enable Bodak_CheckoutCustomForm
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
```

---

### Admin Configuration

Go to **Stores → Configuration → Sales → Checkout → Checkout Custom Form Configuration**.

- **Enabled Form Fields** – multiselect; choose which fields appear in the checkout.
- **\*\* Character Limit** fields – optionally restrict the maximum length of each field (leave empty for no limit).

---

### Customisation

#### Adding / removing fields

1. Add the constant to `Api/Data/CustomFieldsInterface.php` and the `ATTRIBUTES` array.
2. Add the column to `Setup/Patch/Data/AddCustomFields.php`.
3. Extend `Model/Data/CustomFields.php` with getters, setters, and `isXxxEnabled()`.
4. Register the field in `Model/Config/Source/Option.php`.
5. Add it to `Model/Checkout/LayoutProcessor/Plugin.php` (`$fields` array).
6. Update `Observer/AddCustomFieldsToOrder.php` if custom mapping is required.
7. Update `Observer/Sales/OrderLoadAfter.php` to populate the extension attribute.
8. Update templates in `view/adminhtml/templates/order/view/custom_fields.phtml`
   and `view/frontend/templates/order/view/custom_fields.phtml`.

#### Making a field required

In `Model/Checkout/LayoutProcessor/Plugin.php`, add a `validation` key to the field array:

```php
[
    'dataScopeName' => CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO,
    'label'         => 'Purchase order no.',
    'validation'    => ['required-entry' => true],
],
```

#### Overriding translations

Edit `i18n/en_US.csv` (or create a language-specific CSV, e.g. `de_DE.csv`).

---

### Screenshots

#### Checkout – Guest
![Checkout frontend custom form – Guest](docs/frontened_checkout_custom_form_guest.png)

#### Checkout – Logged in
![Checkout frontend custom form – Logged in](docs/frontened_checkout_custom_form_logged.png)

#### Customer account – Order history
![Customer account – Order history view](docs/frontend_customer_account_orders.png)

#### Admin panel – Order edit
![Admin panel – order edit](docs/backend_order_custom_information.png)

---

### Running Tests

```bash
composer install
vendor/bin/phpunit
```

---

### Uninstall

```bash
php bin/magento module:uninstall Bodak_CheckoutCustomForm
```

This drops the custom columns from the `sales_order` and `quote` tables.

---

### Changelog

See [CHANGELOG.md](CHANGELOG.md).

### License

[MIT License](LICENSE)

## Changelog

All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [2.0.0] - 2026-04-18

#### Breaking Changes
- Moved all module source code into `src/` directory; only `composer.json`, `LICENSE`, `README.md`,
  `docs/`, and `CHANGELOG.md` remain in the repository root.
- Replaced deprecated `Setup/InstallData.php` with `Setup/Patch/Data/AddCustomFields.php`
  (Magento 2.3+ declarative data-patch API).
- Removed `setup_version` from `etc/module.xml` (no longer required in Magento 2.4).
- Setter and getter signatures now use proper `?string` nullable types and return types.

#### Added
- **Magento 2.4.8 compatibility** — tested against M2.4.7 / M2.4.8.
- **PHP 8.1 – 8.4 support** in `composer.json`.
- **Admin configuration** (`etc/adminhtml/system.xml` + `etc/config.xml`): enable/disable
  individual form fields per store view and optionally set per-field character limits.
- **`Helper/Config`** — centralises access to `bodak/checkout/*` store configuration.
- **`Model/Checkout/LayoutProcessor/Plugin`** — injects enabled fields into the checkout JS layout
  at runtime, replacing the hardcoded `checkout_index_index.xml` field definitions.
- **`Model/Config/Source/Option`** — multiselect source model for the admin field selector.
- **`Model/CustomFields/Validator`** — validates field lengths against configured limits before
  persisting to the quote; uses `Laminas\Filter\Word\UnderscoreToCamelCase`.
- **`Observer/Sales/OrderLoadAfter`** — populates Sales Order extension attributes after load
  so custom fields are available via the REST API response.
- **`etc/extension_attributes.xml`** — declares extension attributes on `OrderInterface` for all
  five custom fields.
- **`etc/events.xml`** — added `sales_order_load_after` observer.
- **`etc/di.xml`** — added `LayoutProcessor` plugin registration.
- **`i18n/de_DE.csv`** — German translations.
- **Custom textarea template** (`view/frontend/web/template/form/element/textarea.html`).
- **Unit test suite** (`tests/Unit/`) covering `CustomFields`, `Config`, `Validator`, and
  `AddCustomFieldsToOrder`.

#### Changed
- `Observer/AddCustomFieldsToOrder` — only copies fields that are enabled in admin config
  (previously always copied all fields).
- Frontend and admin templates now conditionally render only enabled fields via
  `isCheckoutXxxEnabled()` methods.
- `checkout_index_index.xml` simplified: field definitions are now managed entirely by the
  `LayoutProcessor` plugin rather than XML items.
- Dependency declarations in `composer.json` now include `magento/module-checkout`,
  `magento/module-quote`, `magento/module-sales`, and `magento/module-store`.
- Templates updated from deprecated `/* @escapeNotVerified */` comments to `$block->escapeHtml()`
  short syntax (`<?= ?>`).
- Module sequence in `etc/module.xml` extended to include `Magento_Checkout`, `Magento_Quote`,
  and `Magento_Store`.

#### Fixed
- `Model/Data/CustomFields` now correctly calls `parent::__construct()` on
  `AbstractExtensibleObject`.

---

### [1.2.1] - Previous release

- Magento 2.3 compatibility.

### [1.1.*] - Legacy

- Magento 2.1.x – 2.2.x support (no longer maintained).

## Recent Versions

| Version | Released |
|---|---|
| 2.0.0 | 2026-04-18 |
| 1.2.1 | 2019-03-27 |
| 1.2.0 | 2019-03-14 |
| 1.1.4 | 2018-09-19 |
| 1.1.3 | 2018-09-19 |
| 1.1.2 | 2018-04-04 |
| 1.1.1 | 2018-03-03 |
| 1.1.0 | 2018-03-03 |
| 1.0.3 | 2017-11-13 |
| 1.0.2 | 2017-11-12 |

Showing 10 of 12 versions. Full release history on https://packagento.com/sbodak/magento2-checkout-custom-form.

## Dependencies

### Require

| Package | Constraint |
|---|---|
| laminas/laminas-filter | ^2.25 |
| magento/framework | ^103.0 |
| magento/module-checkout | ^100.4 |
| magento/module-quote | ^101.2 |
| magento/module-sales | ^103.0 |
| magento/module-store | ^101.1 |
| php | ^8.1\|^8.2\|^8.3\|^8.4 |

### Require (dev)

| Package | Constraint |
|---|---|
| phpunit/phpunit | ^10.5\|^11.0 |

## Quality

Latest release (2.0.0) fails the Packagento QA pipeline. Verdicts below are per-cell (Magento line × PHP version) for the matrixed tools, and run-once for the static / security tiers.


### Compatibility

Each Magento line is installed on its supported PHP versions, then the module is built (DI compile + static-content deploy). Cells show passed / failed / untested; staircase gaps render as `–`.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | Pass | Pass | – | – |
| 2.4.8 | – | Pass | Pass | – |
| 2.4.9 | – | – | Pass | Pass |


### Code Quality

Advisory checks against the module's source. Never affect the Compatibility verdict — a phpcs finding can't make a module incompatible.

#### Static Analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| PHPCS | Fail | 81 | 2 errors, 79 warnings (ruleset: Magento2) — 54 auto-fixable with phpcbf |
| PHPMD | Warning | 3 | 3 rule violations (UnusedFormalParameter:2, TooManyPublicMethods:1) |
| Cpd | Pass | 0 |  |
| Composer validate | Info | 1 | valid; 1 advisory note (composer validate --strict) |

#### PHPStan

Type-checks the module against a real Magento install. Re-runs per Magento + PHP version because resolvable symbols differ between releases.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | 3 | 3 | – | – |
| 2.4.8 | – | 3 | 3 | – |
| 2.4.9 | – | – | 3 | 3 |


### Tests

Unit and integration suites run per Magento + PHP cell. Test failures speak to the module's behaviour, not its compatibility with a line, so they're reported here separately.

#### Unit Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |

#### Integration Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |


### Security

Dependency-advisory audit (composer audit) plus a source malware scan. A malware detection fails the version outright.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| Composer audit | Pass | 0 |  |
| Malware scan | Pass | 0 |  |

## Licence and pricing

Free. A licence is still minted on checkout and bound to your project for Composer access — no payment step.

Refundable within 14 days of first purchase via https://packagento.com/account/refunds/.

## Install via Claude Code or any MCP client

The Packagento MCP server can run the licence + project + Composer steps above in one tool call:

```
purchase_and_install_packages(
  composer_names=["sbodak/magento2-checkout-custom-form"],
  project_id="proj_xxx"
)
```

This handles cart, checkout, licence minting, project activation, and writes auth.json credentials. Connect a client with `claude mcp add packagento https://mcp.packagento.com`. Full setup at https://packagento.com/docs/mcp-setup.

## Vendor

sbodak is a Magento 2 vendor on Packagento. See https://packagento.com/sbodak.md for their full catalogue.

