Skip to content

Adding more than one custom options programmatically

tried different solutions here which I found, but none of them is working for me.
I want to add two custom options to a product with the code below.
Problem is, that there is always only the second option added to the product and I do not know why.
As far as I understand, it should add both of them but it is not working.
Working on Magento 2.4.3
Here is my code:

    public function addOptionsEvent($productcopy)
{
    // $this->_options is a dependency injection of MagentoCatalogModelProductOption
    // I want to add two custom options as seen in the array below
    
    $options = [
        [
            'sort_order' => '1',
            'title' => 'Anmerkungen zu Eurer Reservierung',
            'price_type' => 'fixed',
            'price' => '0',
            'type' => 'field',
            'is_require' => '0',
        ],
        [
            'sort_order' => '2',
            'title' => 'Telefonnummer',
            'price_type' => 'fixed',
            'price' => '0',
            'type' => 'field',
            'is_require' => '1',
        ]
    ];
    
    foreach ($options as $arrayOption) {
        $this->_options->setProductId($productcopy->getId());
        $this->_options->setStoreId($productcopy->getStoreId());
        $this->_options->addData($arrayOption);
        $this->_options->save();
        $productcopy->addOption($this->_options);
    }

    $productcopy->getResource()->save($productcopy);


}

Any ideas?
Please let me know.
BR
CHequille