Happy to see you guys again!
In this Magento 2 Tutorial, we will learn how to Add a new product type in Magento 2 step by step using one real extension. As you know, Magento 2 store comes with six different product types.
- Simple product
- Configurable product
- Grouped product
- Bundle product
- Downloadable product
- Virtual products
These are all product types available in Magento Community out of the box. In this blog post we will learn how to create custom product type in Magento 2 using extension. We will create “Digital Product” type with this blog post step by step.
Lets consider module with Namespace is Hiddentechies and Module Name is DigitalProduct. Lets list down all the files require for this extension.
1)app/code/Hiddentechies/DigitalProduct/registration.php
2)app/code/Hiddentechies/DigitalProduct/composer.json
3)app/code/Hiddentechies/DigitalProduct/etc/module.xml
4)app/code/Hiddentechies/DigitalProduct/etc/product_types.xml
5)app/code/Hiddentechies/DigitalProduct/Setup/InstallData.php
6)app/code/Hiddentechies/DigitalProduct/Model/Product/Type/Digital.php
7)app/code/Hiddentechies/DigitalProduct/Model/Product/Type/Digital/Price.php
First we will create registration file at app/code/Hiddentechies/DigitalProduct/registration.php file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Hiddentechies_DigitalProduct',
__DIR__
);Now, create a composer.json file in app/code/Hiddentechies/DigitalProduct/ folder as per below.
{
"name": "hiddentechies/digitalproduct",
"description": "N/A",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.2",
"authors": [
{
"name": "Hiddentechies",
"email": "[email protected]",
"homepage": "https://www.hiddentechies.com/",
"role": "Developer"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Hiddentechies\\DigitalProduct\\": ""
}
}
}now create module.xml file in app/code/Hiddentechies/DigitalProduct/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hiddentechies_DigitalProduct" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>now create product_types.xml file in app/code/Hiddentechies/DigitalProduct/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Magento/Catalog/etc/product_types.xsd">
<type name="digitalproduct" label="Digital Product" modelInstance="Hiddentechies\DigitalProduct\Model\Product\Type\Digital" indexPriority="15" sortOrder="20">
<priceModel instance="Hiddentechies\DigitalProduct\Model\Product\Type\Demo\Price" />
<customAttributes>
<attribute name="refundable" value="true"/>
<attribute name="taxable" value="true"/>
</customAttributes>
</type>
<composableTypes>
<type name="digitalproduct" />
</composableTypes>
</config>Create a setup file in app/code/Hiddentechies/DigitalProduct/Setup/InstallData.php
<?php
namespace Hiddentechies\DigitalProduct\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface {
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory) {
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
/** @var EavSetup $eavSetup */
$attributes = [
'cost',
];
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
foreach ($attributes as $attributeCode) {
$relatedProductTypes = explode(
',', $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, 'apply_to')
);
if (!in_array(\Hiddentechies\DigitalProduct\Model\Product\Type\Digital::TYPE_CODE, $relatedProductTypes)) {
$relatedProductTypes[] = \Hiddentechies\DigitalProduct\Model\Product\Type\Digital::TYPE_CODE;
$eavSetup->updateAttribute(
\Magento\Catalog\Model\Product::ENTITY, $attributeCode, 'apply_to', implode(',', $relatedProductTypes)
);
}
}
}
}Create Model file app/code/Hiddentechies/DigitalProduct/Model/Product/Type/Digital.php
<?php
namespace Hiddentechies\DigitalProduct\Model\Product\Type;
class Digital extends \Magento\Catalog\Model\Product\Type\AbstractType {
/**
* Product type code
*/
const TYPE_CODE = 'digitalproduct';
public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) {
}
}
Create Price Model file app/code/Hiddentechies/DigitalProduct/Model/Product/Type/Digital.php
<?php
namespace Hiddentechies\DigitalProduct\Model\Product\Type\Digital;
class Price extends \Magento\Catalog\Model\Product\Type\Price
{
/**
* Default action to get price of product
*
* @param Product $product
*/
public function getPrice($product)
{
return $product->getData('cost');
}
}
Thats it.
Active Hiddentechies_DigitalProduct extension
Open Command line in folder root of Magento and run commands
php bin/magento setup:upgrade
Now you can check new product type at backend panel by adding new product.
If you have any questions about this Magento 2 Tutorial, please ask them in comments.


May 7, 2020 at 2:36 pm
Nice article… Thanks for sharing the information. Your article very useful.