Creating New Product Type and Product Attribute in Magento 2

In Magento, there are 6 types of products by default: simple, configurable, virtual, downloadable, bundle and grouped products, and they delivers various unique features and options that common merchants will find it sufficient for their webstore. However, sometimes we need more than those, so In this post we will find out how to create a new product type and also how to create a new attribute for a product type.

1. Create a new product type

To add a new product type, you just have to create a new file named product_types.xml in etc folder. The content of the file should be something like:

Above we are creating a new product type named “Magenest” and have the type id of “magenest”. The custom attributes is of your choice. Now you should create a file at Vendor\Module\Model\Product\Type and have the name “Magenest” with the following content:

This is the minimum amount of code that you should implement. In the future, maybe you will need to write some helper methods that you can call on the object of the product of this type.

Done, the above steps should create a new product type for you. If you check the Add new product dropdown, here is what you should see:

creat product type in magento 2

2. Create a new attributes

This part should also be no trouble for you. Lets go ahead and create a new file named InstallData.php in Setup folder and add some lines of code:

This will add a new product attribute in the Product Details group of your backend product detail page. This attribute will be of type varchar and will be displayed as a dropdown since the input is set to Boolean. Other options is up to you to choose.

One more thing here, if you create a new product of type Magenest but you can’t see where to set the price, add these lines into your InstallData.php, method install.

This will add some fields into your product types. Now if you look back, you should see an attribute as following:

create product attribute in magento 2

That’s about it for this post. Hope you can soon apply these into your extension.

Relevant Entries
How To Install Magento 2 Running On Zend Server On Ubuntu20170601153207
How To Configure Github API In Magento 220170918200005
Social Login: What Is It?20170913200003