Light API 0.18.0 is a public beta: a minor release such as 0.19.0 may still break. What that means for your module
Light
Developer guideRecipes

Add a kind of product

A pack that adds a kind of product to Light's product editor: a pool item, a card for that kind, a form extension and a registration guard.

A kind of product is a choice the merchant makes on "Add product": a plain product, a bundle, a group page, a digital download. Core ships the kind product. A pack adds another by claiming a Magento product type in the pool product_kinds, then fills the product editor with a card for that kind. Light's bundles pack (mrx/module-product-bundles), group pages pack (mrx/module-product-group-pages) and downloads pack (mrx/module-product-downloads) are built this way, and this recipe follows the group pages pack.

When to use it

  • Your module makes products of one Magento product type that Light's editor doesn't edit itself, such as a gift card, a rental or a subscription type.
  • The merchant picks the kind once, when the product is made. A saved product keeps its type, and Light offers no way to convert one (Built for Light, rule 2).
  • Keep the form to the fields that kind needs. Everything else stays in the advanced view.

Variants and "not shipped" are not kinds. They belong to the plain product, and Catalog handles them.

The variants editor shows what they look like in 0.6.0: slim rows that group by the first option above 12 variants, a row detail, a bulk bar, and "Remove options" to turn a product back into one.

The variants card of a product with 15 variants, grouped by the first option and closed: Grijs, Beige and Marineblauw with their count and stock

The variants editor with the Grijs group open: slim rows, the detail of Grijs / XS open under its row, and the bulk bar for the five ticked variants

The Turn this back into one product? modal of Remove options, with the variant to keep preselected and the red Remove options button

What the merchant gets

A kind of a composite type (Magento's bundle or grouped type) shows up in three places without any code of yours:

  • With one kind installed, "Sell products together" opens that kind's form and carries its action_label. With two or more, it opens the chooser at light/products/new?kind=choose, a card per kind.
  • A name typed on "Add product" travels to the chosen kind. The save bar's allowLeave lets the link leave without the discard question while only the name changed.
  • Without any kind pack, none of these shows.
  • "Sell products together" takes only kinds of a composite type. A kind of another type, like the downloads pack's download, is left out of the button, the quiet link and the chooser, and brings its own way in: a palette action on the stock route catalog/product/new with its type (your claim sends it to ?kind=<code>), the stock "Add Product" menu, and if it fits a line of its own in mrx.product.form.main.after_pricing on a new product. The downloads pack shows "Selling a file, like an e-book? Make it a digital download." there while "This is a physical product" is off.

The list's stock pill opens a popover with the stock of each variant, part or item, sold out first:

The product list with a stock pill for a product with 15 variants, and its popover listing the variants, sold out first

The chooser, with one card per installed kind:

Sell products together: one card for a bundle and one for a group page, and the link Just one product? Add product

The two packs of Light show their own card on the editor. The bundle's:

The bundle's What's inside card, with a choice between two lamps and an optional vase, and the Price card under it

And the group page's:

The group page's Products on this page card: three products with their prices, stock and a pre-filled quantity each

Steps

1. Claim a type with a pool item

The pool product_kinds is keyed by the kind's code. Register your item in your module's etc/adminhtml/di.xml. The group pages pack claims Magento's grouped type:

packages/module-product-group-pages/etc/adminhtml/di.xml
<item name="group_page" xsi:type="array">
    <item name="type_id" xsi:type="string">grouped</item>
    <item name="type_ids" xsi:type="array">
        <item name="grouped" xsi:type="string">grouped</item>
    </item>
    <item name="label" xsi:type="string" translate="true">Group page</item>
    <item name="description" xsi:type="string" translate="true">Separate products together on one page. Each product is paid for separately. The customer picks how many of each, like a table with matching chairs.</item>
    <item name="title" xsi:type="string" translate="true">Add group page</item>
    <item name="action_label" xsi:type="string" translate="true">Make a group page</item>
    <item name="icon" xsi:type="string">columns</item>
    <item name="sort_order" xsi:type="number">30</item>
    <item name="resource" xsi:type="string">Magento_Catalog::products</item>
    <item name="module" xsi:type="string">Magento_GroupedProduct</item>
</item>

Prop

Type

Mark label, description, title and action_label with translate="true": Kinds runs them through __(). Kinds::getAllowed() leaves out an item that is null, one whose module is off, one the admin has no resource for, and one whose type Magento doesn't register (that one is logged).

2. Show a card for your kind only

The card goes into the editor's container mrx.product.form.main.after_pricing. Core's Pricing and Stock cards show only for simple, virtual and downloadable products (Stock also for configurable). A kind for any other type gets neither, so your card is the main content and must carry any price or stock the kind needs, or leave them to the advanced view. It asks Mrx\Catalog\Api\ProductKindContextInterface::getKind() which kind the page shows:

packages/module-product-group-pages/ViewModel/GroupPageCard.php
public function isVisible(): bool
{
    return $this->kindContext->getKind() === GroupPageExtension::CODE
        && ($this->currentForm->get()?->getStoreId() ?? 0) === 0;
}

getKind() returns the kind's code. On Add product it is the kind the page opened for, on a saved product the kind whose type_ids claim its type. It returns null on the chooser and for a type no kind claims, so a card that compares with === never shows there. The group page card also hides in a language view: a language changes texts, not the page's products.

The layout puts the block in the container, and the template returns early when isVisible() is false:

packages/module-product-group-pages/view/adminhtml/layout/light_products_form.xml
<!-- Renders only for the group_page kind (GroupPageCard::isVisible()). -->
<referenceContainer name="mrx.product.form.main.after_pricing">
    <block class="Magento\Backend\Block\Template" name="mrx.product_group_pages.card"

The card renders inside the editor's <form>: give every button type="button", and name an input ext[<code>][<field>] so the save bar tracks it (Fields on a Light editor).

3. Save it with a form extension

The editor posts the kind, and Saver makes the product with the kind's type_id. Your values travel in the ext[<code>] namespace to a class registered in the product form's extension pool, with the same ACL resource:

packages/module-product-group-pages/etc/adminhtml/di.xml
<item name="product" xsi:type="array">
    <item name="group_page" xsi:type="array">
        <item name="extension" xsi:type="object">Mrx\ProductGroupPages\Model\Form\GroupPageExtension\Proxy</item>
        <item name="resource" xsi:type="string">Magento_Catalog::products</item>
    </item>
</item>

apply() sets your values on the product before the editor saves it. The group pages class turns the posted SKUs into the page's product links:

packages/module-product-group-pages/Model/Form/GroupPageExtension.php
public function apply(FormContextInterface $context, array $input, object $entity): void

Rules for the class, learned from the two packs:

  • Act only when your card posted. The card adds a marker (ext[group_page][posted]=1); a post without it leaves the product alone, and so does a post that names your code for another type.
  • Never wipe what is not yours. Magento's product repository rewrites every link type of a product whose links are set, so the group pages class sends the related, up-sell and cross-sell links back as they were (Fields on a Light editor, pitfalls).
  • Put what changes the product in apply(), and what must follow the saved product in save(). The two packs write the product's stock item in save(), because it needs the product's id.
  • Return the merchant's mistakes from validate() as a message per field. The message names the item and says what to do.

4. Guard the registration

A pack that claims a Magento type must not register on a shop without that type. registration.php checks both the Light range and the type's class:

packages/module-product-group-pages/registration.php
// Only with a Light that satisfies ^0.18 and with Magento's grouped type present; otherwise group pages open read-only.
if (class_exists(\Mrx\Light\Api\ExtensionApi::class) && \Mrx\Light\Api\ExtensionApi::satisfies('^0.18')
    && class_exists(\Magento\GroupedProduct\Model\Product\Type\Grouped::class)
) {
    ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Mrx_ProductGroupPages', __DIR__);
}

Without Magento's module the pack stays out, and the shop loses nothing. The module key on each pool item covers the other case: the Magento module is on disk but switched off.

Add the usual range pieces to the package too: extra.mrx-light-api and a require on mrx/module-light, mrx/module-catalog and, when you add a draft type, mrx/module-orders, all at ^0.18 (Versioning).

5. Put it on manual orders

A product type that "Create order" does not know is refused there. Add a draft product type so the picker can offer yours (Draft orders for a product type). Without one, the picker says to use the advanced view.

What the admin sees without your pack

A product of your type that already exists still opens in Light, read-only, with a link to the advanced view. Kinds::forType() finds no kind for it, so getKind() is null and your card stays away. The doctor rule product_kind_pack counts those products when the type has a known pack that is off (Troubleshooting). That rule knows Light's own three packs only; it does not check a pack of yours.

Check it

  • bin/magento mrx:light:doctor --module=<your module>. product_kind_type (error) fails an item with no type_id, a type Magento doesn't register, or a type two kinds claim. unknown_icon catches a misspelled icon, and missing_module an item without a module key.
  • getKind() in a unit test: build the view model with a context fake and assert isVisible() for your code and for null.
  • Round trip every data-changing field: save twice and compare, switch your pack off and on, and check that the stored data is the same. The two packs run a database snapshot diff for this.

Pitfalls

  • One type, one kind. Two kinds that claim the same type are an error, and Light opens the type as the first one.
  • Don't name your Magento module in Catalog. Catalog and Orders never name a pack or its Magento module. Everything reaches them through the pools.
  • The type's price and stock rules. Core's Pricing and Stock cards show only for simple, virtual and downloadable products (Stock also for configurable), so every other type, a bundle and a group page included, and also your gift card or rental, gets neither card. Types::hasOwnPrice() and Types::hasOwnStock() decide this inside Catalog; a type from a module Light does not know opens with the banner "You change this kind of product in the advanced view." unless a kind claims it.
  • A kind that holds stock but never ships. Core starts a new product of such a type (a digital download) with "Track stock" off, so it isn't sold out at 0 on its first save.
  • Stock status. A composite product is in stock when its parts are. Set stock_status_changed_auto on its stock item so Magento keeps the status in step, as both packs do.

Last updated on

On this page