How To

How to Set Product Custom Attribute Set as Default in Magento 2?

Hello Magento Friends,

In today’s blog, I will explain How to Set Product Custom Attribute Set as Default in Magento 2.

In Default Magento 2, the “Default” option is selected for the attribute set field when you create a new product. Checkout below image

If you want to change the default selection and set the “Custom attribute set” as the default selection in the Attribute Set field, you can use the below method. Before that learn

How to Create New Attribute Sets in Magento 2

Let’s get started,

Steps to Set Product Custom Attribute Set as Default in Magento 2:

Step 1: Create di.xml at the below path

app\code\Vendor\Extension\etc\adminhtml

And add the following code

<?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\Ui\DataProvider\Product\Form\Modifier\AttributeSet" type="Vendor\Extension\Ui\DataProvider\Product\Form\Modifier\AttributeSet"></preference>
</config>

Step 2: Create AttributeSet.php in the following path

Vendor\Extension\Ui\DataProvider\Product\Form\Modifier

And add the code as follows

<?php

namespace Vendor\Extension\Ui\DataProvider\Product\Form\Modifier;

class AttributeSet extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AttributeSet
{
    public function modifyData(array $data)
    {
 // 4 is default attribute set id in my case
        if($this->locator->getProduct()->getAttributeSetId() ==4){ 
 // Replace it with your custom attribute set id whatever you want to set
            $attributesetId=10; 
        }else{
          //This is for already created product we are getting previously saved value 
            $attributesetId=$this->locator->getProduct()->getAttributeSetId(); 
        }

        return array_replace_recursive(
            $data,
            [
                $this->locator->getProduct()->getId() => [
                    self::DATA_SOURCE_DEFAULT => [
                        'attribute_set_id' => $attributesetId
                    ],
                ]
            ]
        );
    }
}

Result:

 

Conclusion:

This way, you can set the product custom attribute set as default in Magento 2. If you are unable to set the custom attribute set as default, let me know through the comment section. Share the solution with your friends and stay in touch with us.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • I have the same problem as Denis - it does set the attribute set, but it does not print the additional attributes from that set - you have to use the Attribute Set dropdown, and select the attribute set again, and then the custom attributes show up. Not really helpful like this.

  • so this does correctly change the default attribute set in the product creation form. However, it does not pull the ajax fields of the new custom attribute set. You have to again toggle the drop down from custom attribute set to default to custom attribute set to see the fields.

    How to fix this?

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago