use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productcollection */
$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect('id');
// Convert these attributes from Text Field to Dropdown
$convertAttributes = [
'design' => 'wzor',
'department' => 'motyw',
];
echo "Found " . $productcollection->getSize() . " total products" . PHP_EOL;
foreach($productcollection as $_product)
{
$productId = $_product->getId();
/** @var \Magento\Catalog\Model\Product $product */
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$updateAttributes = array();
$wzor = trim($product->getData('design'));
if($wzorforeach !=($convertAttributes ""){
as $from => $to) {
// Get current text $_attrvalue =from $product->getResource()->getAttribute('wzor');product
$wzor if= trim($_attr$product->usesSource>getData($from)) {;
if $wzor_id($wzor != $_attr->getSource()->getOptionId($wzor"");
{
if(!$wzor_id){
// Get new attribute
getOrSetAttribute($objectManager, 'wzor',/** $wzor);
@var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $_attr */
$_attr = $product->getResource()->getAttribute('wzor'$to);
if ($_attr->usesSource();) {
$wzor_id =// Find $_attr->getSource()->getOptionId($wzor);
existing Option for this product's value
} $wzor_id = $_attr->getSource()->getOptionId($wzor);
$updateAttributes['wzor']// =if $wzor_id;
not found, get or create new Option
}
}if (!$wzor_id) {
$motyw = trim($product->getData('department'));
ifgetOrSetAttribute($motyw$objectManager, !=$to, ""$wzor){;
$_attr = $product->getResource()->getAttribute('motyw'$to);
if ($_attr->usesSource()) {
$motyw_id = $_attr->getSource()->getOptionId>usesSource($motyw);
if(!$motyw_id){
// Get new option ID
getOrSetAttribute($objectManager, 'motyw', $motyw);
$_attr$wzor_id = $product$_attr->getResource>getSource()->getAttribute>getOptionId('motyw'$wzor);
$_attr->usesSource(); }
$motyw_id =echo "Updated attribute " . $_attr->getSource>getId() . " `" . $_attr->getOptionId>getAttributeCode($motyw);
. "` for Product " . $productId . " `" }
. $product->getSku() . "`" . PHP_EOL;
$updateAttributes['motyw'] = $motyw_id; $updateAttributes[$to] = $wzor_id;
}
}
}
if(!empty($updateAttributes)){
try{
$action->updateAttributes([$productId], $updateAttributes, 0);
}
catch(Exception $e){
echo "Unbale"Unable to update product " . $productId ."; PHP_EOL;
continue;
}
}
echo "Product ".$productId." Design Pattern updated";
}
function getOrSetAttribute($objectManager, $attibutecode, $values){
$eavAttributeFactory = $objectManager->get('\Magento\Eav\Model\Entity\AttributeFactory');
$attributeOptionManagement = $objectManager->get('\Magento\Eav\Api\AttributeOptionManagementInterface');
$magentoAttribute = $eavAttributeFactory->create()->loadByCode('catalog_product', $attibutecode);
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $attributeOptionManagement->getItems(
'catalog_product',
$attributeCode
);
$attributeOptions = array($values);
$existingMagentoAttributeOptions = array();
$newOptions = array();
$counter = 0;
foreach($magentoAttributeOptions as $option) {
if (!$option->getValue()) {
continue;
}
if($option->getLabel() instanceof \Magento\Framework\Phrase) {
$label = $option->getText();
} else {
$label = $option->getLabel();
}
if($label == '') {
continue;
}
$existingMagentoAttributeOptions[] = $label;
$newOptions['value'][$option->getValue()] = array($label, $label);
$counter++;
}
foreach ($attributeOptions as $option) {
if($option == '') {
continue;
}
if(!in_array($option, $existingMagentoAttributeOptions)) {
$newOptions['value']['option_'.$counter] = array($option, $option);
}
$counter++;
}
if(count($newOptions)) {
$magentoAttribute->setOption($newOptions)->save();
}
}
Remove duplicate code by using an array of attributes to convert
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect('id');
foreach($productcollection as $_product)
{
$productId = $_product->getId();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$updateAttributes = array();
$wzor = trim($product->getData('design'));
if($wzor != ""){
$_attr = $product->getResource()->getAttribute('wzor');
if ($_attr->usesSource()) {
$wzor_id = $_attr->getSource()->getOptionId($wzor);
if(!$wzor_id){
getOrSetAttribute($objectManager, 'wzor', $wzor);
$_attr = $product->getResource()->getAttribute('wzor');
$_attr->usesSource();
$wzor_id = $_attr->getSource()->getOptionId($wzor);
}
$updateAttributes['wzor'] = $wzor_id;
}
}
$motyw = trim($product->getData('department'));
if($motyw != ""){
$_attr = $product->getResource()->getAttribute('motyw');
if ($_attr->usesSource()) {
$motyw_id = $_attr->getSource()->getOptionId($motyw);
if(!$motyw_id){
getOrSetAttribute($objectManager, 'motyw', $motyw);
$_attr = $product->getResource()->getAttribute('motyw');
$_attr->usesSource();
$motyw_id = $_attr->getSource()->getOptionId($motyw);
}
$updateAttributes['motyw'] = $motyw_id;
}
}
if(!empty($updateAttributes)){
try{
$action->updateAttributes([$productId], $updateAttributes, 0);
}
catch(Exception $e){
echo "Unbale to update product ".$productId.";
continue;
}
}
echo "Product ".$productId." Design Pattern updated";
}
function getOrSetAttribute($objectManager, $attibutecode, $values){
$eavAttributeFactory = $objectManager->get('\Magento\Eav\Model\Entity\AttributeFactory');
$attributeOptionManagement = $objectManager->get('\Magento\Eav\Api\AttributeOptionManagementInterface');
$magentoAttribute = $eavAttributeFactory->create()->loadByCode('catalog_product', $attibutecode);
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $attributeOptionManagement->getItems(
'catalog_product',
$attributeCode
);
$attributeOptions = array($values);
$existingMagentoAttributeOptions = array();
$newOptions = array();
$counter = 0;
foreach($magentoAttributeOptions as $option) {
if (!$option->getValue()) {
continue;
}
if($option->getLabel() instanceof \Magento\Framework\Phrase) {
$label = $option->getText();
} else {
$label = $option->getLabel();
}
if($label == '') {
continue;
}
$existingMagentoAttributeOptions[] = $label;
$newOptions['value'][$option->getValue()] = array($label, $label);
$counter++;
}
foreach ($attributeOptions as $option) {
if($option == '') {
continue;
}
if(!in_array($option, $existingMagentoAttributeOptions)) {
$newOptions['value']['option_'.$counter] = array($option, $option);
}
$counter++;
}
if(count($newOptions)) {
$magentoAttribute->setOption($newOptions)->save();
}
}
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productcollection */
$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect('id');
// Convert these attributes from Text Field to Dropdown
$convertAttributes = [
'design' => 'wzor',
'department' => 'motyw',
];
echo "Found " . $productcollection->getSize() . " total products" . PHP_EOL;
foreach($productcollection as $_product)
{
$productId = $_product->getId();
/** @var \Magento\Catalog\Model\Product $product */
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$updateAttributes = array();
foreach ($convertAttributes as $from => $to) {
// Get current text value from product
$wzor = trim($product->getData($from));
if ($wzor != "") {
// Get new attribute
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $_attr */
$_attr = $product->getResource()->getAttribute($to);
if ($_attr->usesSource()) {
// Find existing Option for this product's value
$wzor_id = $_attr->getSource()->getOptionId($wzor);
// if not found, get or create new Option
if (!$wzor_id) {
getOrSetAttribute($objectManager, $to, $wzor);
$_attr = $product->getResource()->getAttribute($to);
$_attr->usesSource();
// Get new option ID
$wzor_id = $_attr->getSource()->getOptionId($wzor);
}
echo "Updated attribute " . $_attr->getId() . " `" . $_attr->getAttributeCode() . "` for Product " . $productId . " `" . $product->getSku() . "`" . PHP_EOL;
$updateAttributes[$to] = $wzor_id;
}
}
}
if(!empty($updateAttributes)){
try{
$action->updateAttributes([$productId], $updateAttributes, 0);
}
catch(Exception $e){
echo "Unable to update product " . $productId . PHP_EOL;
continue;
}
}
echo "Product ".$productId." Design Pattern updated";
}
function getOrSetAttribute($objectManager, $attibutecode, $values){
$eavAttributeFactory = $objectManager->get('\Magento\Eav\Model\Entity\AttributeFactory');
$attributeOptionManagement = $objectManager->get('\Magento\Eav\Api\AttributeOptionManagementInterface');
$magentoAttribute = $eavAttributeFactory->create()->loadByCode('catalog_product', $attibutecode);
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $attributeOptionManagement->getItems(
'catalog_product',
$attributeCode
);
$attributeOptions = array($values);
$existingMagentoAttributeOptions = array();
$newOptions = array();
$counter = 0;
foreach($magentoAttributeOptions as $option) {
if (!$option->getValue()) {
continue;
}
if($option->getLabel() instanceof \Magento\Framework\Phrase) {
$label = $option->getText();
} else {
$label = $option->getLabel();
}
if($label == '') {
continue;
}
$existingMagentoAttributeOptions[] = $label;
$newOptions['value'][$option->getValue()] = array($label, $label);
$counter++;
}
foreach ($attributeOptions as $option) {
if($option == '') {
continue;
}
if(!in_array($option, $existingMagentoAttributeOptions)) {
$newOptions['value']['option_'.$counter] = array($option, $option);
}
$counter++;
}
if(count($newOptions)) {
$magentoAttribute->setOption($newOptions)->save();
}
}
You can done this programmatically...
For Magento 2.4.3 Complete code is given below...
Design (TextField) => Wzor(Dropdown)
Department(TextField) => Motyw(Dropdown)
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
$productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect('id');
foreach($productcollection as $_product)
{
$productId = $_product->getId();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$updateAttributes = array();
$wzor = trim($product->getData('design'));
if($wzor != ""){
$_attr = $product->getResource()->getAttribute('wzor');
if ($_attr->usesSource()) {
$wzor_id = $_attr->getSource()->getOptionId($wzor);
if(!$wzor_id){
getOrSetAttribute($objectManager, 'wzor', $wzor);
$_attr = $product->getResource()->getAttribute('wzor');
$_attr->usesSource();
$wzor_id = $_attr->getSource()->getOptionId($wzor);
}
$updateAttributes['wzor'] = $wzor_id;
}
}
$motyw = trim($product->getData('department'));
if($motyw != ""){
$_attr = $product->getResource()->getAttribute('motyw');
if ($_attr->usesSource()) {
$motyw_id = $_attr->getSource()->getOptionId($motyw);
if(!$motyw_id){
getOrSetAttribute($objectManager, 'motyw', $motyw);
$_attr = $product->getResource()->getAttribute('motyw');
$_attr->usesSource();
$motyw_id = $_attr->getSource()->getOptionId($motyw);
}
$updateAttributes['motyw'] = $motyw_id;
}
}
if(!empty($updateAttributes)){
try{
$action->updateAttributes([$productId], $updateAttributes, 0);
}
catch(Exception $e){
echo "Unbale to update product ".$productId.";
continue;
}
}
echo "Product ".$productId." Design Pattern updated";
}
function getOrSetAttribute($objectManager, $attibutecode, $values){
$eavAttributeFactory = $objectManager->get('\Magento\Eav\Model\Entity\AttributeFactory');
$attributeOptionManagement = $objectManager->get('\Magento\Eav\Api\AttributeOptionManagementInterface');
$magentoAttribute = $eavAttributeFactory->create()->loadByCode('catalog_product', $attibutecode);
$attributeCode = $magentoAttribute->getAttributeCode();
$magentoAttributeOptions = $attributeOptionManagement->getItems(
'catalog_product',
$attributeCode
);
$attributeOptions = array($values);
$existingMagentoAttributeOptions = array();
$newOptions = array();
$counter = 0;
foreach($magentoAttributeOptions as $option) {
if (!$option->getValue()) {
continue;
}
if($option->getLabel() instanceof \Magento\Framework\Phrase) {
$label = $option->getText();
} else {
$label = $option->getLabel();
}
if($label == '') {
continue;
}
$existingMagentoAttributeOptions[] = $label;
$newOptions['value'][$option->getValue()] = array($label, $label);
$counter++;
}
foreach ($attributeOptions as $option) {
if($option == '') {
continue;
}
if(!in_array($option, $existingMagentoAttributeOptions)) {
$newOptions['value']['option_'.$counter] = array($option, $option);
}
$counter++;
}
if(count($newOptions)) {
$magentoAttribute->setOption($newOptions)->save();
}
}
default