Custom Product Types in Magento 2
Custom Product Types in Magento 2

Custom Product Types in Magento 2

Published June 24, 2016 in Development
Magento 2.1 released
Magento Releases 2.1
June 23, 2016
Classy Llama Supports Ride for Korah
June 29, 2016

Overview

Magento supports a number of product types, each with its own behavior and attributes. This powerful concept allows Magento to support a wide range of industries and merchant needs by mixing and matching product experiences in their catalog.

Even more powerful, however, is the ability for developers to easily add new product types.

In general, when a product class has distinct behavior or attributes, it should be represented by its own product type. This allows the product type to have complex and custom logic and presentation, with no impact on other product types — ensuring that native product types can continue to function as intended.

Implementation

Config XML

As with most things in Magento, the first step to defining a custom product type involves an XML declaration. Specifically, in the <Vendor>/<Module>/etc/product_types.xml, add an XML snippet of the following form to declare the new product type’s critical information.

The type node has three required attributes.

  • name: This attribute defines the custom product code which is used in code and in the database.
  • label: Defines the product type label in the Magento admin.
  • modelInstance: Specifies the fully qualified namespace of the corresponding product type model.

Although there are many other nodes and attributes that can influence the product type definition, this is an example of the simplest case.

Product Type Model

Having declared the product type, the next critical component is the product type model. Each product instance is associated with an instance of the corresponding product type model. This model has the opportunity to modify product type behavior and attributes and is called during many product manipulation processes.

Product Type Model Abstract

A product model must inherit from the \Magento\Catalog\Model\Product\Type\AbstractType base class. The base class has only one abstract method: deleteTypeSpecificData. This oddly specific method is called during a product instance save if its type has changed, and gives the original product type the opportunity to clean up any type-specific data before the type change is finalized.

Unless the new custom product type has such type-specific data, the method can be overridden with an empty method.

Finally, although not strictly required, it is conventional for product type models to include a constant with the name TYPE_CODE which defines the product type code string, so it can be referenced in code in a consistent manner.

Core Product Types

It’s often the case that a custom product type behaves like and has similar requirements of a core product type. In this case, it can be convenient to extend one of the core product types instead of the abstract directly.

The following product type classes are available natively in the core.

  • Simple: \Magento\Catalog\Model\Product\Type\Simple
  • Virtual: \Magento\Catalog\Model\Product\Type\Virtual
  • Configurable: \Magento\ConfigurableProduct\Model\Product\Type\Configurable
  • Grouped: \Magento\GroupedProduct\Model\Product\Type\Grouped
  • Downloadable: \Magento\Downloadable\Model\Product\Type
  • Bundle: \Magento\Bundle\Model\Product\Type
  • Giftcard (Enterprise Edition Only): \Magento\GiftCard\Model\Catalog\Product\Type\Giftcard

Results

Once the product type is declared in XML and its associated product type model is created, products of the new type can be created in the admin.

m2 custom product type

Additional Steps

Having successfully created a new product type, there are some common (although optional) steps to fully utilize it.

Associate With Common Attributes

Since product attributes can be scoped to relevant product types, any core attributes which are already scoped will not apply to the new product type. Any such attributes which are indeed relevant will need to be associated with the new product type in an install data script.

This example install script associates price and weight attributes in the $fieldList array with the new product type. Once this install script is run, these product attributes will show in the admin when editing or creating a product of the new type.

Composite Products

When adding child products to a composite product type (grouped, configurable or bundle), the admin interface will only show products where the associated product type has explicitly declared its eligibility to be the child of a composite product. New product types can allow themselves to be children of composite product types by adding a node to the composableTypes node in Vendor/Module/etc/product_types.xml .

Where To Go From Here

Creating a new product type is only the beginning. Now that it’s defined, the new product type provides an enormous capacity for scoped customization.

Product Type Model Methods

The behavior of products can be significantly influenced by overriding methods in the product type model. This allows custom behaviors to be encapsulated within the product type rather than global observers/plugins.

Some examples of such methods:

  • beforeSave() and save(): these methods run during the beforeSave() and afterSave(), respectively, methods of products of the given type.
  • isSalable(): allows a custom product type to customize product saleability.
  • _prepareProduct(): provides a hook to interact with the info by request when a product is initially added to the cart.

Price Model

In addition to the product type model, a module can specify a price model in the product type definition XML.

The price model referenced should extend \Magento\Catalog\Model\Product\Type\Price. This class has no abstract methods which must be implemented, but allows extending classes to interact with nearly every aspect of price calculation.

Summary

By providing a framework for defining custom product types, Magento empowers merchants and developers to implement significant customization to facilitate business requirements without risking default product behavior.

14 Comments

  1. PJ says:

    Data Setup Scripts is not working. It is giving an error:
    Installing data…

    [Exception] Notice: Undefined index: entity_type_id in D:wamp64www..vendorm agentomodule-eavSetupEavSetup.php on line 1065

    setup:upgrade [–keep-generated] [–magento-init-params=”…”]

  2. Abbas says:

    Thank you for such a easy post to understand.

    I am trying to create a new product type which basic functionality is similar to the configurable product that’s why i extend product type with \Magento\ConfigurableProduct\Model\Product\Type\Configurable but still it is not showing the functionality of the Configurable. Such as my custom product type is not showing the Tax Class field and Configurations tab while creating my custom product type.

    Can you please help me how to achieve it? Thank you very much

  3. Taz says:

    I followed this tutorial but in admin, when I create product it does not show price option. Can you provide some input please?

  4. alex says:

    Thanks for sharing, Here’s another guide that helped me, https://www.cloudways.com/blog/magento-2-bundle-products/

  5. Cheayden says:

    I am new in Magento 2, but I have a problem how to remove/hide the core product type from drop-down in admin dashboard.
    Thanks you so much,

  6. Bhagyashri says:

    In my project, we are having a requirement for kitchen cabinet to supply and which product type do we needed, that I am confused. So can you guide me for the same?

  7. Ravindra says:

    Great post i really appreciate this post it seems that there’s a lot of interesting on this site i will bookmark your page for more new updates on your site. keep it up .

  8. KaMZaTa says:

    I’d like to create a product which can be either shipped or downloaded. Does anybody have an idea to how achieve it? I want to do this in order to avoid to create a grouped product which contains a simple product and a downloadable product (this is how I did till now).

  9. Kirill from Magecom says:

    Thx for useful article.
    By the way, in Magento 2.3 i faced the next issue – there’s need one more attrubute for type tag in xml – isQty=”true”. If it have no this, you just can’t set quantity for given type of product when you gonna fill in admin.
    So the final solution for Magento 2.3 should be like that:

  10. Ars says:

    Please write a tutorial on
    How to extend bundle product type as it’s not working and i didn’t found any help…..

  11. useful and valuable details you shared, thanks for the important blog post. It helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Recent PostsView all
October 15, 2021

Classy Llama Wins 2021 Comparably Awards for Best Perks&Benefits, Best Work-Life Balance

Classy Llama, Springfield’s only full-service eCommerce agency, has been placed on the top 50 list for Comparably.com’s Best Perks&Benefits Award, as well as their Best Work-Life […]
September 15, 2021

Classy Llama Awarded #3 Best Place to Work by Biz417

Springfield, MO — Classy Llama has been awarded the number 3 spot on the Biz417 Best Places to Work Top 20 for 2021.  This award is […]
September 13, 2021

2021 Instagram Changes: What you need to know for eCommerce Marketing

What’s Going On With Instagram in 2021? The Head of Instagram, Adam Mosseri, announced in summer 2021 that big changes are coming to the platform sometime […]