Prepare the attribute options data: Create an array of attribute options data that you want to import. Each option should include the necessary information such as the option label, swatch value (for visual swatch), and any other relevant details. For example:
php Copy code $attributeOptions = [ [ ‘label’ => ‘Red’, ‘value’ => ‘red’, ‘swatch_value’ => ‘#FF0000’, // Swatch value for visual swatch ‘store_labels’ => [ ‘1’ => ‘Red’, //Store-specific labels for text swatch ‘2’ => ‘Rouge’, ], ], // Add more attribute options as needed ]; Load the attribute by code: Load the attribute object using the attribute code for which you want to import options.
Code Below helps to achieve the task:
namespace Vendor\Module\Model;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
class ColorImport
{
    private AttributeFactory $attributeFactory;
    
    public function __construct(AttributeFactory $attributeFactory)
    {
        $this->attributeFactory = $attributeFactory;
    }
    public function execute()
    {
        /** @var ProductAttributeInterface $model */
        $model = $this->attributeFactory->create();
        $model->loadByCode(Product::ENTITY,'color');
        $data = [];
        $data['optionvisual']['value'] = ['option_0' => ['Black', 'Black']];
        $data['swatchvisual']['value'] = ['option_0' => '#000000'];
        $model->addData($data);
        $model->save();
    }
}