If you want to create product attribute options programatically, but not by admin then this blog post is right the solution for you.
You need to add below code in controller.
Codes for 2.2.x & 2.3.x will be different.
Code for Magento 2.2.X :
$optionLabel = $this->optionLabelFactory->create();
$optionLabel->setStoreId(0);
$optionLabel->setLabel($attoption);
$option = $this->optionFactory->create();
$option->setLabel($optionLabel);
$option->setStoreLabels([$optionLabel]);
$option->setSortOrder(0);
$option->setIsDefault(false);
$this->attributeOptionManagement->add(MagentoCatalogModelProduct::ENTITY,
$this->attributeRepository->get($attrname)->getAttributeId(),$option);
$_product = $this->productFactory->create();
$isAttributeExist = $_product->getResource()->getAttribute($attrname);
$optionId = '';
if ($isAttributeExist && $isAttributeExist->usesSource()) {
$optionId = $isAttributeExist->getSource()->getOptionId($attoption);
}
return $optionId;
Code for Magento 2.3.X
$optionLabel = $this->optionLabelFactory->create();
$optionLabel->setStoreId(0);
$optionLabel->setLabel($attoption);
$option = $this->optionFactory->create();
$option->setLabel($attoption);
$option->setStoreLabels([$optionLabel]);
$option->setSortOrder(0);
$option->setIsDefault(false);
$this->attributeOptionManagement->add(MagentoCatalogModelProduct::ENTITY,
$this->attributeRepository->get($attrname)->getAttributeId(),$option);
$_product = $this->productFactory->create();
$isAttributeExist = $_product->getResource()->getAttribute($attrname);
$optionId = '';
if ($isAttributeExist && $isAttributeExist->usesSource()) {
$optionId = $isAttributeExist->getSource()->getOptionId($attoption);
}
return $optionId;
I hope adding the above codes in their respective versions of your Magento store will help you to create product attribute options. Let us know by comment if it worked for you or not?

