1

How can I convert a product attribute type from Text to Dropdown in Magento?

I already have attribute values assigned to many products, so don't want to lose these in the process.

4
  • Use Select type
    – fmsthird
    Feb 4 '19 at 0:43
  • How and where do I choose "Select" type?
    – John Doe
    Feb 4 '19 at 16:41
  • Did you resolve your issue? Feb 6 '19 at 9:57
  • No. It's not that easy. As the option is greyed out. Changing it also implies a lot of database changes. So I've yet to find the solution...
    – John Doe
    Feb 7 '19 at 12:47
3

Magento Won't allow you to change input type of an attribute, and if you somehow change it from database, values of attributes would still not work for you, as Text stores just a value whereas dropdown stores id of each option's value

Instead of working on changing attribute type you can do this:

  • You need to export the data from magento exporter. Like, sku, your_attribute

  • Delete existing attribute

  • Create new one with the same attribute code your_attribute

  • Import the data that you have just exported

1
  • Doesn't this interfere with native magento functions such as ->setColor(
    – John Doe
    Feb 4 '19 at 16:41
0

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();
    }
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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