ProductIdentitiesExtender.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\ConfigurableProduct\Model\Plugin;
  8. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Catalog\Model\Product;
  11. /**
  12. * Extender of product identities for child of configurable products
  13. */
  14. class ProductIdentitiesExtender
  15. {
  16. /**
  17. * @var Configurable
  18. */
  19. private $configurableType;
  20. /**
  21. * @var ProductRepositoryInterface
  22. */
  23. private $productRepository;
  24. /**
  25. * @param Configurable $configurableType
  26. * @param ProductRepositoryInterface $productRepository
  27. */
  28. public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository)
  29. {
  30. $this->configurableType = $configurableType;
  31. $this->productRepository = $productRepository;
  32. }
  33. /**
  34. * Add parent identities to product identities
  35. *
  36. * @param Product $subject
  37. * @param array $identities
  38. * @return array
  39. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  40. */
  41. public function afterGetIdentities(Product $subject, array $identities): array
  42. {
  43. foreach ($this->configurableType->getParentIdsByChild($subject->getId()) as $parentId) {
  44. $parentProduct = $this->productRepository->getById($parentId);
  45. $identities = array_merge($identities, $parentProduct->getIdentities());
  46. }
  47. return array_unique($identities);
  48. }
  49. }