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 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.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bizspice_Newproducttype', __DIR__ );
<?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>
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
Here we have added one more model to define the charge of a 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.
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.