Magento – How to change the attribute set of the product via programatically in magento 2

attribute-setmagento2.2.2

I have created custom attributes for the attribute sets namely set1 and set2.

I have created the products via script with attribute set name as set1.
The products are saved successfully.

But Now I wish to change the attributeset name from set1 to set2.

Please provide me a solution how do i update the attribute set for the existing products while saving the product via programatically.

Best Answer

Try this code:

$attributeSetId = 12; //you can change as per your requirement.
$product_id = 10;
$storeId = YOUR STORE ID;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
$product->setAttributeSetId($attributeSetId)->setStoreId($storeId)->save();
Related Topic