SwatchAttributesProvider.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Model;
  7. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
  10. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Provide list of swatch attributes for product.
  14. */
  15. class SwatchAttributesProvider
  16. {
  17. /**
  18. * @var Configurable
  19. */
  20. private $typeConfigurable;
  21. /**
  22. * @var SwatchAttributeCodes
  23. */
  24. private $swatchAttributeCodes;
  25. /**
  26. * Key is productId, value is list of attributes
  27. * @var Attribute[]
  28. */
  29. private $attributesPerProduct;
  30. /**
  31. * @var SwatchAttributeType
  32. */
  33. private $swatchTypeChecker;
  34. /**
  35. * @param Configurable $typeConfigurable
  36. * @param SwatchAttributeCodes $swatchAttributeCodes
  37. * @param SwatchAttributeType|null $swatchTypeChecker
  38. */
  39. public function __construct(
  40. Configurable $typeConfigurable,
  41. SwatchAttributeCodes $swatchAttributeCodes,
  42. SwatchAttributeType $swatchTypeChecker = null
  43. ) {
  44. $this->typeConfigurable = $typeConfigurable;
  45. $this->swatchAttributeCodes = $swatchAttributeCodes;
  46. $this->swatchTypeChecker = $swatchTypeChecker
  47. ?: ObjectManager::getInstance()->create(SwatchAttributeType::class);
  48. }
  49. /**
  50. * Provide list of swatch attributes for product. If product is not configurable return empty array
  51. * Key is productId, value is list of attributes
  52. *
  53. * @param Product $product
  54. * @return Attribute[]
  55. */
  56. public function provide(Product $product)
  57. {
  58. if ($product->getTypeId() !== Configurable::TYPE_CODE) {
  59. return [];
  60. }
  61. if (!isset($this->attributesPerProduct[$product->getId()])) {
  62. $configurableAttributes = $this->typeConfigurable->getConfigurableAttributes($product);
  63. $swatchAttributeCodeMap = $this->swatchAttributeCodes->getCodes();
  64. $swatchAttributes = [];
  65. foreach ($configurableAttributes as $configurableAttribute) {
  66. if (array_key_exists($configurableAttribute->getAttributeId(), $swatchAttributeCodeMap)) {
  67. /** @var AbstractAttribute $productAttribute */
  68. $productAttribute = $configurableAttribute->getProductAttribute();
  69. if ($productAttribute !== null
  70. && $this->swatchTypeChecker->isSwatchAttribute($productAttribute)
  71. ) {
  72. $swatchAttributes[$configurableAttribute->getAttributeId()] = $productAttribute;
  73. }
  74. }
  75. }
  76. $this->attributesPerProduct[$product->getId()] = $swatchAttributes;
  77. }
  78. return $this->attributesPerProduct[$product->getId()];
  79. }
  80. }