Skip to content

Price attribute missing when we create product with our custom product type

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="BrainvireCustomProductTypeModelProductTypeCustomProduct" indexPriority="60" sortOrder="80" isQty="true">
      <priceModel instance="BrainvireCustomProductTypeModelProductTypePrice" />
   </type>
</config>

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

<?php
namespace ModuleCustomProductTypeModelProductType;

class CustomProduct extends MagentoCatalogModelProductTypeVirtual
{
    const TYPE_CODE= 'new_product_type_code';

    public function deleteTypeSpecificData(MagentoCatalogModelProduct $product)
    {
    }
}

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

<?php

namespace ModuleCustomProductTypeModelProductType;

class Price extends MagentoCatalogModelProductTypePrice
{
  
}

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

<?php

namespace BrainvireCustomProductTypeSetupPatchData;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchVersionInterface;
use BrainvireCustomProductTypeModelProductTypeCustomProduct;

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

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

    /**
     * @var MagentoCatalogModelResourceModelProductAttributeCollectionFactory
     */
    private $productAttributeCollectionFactory;

    /**
     * constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     * @param MagentoCatalogModelResourceModelProductAttributeCollectionFactory $productAttributeCollectionFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory,
        MagentoCatalogModelResourceModelProductAttributeCollectionFactory $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(
                MagentoCatalogModelProduct::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