CREATE A NEW PRODUCT TYPE IN MAGENTO 2

How To Create/Build New Product Type In Magento 2

Magento has several inbuilt product types that have their own unique behavior, attributes, and functionalities. However, the magic of Magento does not stop here. It’s so flexible and extendible that you can create your own product types in Magento to serve your or your client’s business needs.

The Existing Inbuilt Magento 2 Product Types Are:

  • 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/ Cloud Edition Only): \Magento\GiftCard\Model\Catalog\Product\Type\Giftcard

The best use case to create a product type is when your requirement about the product class functionality and behavior is different than what is inbuilt. In that case, it’s an excellent time to create a new unique product type for that need. This helps to drive the complex and custom logic into this product type without touching or interfering with existing product types.

To create/ build a new product type in Magento 2, we will build a Magento 2 module.

Step1: Create registration.php in app/code/Bizspice/Newproducttype

<?php 
\Magento\Framework\Component\ComponentRegistrar::register( 
\Magento\Framework\Component\ComponentRegistrar::MODULE, 
'Bizspice_Newproducttype', 
__DIR__ 
);

Step2: Create module.xml in app/code/Bizspice/Newproducttype/etc

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> 
  <module name="'Bizspice_Newproducttype'" setup_version="1.0.0"></module> 
</config>

Step3: Create product_types.xml in app/code/Bizspice/Newproducttype/etc

Here we declare critical information about new product types like name, label, corresponding product type model, etc.

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
  <type name="new_product_type" label="New Product Type" modelInstance="Bizspice\Newproducttype\Model\Product\Type\NewProductType" indexPriority="60" sortOrder="80" isQty="true">
<priceModel instance="Bizspice\Newproducttype\Model\Product\Price" />
</type>
</config>

The type node has three required variables

  • name: This is used to define the custom product type code, which can be further used to identify in our coding and database.
  • label: this is the label name that will be used in the Magento admin
  • modelInstance: this is the namespace, i.e. fully qualified namespace name for the product type mode

Here we have added one more model to define the charge of a new product type.

Step 4: As we have defined our model for our new product type

Create NewProductType.php in app/code/Bizspice/Newproducttype/Model/Product/Type

<?php
namespace BizspiceNewproducttypeModelProductType;
class NewProductType extends MagentoCatalogModelProductTypeAbstractType
{
const TYPE_CODE = 'new_product_type';
 public function save($product)
 {
    parent::save($product);
    // your additional saving logic
   return $this;
 }
 public function deleteTypeSpecificData(MagentoCatalogModelProduct $product)
 { 
   //your deleting logic 
 } 
}

You can rewrite some functions and implement the changes you want there.

Step 5:

And add the price model defined in product_types.xml. Here we have to define our pricing logic as we can always add a certain amount of actual cost to the product or some other logic. For this, we have to define Price.php in app\code\Bizspice\Newproducttype\Model\Product

<?php
namespace Bizspice\Newproducttype\Model\Product;
class Price extends \Magento\Catalog\Model\Product\Price
{
public function getPrice($product) 
{ 
//add your logic here return 
//some_value; 
     } 
  } 

Now you can check the admin panel and add a product, and you will find a new type of product in the listing.