Get Free Audit

How to Create a Custom Product Type and Specific Import/Export in Magento 2

Nov 22, 2016

3712 Oleg Semenov

How to Create a Custom Product Type and Specific Import/Export in Magento 2

In the following article I would like to share my experience of Custom Product Type creation and I’d like to guide you how to import it to the system in specific way.

Sometimes we are facing some extraordinary needs of our clients about product types, and these types are easy to create by observer or default product types. But sometimes there is no way, except of creating your own type of product. For example it could be applied to gift cards or other types of VIP cards.

I won’t tell you about creating a new module from scratch (this info you can find from another source), so let’s get into the basic part:

Let’s say you’ve already created a module and the first thing we have to do is to add a file with description of a new type of product: <Vendor_Name>/<Module_Name>/etc/product_types.xml

Example:


You can specify such attributes:

  • name: new product code
  • label: product name
  • modelInstance: new product model
  • indexPriority: priority index
  • isQty: whether it has a quantity
  • sortOrder: position number in sort list
  • canUseQtyDecimals: whether the quantity is fractional

Also we are capable to override such internal models:

  • priceModel: model of a price
  • indexerModel: model of indexing logic
  • stockIndexerModel: model of stock indexing

Magento Audit

Take your online store to the next level with BelVG Magento Audit

Visit the page

Example:


Or we can add custom attributes:

Example:


So now let’s take a look at class content. First of all:

<Vendor_Name>\<Module_Name>\Model\Product\Type.php

Example:


Custom model of the price:

<Vendor_Name>\<Module_Name>\Model\Product\Price.php

Example:


We created primary files for a new product. Now we should initialize it into the system. And for that we must describe process of installation of the module.

<Vendor_Name>\<Module_Name>\Setup\InstallData.php

Example:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


How to Create a Custom Product Type and Specific ImportExport in Magento 2

Perfect! A new product is done. And now if you want to fill your store with new products, what should you do?

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Further I am going to describe a process of creating a custom import of our new products. First of all we must create the file:

<Vendor_Name>/<Module_Name>/etc/import.xml

  • entity: EAV import model
  • name: import code
  • model: data processing model

In the current xml file we describe import model and its relations, for example reindexing trigger after import.

Below is the import model that could be inherited by abstract class

\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType


Data export process looks rather similar. We describe export.xml file, it has the same content, but refer to export models and export class of our type.

Well, I think that’s all. Thanks for reading this brief guide and I hope it will be helpful in the process of creating new products and import.

Here is an example of the module.

Magento Development

Take your online store to the next level with BelVG Magento Development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

4 Comments

  1. Notice: Use of undefined constant ‘my_new_product_type’ – assumed ‘‘my_new_product_type’’ in /var/www/html/xxxxxx/app/code/Webkul/Sellerproduct/Setup/InstallData.php on line 47

    i am facing this kind of issues while upgrade…

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *