Skip to content

Updating label for product attribute option on 1 storeview only

How can I update storeview label without removing label on the rest of the storeviews?

The code works fine for updating just 1 storeview, but it removes all other labels. Updating store 2, removes label from store 1+3+4 ??

Relevant part of code:

use MagentoCatalogApiProductAttributeRepositoryInterface;
use MagentoEavApiDataAttributeOptionLabelInterfaceFactory;

/* .......... */


         foreach($csv as $obj){
            $attr = $this->productAttributeRepository->get('xxxxxxxx');
            $options = $attr->getOptions();
            foreach ($options as $option) {
                if ($option->getLabel() == $obj[1]) {

                    $optionLabel = $this->optionLabelFactory->create();
                    $optionLabel->setStoreId(2);
                    $optionLabel->setLabel($obj[5]);

                    $option->setStoreLabels([$optionLabel]);
                    $attr->setOptions([$option]);
                    $this->productAttributeRepository->save($attr);
                }
            }
        }