private function mergeProducts(Product $product, Product $existing) { $product->setId($existing->getId()); $product->setV($existing->getV() + 1); foreach ($product->getImages() as $image) { foreach ($existing->getImages() as $existingImage) { if ($image->getUrl() === $existingImage->getUrl()) { $image->setFileId($existingImage->getFileId()); } } } $categoryIds = $product->getCategoryIds(); foreach ($product->getCategoryTexts() as $i => $categoryText) { foreach ($existing->getCategoryTexts() as $j => $existingCategoryText) { if ($categoryText === $existingCategoryText) { $categoryIds[$i] = $existing->getCategoryIds()[$j]; } } } $product->setCategoryIds($categoryIds); }