Magento setup not saving client attribute parameter values?
I would like to programmatically change the values ββof the client address attribute parameter, so I created a file and searched it and came up with this solution:
<?php
require_once 'app/Mage.php';
Mage::app();
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'geslacht');
$data['option']['value'] = array(
8649 => array(
0 => 'De heer',
1 => 'heer',
2 => 'heer'
),
8648 => array(
0 => 'Mevrouw',
1 => 'mevrouw',
2 => 'mevrouw'
)
);
$data['store_labels'] = array(
0 => 'Geslacht',
1 => 'Aanhef',
2 => 'Aanhef'
);
$attribute->addData($data)->save();
And it works great! But ... Now I would like to use this in the setup file for the module I am creating, so I copied it to the setup file and came up with this:
<?php
$installer = $this;
$installer->startSetup();
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'geslacht');
$data['option']['value'] = array(
8649 => array(
0 => 'De heer',
1 => 'heer',
2 => 'heer'
),
8648 => array(
0 => 'Mevrouw',
1 => 'mevrouw',
2 => 'mevrouw'
)
);
$data['store_labels'] = array(
0 => 'Geslacht',
1 => 'Aanhef',
2 => 'Aanhef'
);
$attribute->addData($data)->save();
$installer->endSetup();
Now only store labels are saved and all parameter values ββare cleared! Why can't I use this code in the setup file?
source to share
Ok, I'm not sure if this will work, but this seems to be a possible explanation. Perhaps the problem is caused by the environment not fully initialized when installing scripts running Magento. The solution might be to switch the script to a data install script that will run after the environment is initialized.
The naming conventions for these files are basically identical to sql scripts, but they are in a separate folder.
This is not a topic here, so I will post a link only on how to create scripts to install data: http://inchoo.net/magento/using-magento-1-6-data-install-scripts/
I also found a very helpful article: http://vinaikopp.com/2014/11/03/magento-setup-scripts/
Good luck!
source to share
You cannot update parameter values ββto an attribute after it has been created. When you first call addAttribute, it detects an array of parameter values ββand automatically populates the eav_config_option and eav_config_option_value tables with the required information. You can see how the install script does it (primarily the addAttribute and addAttributeOption functions): http://freegento.com/doc/d0/d7b/_eav_2_model_2_entity_2_setup_8php-source.html
I recently had to do something like this (add a bunch of parameter values ββto a custom product attribute), so this might be useful as a guide based on the process in the kernel file I linked above.
Good luck!
source to share
There is a customization function for updating attribute attribute data called addAttributeOption. Its name starts with "add", but it also does the update. There is no customization function to update the store labels for an attribute, but luckily your code works for that purpose. So this code might work for your example;
<?php
$installer = $this;
$installer->startSetup();
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'geslacht');
$attributeData['store_labels'] = array(
0 => 'Geslacht',
1 => 'Aanhef',
2 => 'Aanhef'
);
$attribute->addData($attributeData)->save();
$optionData['option']['attribute_id'] = $attribute->getId();
$optionData['option']['value'] = array(
8649 => array(
0 => 'De heer',
1 => 'heer',
2 => 'heer'
),
8648 => array(
0 => 'Mevrouw',
1 => 'mevrouw',
2 => 'mevrouw'
)
);
$installer->addAttributeOption($optionData); //Function is named "addAttributeOption", but it makes update too.
$installer->endSetup();
source to share
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 using 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
source to share
Try something like this
$installer->addAttribute('customer_address', 'geslacht', array(
'label' => 'De heer',
'type' => 'varchar'
));
$installer->addAttribute('customer_address', 'geslacht', array(
'label' => 'heer',
'type' => 'varchar'
));
// and so on
$installer->endSetup();
NTN
Luck
source to share