Magento 2 – Get Last Added Attribute Option ID

magento2product-attribute

I am adding options to attribute programmatically as explained here http://webkul.com/blog/programmatically-add-options-attribute-magento2/.
What i want is to get the id of that attribute option to save it to my custom db table. How to achieve this?
thanks

Best Answer

At magento2, If you want a options then follow Marius answer https://magento.stackexchange.com/a/105171/4564

$options = $attribute->getSource()->getAllOptions();

give list of options with value and label

$OptionInarray=array();
foreach ($options as $option) {
    $options->getValue();  // Value
    $option->getLabel();  // Label
   $OptionInarray[$options->getValue()] = $option->getLabel();
}

here, $options->getValue(); is give u list option id of this attribute

Then using krsort() you can get last order id fro $OptionInarray

krsort($OptionInarray);..Then array_values(krsort($OptionInarray))[0]; , we can get last id

Related Topic