Create a new product type in Magento 2

Create a new product type in Magento 2

How to Create/build New Product Type in Magento 2

Magento has several inbuilt product types which have its own unique behavior, attribute 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 yours 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 a good 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.

 

Step 1: Create registration.php in app/code/Bizspice/Newproducttype

 


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

 

Step 2: 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> 

 

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

Here we will declare critical information about new product type 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, will be used in the Magento admin
  • modelInstance: this is the namespace i.e. fully qualified namespace name for the product type model

Here we have added one more model to define charge of 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 some changes you want there like we did.

 

Step 5: And add price model defined in product_types.xml. Here we have to define our pricing logic like we can add certain amount always of actual cost to 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 admin and add product and you will find an new type of product in listing.