0

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.

3 Answers 3

0

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();
1
  • I have done this for configurable product but it is not working
    – Jaisa
    Jun 6, 2018 at 10:56
0

Try to use Product Factory and save with Resource Model. In the example below, the $productData argument contains the main product data. This data came from the product collection. I walked through the collection with the iterator \Magento\Framework\Model\ResourceModel\Iterator::walk and used as a callback a method similar to changeAttributeSet($productData,$newAttributeSetId). I changed some points to make the example simpler.

/** @var \Magento\Catalog\Model\ProductFactory */
private $productFactory;

/** @var \Magento\Catalog\Model\ResourceModel\Product */
private $productResourceModel;

...

public function changeAttributeSet($productData,$newAttributeSetId)
{
    $product = $this->productFactory->create();
    $product->setData($productData);
    $product->setData('attribute_set_id',$newAttributeSetId);
    try {
        $this->productResourceModel->save($product);
    } catch (\Exception $e) {
        //
    }
}
0

Try this code

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Eav\Api\AttributeSetRepositoryInterface;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);

// Load the product by ID
$productId = 1; // Replace with your product ID
$product = $productRepository->getById($productId);

// Set the new attribute set name
$newAttributeSetName = 'set2'; // Replace with your new attribute set name

// Get the new attribute set ID
$attributeSet = $attributeSetRepository->get($newAttributeSetName);
$newAttributeSetId = $attributeSet->getAttributeSetId();

// Update the product's attribute set ID
$product->setAttributeSetId($newAttributeSetId);

// Save the product
$productRepository->save($product);

echo "Product attribute set updated successfully.\n";

Make sure to replace 1 with the actual ID of your product and 'set2' with the name of the new attribute set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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