July 12, 2023

Magento 2 : How to add custom product type

Developer Guide on How to add custom product type in Magento 2 Stores

Magento 2, renowned for its flexibility and extensibility, offers default support for six product types. However, did you know that Magento 2 empowers you to go beyond the defaults and create your very own custom product type?

In our latest blog, we delve into the fascinating world of Magento 2 customization, walking you through the process of creating a new product type from scratch. This comprehensive step-by-step guide equips you with the knowledge and expertise to bring your unique product concepts to life within the Magento 2 ecosystem.

Create product_types.xml

The first step is defining a custom product type with an XML declaration. Specifically, to be placed in Webficial/CustomProductType/etc/product_types.xml

				
					<?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="webficial_custom_product" label="Webficial Custom Product" modelInstance="Webficial\CustomProductType\Model\Product\Type\CustomProductType" indexPriority="100" sortOrder="10" isQty="true">
		<priceModel instance="Webficial\CustomProductType\Model\Product\Price" />
	</type>
	<composableTypes>
		<type name="generic" />
	</composableTypes>
</config>

				
			

Create CustomProductType.php

After defining product type in ‘product_types.xml’, we need to create model for product type.
Now, you have to add the code CustomProductType model. Create CustomProductType.php at Webficial/CustomProductType/Model/Product/Type

				
					<?php

namespace Webficial\CustomProductType\Model\Product\Type;

class CustomProductType extends \Magento\Catalog\Model\Product\Type\AbstractType
{
	const TYPE_ID = "webficial_custom_product";
 
	public function save($product)
	{
		parent::save($product);
		//  your additional saving logic
		return $this;
	}
 
	public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
	{
		//your deleting logic 
	}
}

				
			

Add the Price model

By leveraging the Config XML, you have the ability to implement intricate pricing calculations and rules that go beyond the standard cost structure. Whether it’s adding a fixed amount to the base cost or implementing a complex pricing algorithm, Magento 2 offers the flexibility to accommodate your specific pricing needs.

Create Price.php at Webficial/CustomProductType/Model/Product

				
					<?php

namespace Webficial\CustomProductType\Model\Product;

class Price extends \Magento\Catalog\Model\Product\Type\Price
{   
  // your custom code
}

				
			

Output

That’s all. Now if you go to your admin panel and click on “Add Product”, you will see your newly created product type. Select the product type and you can add products of your custom type.

 

Magento 2 Custom product type

Conclusion

Unlock the full potential of Magento 2 and unleash your creativity by following our comprehensive guide. Let us empower you to shape your own product types and revolutionize your e-commerce offerings. Whether you want to offer custom bundles, unique digital products, or any other specialized offerings, Magento 2 provides the flexibility and tools to make it happen.

Author

Share this post:
Facebook
Twitter
LinkedIn
WhatsApp

Discover more articles