How to programmatically update attribute parameters in Magento?

I want to update / add drop down attribute parameters in Magento, although the code is (programmatically). I found how to add attribute parameters, but how can I update the parameter values.
Example:
Assume the attribute is manufacturer. I added three options man1, man2, man3. Now, using my code, I want to change the label man1 to man11 and man2 to man22. How can i achieve this? Thank.

+1
source share
3 answers

Ok, I found a solution myself. See here for more details .

//Get the eav attribute model
$attr_model = Mage::getModel('catalog/resource_eav_attribute');

//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);

//Create an array to store the attribute data
$data = array();

//Create options array
$values = array(
    //15 is the option_id of the option in 'eav_attribute_option_value' table
    15 => array(
            0 => 'Apple'    //0 is current store id, Apple is the new label for the option
        ),
    16 => array(
            0 => 'HTC'
        ),
    17 => array(
            0 => 'Microsoft'
        ),
);

//Add the option values to the data
$data['option']['value'] = $values;

//Add data to our attribute model
$attr_model->addData($data);

//Save the updated model
try {
    $attr_model->save();
    $session = Mage::getSingleton('adminhtml/session');
    $session->addSuccess(
        Mage::helper('catalog')->__('The product attribute has been saved.'));

    /**
     * Clear translation cache because attribute labels are stored in translation
     */
    Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
    $session->setAttributeData(false);
    return;
} catch (Exception $e) {
    $session->addError($e->getMessage());
    $session->setAttributeData($data);
    return;
}
+2
source

, AttributeController, \code\core\Mage\Adminhtml\controllers\Catalog\Product\AttributeController.php, saveAction() .

0

We can update the attribute parameter (update the label for the store) programmatically using the following script

I was able to update the value of the product attribute parameter with this script

// This Function Get Attribute code and Attribute Value.

function getOptionId($atributeCode,$optionValue){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    /*$attributeRepository = $objectManager->create('\Magento\Eav\Model\AttributeRepository');
    $attribute_id = $attributeRepository->get('catalog_product', $atributeCode)->getAttributeId();*/
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $eaov = $resource->getTableName('eav_attribute_option_value');
    $eao = $resource->getTableName('eav_attribute_option');
    $ea= $resource->getTableName('eav_attribute');
    $attributeId = $connection->fetchOne("SELECT 'attribute_id' FROM $ea WHERE 'attribute_code' = '$atributeCode' AND 'entity_type_id' = '4'");
    $sql = "select * from $eao join $eaov on $eaov.option_id = $eao.option_id where $eaov.value='$optionValue' AND $eao.attribute_id='$attributeId'";
    $result = $connection->fetchRow($sql);
    return $optionId = isset($result['option_id']) ? $result['option_id']: null;
}

// This Function Get attribute OptionId with help of Attribute code and Attribute Value.

function creatOrGetId($atributeCode,$optionValue)
{
    $optionId = getOptionId($atributeCode,$optionValue);
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    if(!$optionId ){
        $attributeRepository = $objectManager->create('\Magento\Eav\Model\AttributeRepository');
        $attributeId = $attributeRepository->get('catalog_product', $atributeCode)->getAttributeId();
        $option = $objectManager->create('\Magento\Eav\Model\Entity\Attribute\Option');
        $attributeOptionLabel = $objectManager->create('\Magento\Eav\Api\Data\AttributeOptionLabelInterface');
        $attributeOptionManagement = $objectManager->create('\Magento\Eav\Api\AttributeOptionManagementInterface');
        $option->setValue($optionValue);
        $attributeOptionLabel->setStoreId(0);
        $attributeOptionLabel->setLabel($optionValue);
        $option->setLabel($attributeOptionLabel);
        $option->setStoreLabels([$attributeOptionLabel]);
        $option->setSortOrder(0);
        $option->setIsDefault(false);
        $attributeOptionManagement->add('catalog_product', $attributeId, $option);
        return $optionId = getOptionId($atributeCode,$optionValue);
    }else{
      return $optionId;
    }
}

//echo "\n================== Created Attribute Option Values =============\n";

$manufacturer = array['aa', 'bb', 'cc', 'dd']
$manufacturerId = creatOrGetId('manufacturer',$manufacturer);

//echo "\n================== Update Attribute Option Values =============\n";

$product->setManufacturer($manufacturerId); // manufacturer of product
0
source

All Articles