1

Module/CustomProductType/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" label="Custom Product Type" modelInstance="Brainvire\CustomProductType\Model\Product\Type\CustomProduct" indexPriority="60" sortOrder="80" isQty="true">
      <priceModel instance="Brainvire\CustomProductType\Model\Product\Type\Price" />
   </type>
</config>

Module/CustomProductType/Model/Product/Type/CustomProduct.php

<?php
namespace Module\CustomProductType\Model\Product\Type;

class CustomProduct extends \Magento\Catalog\Model\Product\Type\Virtual
{
    const TYPE_CODE= 'new_product_type_code';

    public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
    {
    }
}

Module/CustomProductType/Model/Product/Type/Price.php

<?php

namespace Module\CustomProductType\Model\Product\Type;

class Price extends \Magento\Catalog\Model\Product\Type\Price
{
  
}

Module/CustomProductType/Setup/Patch/Data/InstallData.php

<?php

namespace Brainvire\CustomProductType\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Brainvire\CustomProductType\Model\Product\Type\CustomProduct;

class InstallData implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
     */
    private $productAttributeCollectionFactory;

    /**
     * constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttributeCollectionFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory,
        \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttributeCollectionFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->productAttributeCollectionFactory = $productAttributeCollectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->startSetup();

        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $collection = $this->productAttributeCollectionFactory->create();
        $collection->addFieldToFilter('apply_to', ['like' => '%virtual%']);
        foreach ($collection as $attribute) {
            $applyTo = $attribute->getApplyTo();
            if (!in_array(CustomProduct::TYPE_CODE, $applyTo)) {
                $applyTo[] = CustomProduct::TYPE_CODE;
            }
            $eavSetup->updateAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                $attribute->getAttributeCode(),
                'apply_to',
                implode(',', $applyTo)
            );
        }

        $this->moduleDataSetup->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '1.0.0';
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

enter image description here

1
  • Please describe in detail what you need to actually? Feb 22 at 13:41

0

Your Answer

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

Browse other questions tagged or ask your own question.