Hello everyone, This is Mo. In this blog, we are going to explain how to create a new product type in Magento 2
However, Magento 2 provides 6 product types namely:
- Simple
- Grouped
- Configurable
- Virtual
- Bundled
- Downloadable
These product types cover the majority of e-commerce needs by supporting a wide range of requirements.
Since these product types might not be suitable for all circumstances of e-commerce development but we can come up creating new custom product types.
In general, when a product class has distinct behavior or attributes, it should be represented by its custom product type. This allows the product type to have complexity and presentation, without impacting the other product types, ensuring that the default product types can continue to function as intended.
First, create a new module with the name “Mo_Tutorial”, if you are looking to create a new module kindly check our “Module creation blog”.
Once the module is created follow the following steps to create the new custom product type:
Step 1:Create a new product type
Define a custom product type in the etc/product_type.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="mo_product" label="Mo Product" modelInstance="Mo\Tutorial\Model\Product\Type\MoProductType" indexPriority="60" sortOrder="80" isQty="true"> <priceModel instance="Mo\Tutorial\Model\Product\Price"/> </type> </config>
- Name: The name you need to set for the new product type
- Label: The Label which is visible in the Magento Backend
- Model Instance: Endorse the product type’s attributes
- price Model: Endorse the Price of the new product type
- Index priority: priority index
- isQty: whether it has a quantity
- sortOrder: position number in the sort list
Step 2:Add the code NewProductType model
Each product type instance is associated with an instance of the corresponding product type model.
This model has the opportunity to modify product type behavior and attributes and is called during several product manipulation processes. In short, you can apply your custom logic in the product type model.
Create The Mo\Tutorial\Model\Product\Type\MoProductType model, which should be based on Magento\Catalog\Model\Product\Type\AbstractType
<?php namespace Mo\Tutorial\Model\Product\Type; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product\Type\AbstractType; class MoProductType extends AbstractType { const TYPE_ID = 'mo_product'; /** * @param Product $product */ public function deleteTypeSpecificData(Product $product) { // TODO: Implement deleteTypeSpecificData() method. } } ?>
The new product model is inherited from the Magento core Abstract type class
\Magento\Catalog\Model\Product\Type\AbstractType. This class has only one abstract method and it is deleteTypeSpecificData.
This specific method is called while saving a product instance if its type allows the original product type opportunity to clean up any type-specific data before the type change is finalized.
Unless the new custom product type has such type-specific data, the method can be overridden with an empty method. After this, it is possible to rewrite some functions and implement some changes you want there.
Step 3:Add the Price Model
Create The Mo\Tutorial\Model\Product\Price model. The price model should extend \Magento\Catalog\Model\Product\Type\Price. This class has no abstract methods which must be implemented but allows extending classes to interact with nearly every aspect of price calculation.
<?php namespace Mo\Tutorial\Model\Product; class Price extends \Magento\Catalog\Model\Product\Type\Price { // your code here } ?>
Step 4:Add Common Attributes (optional)
Since product attributes can be scoped to relevant product types, any core attributes which are already scoped will not apply to the new custom product type. Thus attributes that are indeed relevant will need to be associated with the new custom product type. Hence you have to create those attributes. To create those attributes create the AssociateAttributes.php file in Mo\Tutorial\Setup\Patch\Data namespace.
<?php namespace Mo\Tutorial\Setup\Patch\Data; use Magento\Catalog\Model\Product; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Mo\Tutorial\Model\Product\Type\MoProductType; class AssociateAttributes implements DataPatchInterface { /** @var ModuleDataSetupInterface */ private $moduleDataSetup; /** @var EavSetupFactory */ private $eavSetupFactory; /** * AssociateAttributes constructor. * * @param ModuleDataSetupInterface * $moduleDataSetup * @param EavSetupFactory * $eavSetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup * $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); //associate these attributes with the new product type $fieldList = [ 'price', 'special_price', 'special_from_date', 'special_to_date', 'minimal_price', 'cost', 'tier_price', 'weight', ]; // make these attributes applicable to new product type foreach ($fieldList as $field) { $applyTo = explode(',', $eavSetup->getAttribute(Product::ENTITY, $field, 'apply_to') ); if (!in_array(MoProductType::TYPE_ID, $applyTo)) { $applyTo[] = MoProductType::TYPE_ID; $eavSetup->updateAttribute( Product::ENTITY, $field, 'apply_to', implode(',', $applyTo) ); } } } /** * @return array|string[] */ public static function getDependencies() { return []; } /** * @return array|string[] */ public function getAliases() { return []; } /** * @return string */ public static function getVersion() { return '2.0.0'; } } ?>
Kudos now we have learned to create a new custom product type in Magento 2.
To Update product attributes faster check out our blog 100+ times faster to update the products in Magento 2.
Thank you for reading. Are you fascinated to know more about Magento,
What are you waiting for? Check out the other blogs in the Magento section and
follow us through Youtube and Instagram to know more about us.
0 Comments