Draft orders for a product type
Let Create order offer a product type that Orders does not handle itself, with a provider class and an item in the draft_product_types pool.
"Create order" builds a real cart behind the scenes, so every product on it needs a buy request that Magento accepts. Orders handles simple, virtual and configurable products itself. A module that adds another product type, such as a bundle or a group page, tells Orders how to ask for it by adding a provider to the pool draft_product_types.
When to use it
- Your module makes products of a Magento type that needs more than a quantity to go on an order: a choice per part, a quantity per item, a date.
- It is a kind of product from a pack (Add a kind of product) and the merchant should be able to take a phone order for it.
- Keep the fields few and say plainly what each asks. A manual order is a screen the merchant fills in with the customer on the phone (Built for Light, rule 2).
Without a provider, the picker shows the product and says to add it in the advanced view.
Steps
1. The provider class
One class per Magento type id implements Mrx\Orders\Api\DraftProductTypeInterface:
interface DraftProductTypeInterface
{
/** False when the product uses settings a manual order can't offer; the picker then shows OR2. */
public function isSupported(ProductInterface $product): bool;
/**
* The fields the picker shows for this product, in order. A 'choice' field has options; a 'quantity' field has
* none and takes a number of 0 or more. Prices are catalogue prices in the base currency and the catalogue's
* tax basis, like DraftLine's price.
*
* @return list<array{type: 'choice'|'quantity', id: string, label: string, required: bool,
* options?: list<array{id: string, label: string, price: float, available: bool}>,
* default?: string|float}>
*/
public function getFields(ProductInterface $product, int $storeId): array;
/**
* The buy request for Quote::addProduct(). $qty is the line quantity; a type whose fields carry the quantities
* (a group page) gets 1.
*
* @param array<string, string|float> $values field id => the chosen option id or the quantity
* @throws LocalizedException with a message for the merchant when a required value is missing or no longer offered
*/
public function buildRequest(ProductInterface $product, array $values, float $qty): DataObject;
}isSupported() answers whether a manual order can offer this product. Return false for a product that uses settings you cannot ask for, such as a bundle the editor shows read-only. The picker then says "Add this product in the advanced view." (a bundle gets "Add this bundle in the advanced view.", OR2).
2. Describe the fields
getFields() returns the fields the picker draws, in order. A product with fields appears as one option labelled "Pick per part" (OR1), and its line shows one input per field under the name:
| Field type | Picker input | Value you get back |
|---|---|---|
choice | A select over options, each with an id, a label, a price and an available flag. default preselects one. | The chosen option id |
quantity | A number of 0 or more. default is the starting number. | The number |
Prices are catalogue prices in the base currency and the catalogue's tax basis, like a line's own price. A type whose fields carry the quantities hides the line's price and quantity controls.
The group pages provider asks for a quantity per product on the page. The field id is the product's id, and the default is the quantity the storefront fills in:
public function getFields(ProductInterface $product, int $storeId): array
{
$fields = [];
foreach ($this->items($product, $storeId) as $item) {
if (!$item['row']['enabled']) {
continue;
}
$fields[] = [
'type' => 'quantity',
'id' => (string)$item['row']['id'],
'label' => $item['row']['name'] !== '' ? $item['row']['name'] : $item['row']['sku'],
'required' => false,
'default' => $item['qty'],
];
}
return $fields;
}The bundles provider returns one choice field per part, so the picker draws a select for each; a part with one product is preselected and still posted, because Magento wants every required part.
3. Build the buy request
buildRequest() turns the values into the data object that Quote::addProduct() takes. $values maps a field id to the chosen option id or the quantity. $qty is the line quantity, and a type whose fields carry the quantities gets 1.
The group pages provider fills Magento's super_group:
return new DataObject([
'product' => (int)$product->getId(),
'super_group' => $superGroup,
'qty' => $qty,
]);The bundles provider fills bundle_option and bundle_option_qty instead:
return new DataObject([
'qty' => $qty,
'bundle_option' => $bundleOption,
'bundle_option_qty' => $bundleOptionQty,
]);Throw a LocalizedException with a message the merchant can act on when a required value is missing or no longer offered. Orders shows the message for that line. Any other exception is logged, and the line says "This product can't be ordered right now."
4. Register it
Add an item to the pool, in the etc/adminhtml/di.xml of your module. The pool is keyed by an item name of your choice:
<item name="group_page" xsi:type="array">
<item name="type_id" xsi:type="string">grouped</item>
<item name="provider" xsi:type="object">Mrx\ProductGroupPages\Model\Draft\GroupPageType\Proxy</item>
<item name="module" xsi:type="string">Magento_GroupedProduct</item>
</item>Prop
Type
The ACL is Orders' own: the finder and the calculate and place routes sit behind Magento_Sales::create, so the item has no resource.
How Orders uses it
ProductTypes::get() returns the provider for a type, skipping an item whose module is off. The product finder asks isSupported() and getFields() for each result, and the quote builder asks buildRequest() when the order is calculated and placed. A provider that throws in isSupported() or getFields() makes its product unsupported, and Orders logs it. A field that is not a choice or quantity, has no id, or is a choice without options is dropped before the picker sees it. A required field that is dropped makes the product unsupported, and a product with more than 50 fields goes to the advanced view.
A bundle with the bundle pack on shows one select per part, here with the lamp choice preselected:

Check it
bin/magento mrx:light:doctor --module=<your module>. The ruledraft_product_type(error) wants a provider that implements the interface, a type Magento registers that is not simple, virtual or configurable, and one provider per type (Troubleshooting).- A unit test with a fake product: assert
getFields()for a product with and without items, andbuildRequest()for a normal pick, a missing required value and a stale option. - Place a manual order for the type and open it: the order lines are the ones the storefront would make.
Pitfalls
- Interface changes.
DraftProductTypeInterfaceis@api. It never gains a method within a major, so a provider you write for 0.6 keeps working on every 0.x after it that stays in range. - Prices. Return catalogue prices, not the cart's. The calculator runs a real cart and shows the real totals; your prices only label the options.
- Stale data. Read the product's parts and stock when asked, and keep no state between orders. The finder asks again on every search.
- A part with no stock. Mark the option
available: falserather than leaving it out. The picker labels it "Sold out" and does not preselect it on its own; don't pointdefaultat a sold-out option.
Last updated on