cancel
Showing results for 
Search instead for 
Did you mean: 

Default Attribute set for custom product type

Default Attribute set for custom product type

Hi,

 

Is there a way to define the default attribute set a custom product type should use ? 

 

I created my custom attributes which I made part of a new attribute set, I created my custom Product type and I'd like to have my custom attribute set selected by default when I choose to create a new product of my custom product type.

 

Or should I use the 'apply_to' field in my InstallData/UpgradeData script, as I do for the general fields:

 

$fieldList = [
            'price',
            'special_from_date',
            'special_to_date',
            'cost',
            'tier_price'
        ];
        $type = \Ltc\ProductVisite\Model\Product\Type\Visite::TYPE_VISITE;
        foreach ($fieldList as $field) {
            $applyTo = explode(
                ',',
                $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
            );
            if (!in_array($type, $applyTo)) {
                $applyTo[] = $type;
                $eavSetup->updateAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $field,
                    'apply_to',
                    implode(',', $applyTo)
                );
            }
        }

Any idea would help Smiley Happy

 

Thanks

2 REPLIES 2

Re: Default Attribute set for custom product type

I have the same question. 

Re: Default Attribute set for custom product type

And I found one solution.

Override the \Magento\Catalog\Block\Adminhtml\Product::_getProductCreateUrl($type) with di.xml. So I used in my modules etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    	<preference for="Magento\Catalog\Block\Adminhtml\Product" type="VendorName\ModuleName\Block\Adminhtml\Product" />
</config>

Then created Block/Adminhtml/Product.php in my module with:

<?php

namespace VendorName\ModuelName\Block\Adminhtml;


class Product extends \Magento\Catalog\Block\Adminhtml\Product
{
    
    /**
     * Retrieve product create url by specified product type
     * Override parent so we use the attribute set we want for our custom product
     * @param string $type
     * @return string
     */
    protected function _getProductCreateUrl($type)
    {
        if ($type === \VendorName\ModuleName\Model\Product\Type::TYPE_ID) {
            $attributeSetId = 10;  // TODO get id based on name in db
        } else {
            $attributeSetId = $this->_productFactory->create()->getDefaultAttributeSetId();
        }
        
        return $this->getUrl(
            'catalog/*/new',
            ['set' => $attributeSetId, 'type' => $type]
        );
    }
}

 

Not sure if it's the best solution but it seems the only way to do it right now. Hopefully Magento will get a way to set the default attribute set on a per product type basis in the future.