2

I have created new product type in magento2 using https://www.classyllama.com/blog/custom-product-types-magento-2.

After the i can't see any product info in view page.. It only display the Images tab in view page..

enter image description here

product_type.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="XXX\CustomiseProductInfo\Model\Product\Type" indexPriority="60" sortOrder="80" isQty="true">
    </type>
</config>

Type.php

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
    }
}
  • Can you see any error in exception.log ? – Supravat M Dec 18 '18 at 5:14
  • main.CRITICAL: Cannot gather stats! Warning!stat(): stat failed for /var/www/html/XXX/pub/static/adminhtml/Magento/backend/en_US/mage/requirejs/mixins.js {"exception":"[object] (Magento\\Framework\\Exception\\FileSystemException(code: 0): Cannot gather stats! Warning!stat(): stat failed for /var/www/html/xxx/pub/static/adminhtml/Magento/backend/en_US/mage/requirejs/mixins.js at /var/www/html/xxx/vendor/magento/framework/Filesystem/Driver/File.php:71)"} [] – Mano M Dec 18 '18 at 5:16
  • In console...ReferenceError: Unable to process binding "if: function(){return label }" Message: label is not defined – Mano M Dec 18 '18 at 5:16
  • @ManoM can you please update your question with product_types.xml and other files which you have created. – Ramkishan Suthar Dec 18 '18 at 5:21
  • updated my post.. – Mano M Dec 18 '18 at 5:24
3

Please try below code to create custom product type.

In your etc/product_types.xml add following code

<?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 label="Customtype" modelInstance="XXX\CustomiseProductInfo\Model\Product\Type\Customtypecode" name="customtypecode">
        <priceModel instance="XXX\CustomiseProductInfo\Model\Product\Price"/>
    </type>
</config>

Create price model Model/Product/Price.php and add following code

<?php
namespace XXX\CustomiseProductInfo\Model\Product;

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

}

In your Model/Product/Type/Customtypecode.php add following code

<?php
namespace XXX\CustomiseProductInfo\Model\Product\Type;

class Customtypecode extends \Magento\Catalog\Model\Product\Type\Simple
{

    const TYPE_ID = 'customtypecode';

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

Add following code in Setup/InstallData.php

<?php    
namespace XXX\CustomiseProductInfo\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $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(\XXX\CustomiseProductInfo\Model\Product\Type\Customtypecode::TYPE_ID, $applyTo)) {
                $applyTo[] = \XXX\CustomiseProductInfo\Model\Product\Type\Customtypecode::TYPE_ID;
                $eavSetup->updateAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $field,
                    'apply_to',
                    implode(',', $applyTo)
                );
            }
        }
    }
}

Please try and let me know if any issue.

  • Still not working – Mano M Dec 18 '18 at 6:09
  • are you getting any error ? in exception log or system log ? – Aasim Goriya Dec 18 '18 at 6:11
  • After clearing cache its working fine.....thanks. – Mano M Dec 18 '18 at 6:13
  • Its neccessary to add installdata and price model for new product? – Mano M Dec 18 '18 at 6:18
  • Installdata is necessary but price model is not – Aasim Goriya Dec 18 '18 at 6:21

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.