{"id":2852,"date":"2016-06-03T22:28:22","date_gmt":"2016-06-03T16:58:22","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=190"},"modified":"2020-03-13T16:07:31","modified_gmt":"2020-03-13T10:37:31","slug":"add-new-product-type-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-new-product-type-magento-2\/","title":{"rendered":"Add a new product type in Magento 2"},"content":{"rendered":"<p>Happy to see you guys again!<\/p>\n<p>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.<\/p>\n<ul>\n<li>Simple product<\/li>\n<li>Configurable product<\/li>\n<li>Grouped product<\/li>\n<li>Bundle product<\/li>\n<li>Downloadable product<\/li>\n<li>Virtual products<\/li>\n<\/ul>\n<p>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 &#8220;Digital Product&#8221; type with this blog post step by step.<\/p>\n<p>Lets consider module with Namespace is <strong>Hiddentechies<\/strong> and Module Name is <strong>DigitalProduct<\/strong>. Lets list down all the files require for this extension.<\/p>\n<p>1)app\/code\/Hiddentechies\/DigitalProduct\/registration.php<br \/>\n2)app\/code\/Hiddentechies\/DigitalProduct\/composer.json<br \/>\n3)app\/code\/Hiddentechies\/DigitalProduct\/etc\/module.xml<br \/>\n4)app\/code\/Hiddentechies\/DigitalProduct\/etc\/product_types.xml<br \/>\n5)app\/code\/Hiddentechies\/DigitalProduct\/Setup\/InstallData.php<br \/>\n6)app\/code\/Hiddentechies\/DigitalProduct\/Model\/Product\/Type\/Digital.php<br \/>\n7)app\/code\/Hiddentechies\/DigitalProduct\/Model\/Product\/Type\/Digital\/Price.php<\/p>\n<p>First we will create registration file at app\/code\/Hiddentechies\/DigitalProduct\/registration.php file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n    'Hiddentechies_DigitalProduct',\r\n    __DIR__\r\n);<\/pre>\n<p>Now, create a composer.json file in app\/code\/Hiddentechies\/DigitalProduct\/ folder as per below.<\/p>\n<pre class=\"lang:default decode:true \">{\r\n    \"name\": \"hiddentechies\/digitalproduct\",\r\n    \"description\": \"N\/A\",\r\n    \"require\": {\r\n        \"php\": \"~5.5.0|~5.6.0|~7.0.0\"\r\n    },\r\n    \"type\": \"magento2-module\",\r\n    \"version\": \"1.0.2\",\r\n    \"authors\": [\r\n        {\r\n            \"name\": \"Hiddentechies\",\r\n            \"email\": \"admin@hiddentechies.com\",\r\n            \"homepage\": \"https:\/\/www.hiddentechies.com\/\",\r\n            \"role\": \"Developer\"\r\n        }\r\n    ],\r\n    \"autoload\": {\r\n        \"files\": [\r\n            \"registration.php\"\r\n        ],\r\n        \"psr-4\": {\r\n            \"Hiddentechies\\\\DigitalProduct\\\\\": \"\"\r\n        }\r\n    }\r\n}<\/pre>\n<p>now create module.xml file in app\/code\/Hiddentechies\/DigitalProduct\/etc<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n \r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Hiddentechies_DigitalProduct\" setup_version=\"1.0.0\"&gt;\r\n        &lt;sequence&gt;\r\n            &lt;module name=\"Magento_Catalog\"\/&gt;\r\n        &lt;\/sequence&gt;\r\n    &lt;\/module&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>now create product_types.xml file in app\/code\/Hiddentechies\/DigitalProduct\/etc<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/Magento\/Catalog\/etc\/product_types.xsd\"&gt;\r\n    &lt;type name=\"digitalproduct\" label=\"Digital Product\" modelInstance=\"Hiddentechies\\DigitalProduct\\Model\\Product\\Type\\Digital\" indexPriority=\"15\" sortOrder=\"20\"&gt;\r\n        &lt;priceModel instance=\"Hiddentechies\\DigitalProduct\\Model\\Product\\Type\\Demo\\Price\" \/&gt;\r\n        &lt;customAttributes&gt;\r\n            &lt;attribute name=\"refundable\" value=\"true\"\/&gt;\r\n            &lt;attribute name=\"taxable\" value=\"true\"\/&gt;\r\n        &lt;\/customAttributes&gt;\r\n    &lt;\/type&gt;\r\n    &lt;composableTypes&gt;\r\n        &lt;type name=\"digitalproduct\" \/&gt;\r\n    &lt;\/composableTypes&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Create a setup file in app\/code\/Hiddentechies\/DigitalProduct\/Setup\/InstallData.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Hiddentechies\\DigitalProduct\\Setup;\r\n\r\nuse Magento\\Eav\\Setup\\EavSetup;\r\nuse Magento\\Eav\\Setup\\EavSetupFactory;\r\nuse Magento\\Framework\\Setup\\InstallDataInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\n\r\n\/**\r\n * @codeCoverageIgnore\r\n *\/\r\nclass InstallData implements InstallDataInterface {\r\n\r\n    \/**\r\n     * EAV setup factory\r\n     *\r\n     * @var EavSetupFactory\r\n     *\/\r\n    private $eavSetupFactory;\r\n\r\n    \/**\r\n     * Init\r\n     *\r\n     * @param EavSetupFactory $eavSetupFactory\r\n     *\/\r\n    public function __construct(EavSetupFactory $eavSetupFactory) {\r\n        $this-&gt;eavSetupFactory = $eavSetupFactory;\r\n    }\r\n\r\n    \/**\r\n     * {@inheritdoc}\r\n     *\/\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {\r\n        \/** @var EavSetup $eavSetup *\/\r\n        $attributes = [\r\n            'cost',\r\n        ];\r\n\r\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n\r\n        foreach ($attributes as $attributeCode) {\r\n            $relatedProductTypes = explode(\r\n                    ',', $eavSetup-&gt;getAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, $attributeCode, 'apply_to')\r\n            );\r\n            if (!in_array(\\Hiddentechies\\DigitalProduct\\Model\\Product\\Type\\Digital::TYPE_CODE, $relatedProductTypes)) {\r\n                $relatedProductTypes[] = \\Hiddentechies\\DigitalProduct\\Model\\Product\\Type\\Digital::TYPE_CODE;\r\n                $eavSetup-&gt;updateAttribute(\r\n                        \\Magento\\Catalog\\Model\\Product::ENTITY, $attributeCode, 'apply_to', implode(',', $relatedProductTypes)\r\n                );\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>Create Model file app\/code\/Hiddentechies\/DigitalProduct\/Model\/Product\/Type\/Digital.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Hiddentechies\\DigitalProduct\\Model\\Product\\Type;\r\n\r\nclass Digital extends \\Magento\\Catalog\\Model\\Product\\Type\\AbstractType {\r\n\r\n    \/**\r\n     * Product type code\r\n     *\/\r\n    const TYPE_CODE = 'digitalproduct';\r\n\r\n   public function deleteTypeSpecificData(\\Magento\\Catalog\\Model\\Product $product) {\r\n        \r\n    }\r\n\r\n}\r\n<\/pre>\n<p>Create Price Model file app\/code\/Hiddentechies\/DigitalProduct\/Model\/Product\/Type\/Digital.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Hiddentechies\\DigitalProduct\\Model\\Product\\Type\\Digital;\r\n\r\nclass Price extends \\Magento\\Catalog\\Model\\Product\\Type\\Price\r\n{\r\n\r\n    \/**\r\n     * Default action to get price of product\r\n     *\r\n     * @param Product $product\r\n     *\/\r\n    public function getPrice($product)\r\n    {\r\n        return $product-&gt;getData('cost');\r\n    }\r\n}\r\n<\/pre>\n<p>Thats it.<\/p>\n<p>Active Hiddentechies_DigitalProduct extension<\/p>\n<p>Open Command line in folder root of Magento and run commands<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento setup:upgrade<\/pre>\n<p>Now you can check new product type at backend panel by adding new product.<\/p>\n<p>If you have any questions about this Magento 2 Tutorial, please ask them in comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-new-product-type-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[200,10,38,45],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2852"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=2852"}],"version-history":[{"count":4,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2852\/revisions"}],"predecessor-version":[{"id":10516,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2852\/revisions\/10516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4869"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=2852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=2852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=2852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}