$fields */ public function apply(int $productId, array $storeIds, array $fields): int { $fields = $this->filterAndValidateFields($fields); if ($fields === []) { throw new \RuntimeException('No translatable fields were provided.'); } $updated = 0; foreach (array_unique(array_map('intval', $storeIds)) as $storeId) { if ($storeId <= 0) { continue; } $product = $this->productRepository->getById($productId, false, $storeId); $product->setStoreId($storeId); foreach ($fields as $code => $value) { $product->setData($code, $value); } $this->productRepository->save($product); $updated++; } if ($updated === 0) { throw new \RuntimeException('No valid target store views were provided.'); } return $updated; } /** * @param array $fields * @return array */ private function filterAndValidateFields(array $fields): array { $result = []; foreach ($fields as $code => $value) { $code = (string)$code; if (!$this->fieldProvider->isAttributeAllowed($code)) { continue; } $value = (string)$value; if ($this->fieldProvider->isHtmlAttributeCode($code)) { $errors = $this->htmlValidator->validate($value); if ($errors !== []) { throw new \RuntimeException(sprintf( 'HTML for "%s" is invalid: %s', $code, implode('; ', $errors) )); } } $result[$code] = $value; } return $result; } }