Magento2: How to add a new product type
-
22-02-2021 - |
Question
This new product type will contain parent informations of other products. These products are only for internal use, so I only need to add attributes and a new type of relation to other products, like Up-Sells, Cross-Sells. All other default informations like prices, description, etc. are not needed.
What is the best way to create this product type.
Christian
Solution
Step 1: Generate the
app\code\Vendor\Module\registration.php
file
Setup the app\code\Vendor\Module\registration.php
file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Generate the app\code\Vendor\Module\etc\module.xml
<?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="Vendor_Module" setup_version="1.0.0">
</module>
</config>
Next, the creation of the etc/product_types.xml
file is necessary to determine the model of the new product type.
<?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="Vendor\Module\Model\Product\Type\NewProductType" indexPriority="60" sortOrder="80" isQty="true">
<priceModel instance="Vendor\Module\Model\Product\Price" />
</type>
</config>
Step 2: Enter the code :
Vendor\Module\Model\Product\Type\NewProductType
model
Enter the code below: Vendor\Module\Model\Product\Type\NewProductType
model, that should be based on Magento\Catalog\Model\Product\Type\AbstractType
.
<?php
namespace Vendor\Module\Model\Product\Type;
class NewProductType extends \Magento\Catalog\Model\Product\Type\AbstractType
{
}
After that, it is possible to rewrite some functions and implement some changes you want there.
Step 3:
Enter the Vendor\Module\Model\Product\Price
model
Enter Vendor\Module\Model\Product\Price model
, which should be based on the Magento\Catalog\Model\Product\Type\Price
.
<?php
namespace Vendor\Module\Model\Product;
class Price extends \Magento\Catalog\Model\Product\Type\Price
{
}
Besides, you can also set the new product type as versatile type with some custom functions after extending the Magento\Catalog\Model\Product\Type\Price class
Step 4: Public the new Magento 2 Product type
This is the step allowing you to check the result in the Magento 2 Administrator when completing the above steps. The New Product type will display as the old types (Simple or Configurable Product).
I hope this will help