MageStackDay bonus question for 500pts Bounty AND a the possibility of winning a free Z-Ray license for a year. More info can be found >> here <<

The questions are provided by Magento 2 core developer Anton Kril.

Question:

I want to add a new product type to Magento. How would I go about doing this in Magento 2 dev beta

up vote 37 down vote accepted
+500

For adding a new product type in Magento 2 you need to create a etc/product_types.xml in your module. In this file you specify:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Catalog/etc/product_types.xsd">
    <type name="demoproduct" label="Demo Product" modelInstance="Genmato\DemoProduct\Model\Product\Type\Demo" indexPriority="25" sortOrder="25">
        <customAttributes>
            <attribute name="refundable" value="true"/>
        </customAttributes>
    </type>
</config>

Then create the modelInstance:

/**
 * @category    Genmato
 * @package     Genmato_MageStackProduct
 * @copyright   Copyright (c) 2015 Genmato BV (https://genmato.com)
 */

namespace Genmato\DemoProduct\Model\Product\Type;

class Demo extends \Magento\Catalog\Model\Product\Type\AbstractType
{
    /**
     * Delete data specific for Simple product type
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return void
     */
    public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
    {
    }
}

This will add the new product type and now you can select this when creating a new product in the backend.

Demo Product option

In the product_type.xml it is also possible to specify your own indexer or price calculation method, for more examples see the code for the product types Bundle, ConfigureProduct, Downloadable and GroupedProduct.

For the complete demo product extension see: https://github.com/Genmato/DemoProduct

EDIT:

As requested by Anton a little bit extra functionality for the new product type (If I have a little bit more time this week I will try to at some extra modifications).

For now I updated the Demo Product type with a price input field for the cost attribute: Cost price attribute

This cost attribute is used to calculate the price when it is displayed in the frontend (price attribute is not available and isn't used). For this example I used cost*1.25 (in Genmato\DemoProduct\Model\Product\Type\Demo\Price): Price calculation

The result on the frontend: Frontend result

The complete updated code is available on: https://github.com/Genmato/DemoProduct

  • Just adding a tutorial to your answer, hope you get the bounty! There is a full tutorial on creating new product types located : vimeo.com/116810487 View the documentation here: devdocs.magento.com – JoeyH Feb 13 '15 at 18:23
  • 4
    It would be great if you added at least few customization points (rendering, price calculation, etc) – Anton Kril Feb 16 '15 at 9:39
  • 1
    @Anton: I updated the Demo Product type with an simple price calculation based on the cost attribute. If I have a bit more time the next few days I will try to add some other modifications. – Vladimir Kerkhoff Feb 16 '15 at 13:17
  • How to extend this to create a composite product type such as grouped product? – Sukeshini Sep 9 '16 at 10:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.