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.

8 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. 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 .

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.

Related postsView all
March 27, 2018

How to Create a Payment Method in Magento 2

Sometimes you might need more than the standard out-of-the-box payment method. Perhaps you have an agreement with a credit card processor and a solution for their […]
March 6, 2018

How to Build a Theme in Magento 2

When building a custom theme for Magento, it’s important to follow best practices for how the system is designed to be extended. Magento 2’s frontend is […]
February 12, 2018

Display Configurable Product Price Ranges in Magento 2

Today we’re going to look at the advantages of using configurable products and how we can update how their prices display on Magento 2 product listing […]
Most Recent PostsView all
October 17, 2018

Llamas and the Mobile eCommerce Optimization Initiative

The Mobile eCommerce Optimization Initiative has kicked off and Classy Llama is participating! The Magento Community has come together to create and test multiple conversion optimization […]
June 7, 2018

From Brick-and-Mortar To Online: How to Connect with a Niche Audience

As an eCommerce business, every penny counts. If you want to be successful, you have to carefully balance spending money to generate business and the profits […]
May 11, 2018

Impact on Ad Rank and SERP’ real estate

The importance of extensions and how they can increase your SERP’ (search engine results page) real estate and ad rank Different kinds of extensions – One […]