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


Hello magento friends todays in this blog post im share with you to How to Set Product Custom Attribute Set as Default in Magento 2. In Magento 2, you can set a custom attribute set as the default attribute set for a specific product type (e.g., simple, configurable, etc.). Follow these steps to achieve this:


Please note that modifying core Magento files directly is not recommended as it may cause issues during future updates or conflict with other customizations. Instead, create a custom module or use existing extensions to achieve this functionality.


Here's a general outline of how you can set a product custom attribute set as default:


➤ Create a custom module (if you don't have one already):


  • Create the necessary directory structure: app/code/Vendor/Module
  • Create registration.php in app/code/Vendor/Module with appropriate registration information.
  • Create etc/module.xml in app/code/Vendor/Module to enable your custom module.


➤ Create a custom InstallData script to set the default attribute set for your product type:


  • Create a file named InstallData.php under app/code/Vendor/Module/Setup
  • Use the following code as a starting point:

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

➤ Use this Code :- 

<?php
namespace Vendor\Module\Setup;

use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;

class InstallData implements InstallDataInterface
{
    private $categorySetupFactory;

    public function __construct(CategorySetupFactory $categorySetupFactory)
    {
        $this->categorySetupFactory = $categorySetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

        // Change 'default' to the desired product type (e.g., 'simple', 'configurable', etc.)
        $productType = 'default';

        // Replace 'custom_attribute_set_code' with the code of your custom attribute set
        $customAttributeSetCode = 'custom_attribute_set_code';

        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);

        // Get the ID of the custom attribute set
        $attributeSetId = $categorySetup->getAttributeSetId($entityTypeId, $customAttributeSetCode);

        // Set the default attribute set for the product type
        $categorySetup->setDefaultAttributeSet($entityTypeId, $attributeSetId, $productType);

        $setup->endSetup();
    }
}


➤ Run the necessary Magento setup commands to apply the changes:


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






Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post