2

I am tring to create a new product but I do not get the price enabled on the admin panel.

I create a product_types.xml with this structure:

<?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="my_product" label="My Product" modelInstance="My\Module\Model\Product\Type\MyProduct" composite="false" isQty="true" canUseQtyDecimals="false" indexPriority="30" sortOrder="20">
        <customAttributes>
            <attribute name="is_real_product" value="false"/>
            <attribute name="refundable" value="true"/>
        </customAttributes>
    </type>
</config>

Then I create the model as My\Module\Model\Product\Type\MyProduct like this:

<?php

namespace My\Module\Model\Product\Type;

use \Magento\Catalog\Model\Product\Type\Virtual;

class MyProduct extends Virtual
{
    /**
     * Product type code
     */
    const TYPE_CODE = 'my_product';
}

When I try to create a new product in the admin panel I do not get the option to input a price. What Am I missing? By the way I have tested this model with AbstractType and Simple Product too.

3

Ok, I found what was missing. You need to set price and other attributes to any new prodcut_type. You add so by adding this to your InstallData.php file:

  if (version_compare($version, '0.0.3', '<')) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $fieldList = [
            'price',
            'special_price',
            'special_from_date',
            'special_to_date',
            'minimal_price',
            'cost',
            'tier_price',
        ];

        // make these attributes applicable to downloadable products
        foreach ($fieldList as $field) {
            $applyTo = explode(
                ',',
                $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
            );
            if (!in_array('my_product', $applyTo)) {
                $applyTo[] = 'my_product';
                $eavSetup->updateAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $field,
                    'apply_to',
                    implode(',', $applyTo)
                );
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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