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

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

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:

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?

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.



Post a new comment

top