1

I have to make new Product Type similar like Configurable and I have tried the below reference which is working but I cannot add/update Product Price and Qty. Qty all the time becomes Zero, also Configurations is missing similar like Configurable Products.

Reference : Magento2: How to add a new product type

Github : https://github.com/Genmato/DemoProduct

How to fix it?

2
  • Why would you need to create a new product type? Are you sure you can't fit your needings just extending some functions of existing product types? Feb 19, 2020 at 7:52
  • As per client requirements need to make new product type and need to customize. Feb 19, 2020 at 8:23

1 Answer 1

-1

Vendor/Module/etc/product_types.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="custom_product_type_code" label="Custom Product Type Label" modelInstance="Vendor\Module\Model\Product\Type">
    </type>
</config>

A product model must inherit from the \Magento\Catalog\Model\Product\Type\AbstractType base class. TYPE_CODE which defines the product type code string, so it can be referenced in code in a consistent manner.

Vendor\Module\Model\Product\Type.php

<?php

namespace Vendor\Module\Model\Product;

class Type extends \Magento\Catalog\Model\Product\Type\AbstractType
{
    const TYPE_ID = 'custom_product_type_code';

    /**
     * {@inheritdoc}
     */
    public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
    {
        // method intentionally empty
    }
}

Vendor\Module\Setup

<?php

namespace Vendor\Module\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;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        //associate these attributes with 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(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
            );
            if (!in_array(\Vendor\Module\Model\Product\Type::TYPE_ID, $applyTo)) {
                $applyTo[] = \Vendor\Module\Model\Product\Type::TYPE_ID;
                $eavSetup->updateAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $field,
                    'apply_to',
                    implode(',', $applyTo)
                );
            }
        }
    }
}

New product types can allow themselves to be children of composite product types by adding a node to the composableTypes node in

Vendor/Module/etc/product_types.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="custom_product_type_code" label="Custom Product Type Label" modelInstance="Vendor\Module\Model\Product\Type">
    </type>
    <composableTypes>
        <type name="custom_product_type_code" />
    </composableTypes>
</config>

In addition to the product type model, a module can specify a price model in the product type definition 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="custom_product_type_code" label="Custom Product Type Label" modelInstance="Vendor\Module\Model\Product\Type">
    <priceModel instance="Vendor\Module\Model\Product\Price" />
    </type>
</config>
1
  • It's working in Simple Product not in Configurable Product. Feb 23, 2020 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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