Index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\ResourceModel;
  7. use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
  8. use Magento\Framework\Model\ResourceModel\Db\Context;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Magento\Catalog\Api\ProductRepositoryInterface;
  11. use Magento\Catalog\Api\CategoryRepositoryInterface;
  12. use Magento\Eav\Model\Config;
  13. use Magento\Catalog\Api\Data\ProductAttributeInterface;
  14. use Magento\Framework\EntityManager\MetadataPool;
  15. use Magento\Framework\Search\Request\IndexScopeResolverInterface as TableResolver;
  16. /**
  17. * Elasticsearch index resource model
  18. * @api
  19. * @since 100.1.0
  20. */
  21. class Index extends \Magento\AdvancedSearch\Model\ResourceModel\Index
  22. {
  23. /**
  24. * @var ProductRepositoryInterface
  25. * @since 100.1.0
  26. */
  27. protected $productRepository;
  28. /**
  29. * @var CategoryRepositoryInterface
  30. * @since 100.1.0
  31. */
  32. protected $categoryRepository;
  33. /**
  34. * @var Config
  35. * @since 100.1.0
  36. */
  37. protected $eavConfig;
  38. /**
  39. * Index constructor.
  40. * @param Context $context
  41. * @param StoreManagerInterface $storeManager
  42. * @param MetadataPool $metadataPool
  43. * @param ProductRepositoryInterface $productRepository
  44. * @param CategoryRepositoryInterface $categoryRepository
  45. * @param Config $eavConfig
  46. * @param null $connectionName
  47. * @param TableResolver|null $tableResolver
  48. * @param DimensionCollectionFactory|null $dimensionCollectionFactory
  49. * @SuppressWarnings(Magento.TypeDuplication)
  50. */
  51. public function __construct(
  52. Context $context,
  53. StoreManagerInterface $storeManager,
  54. MetadataPool $metadataPool,
  55. ProductRepositoryInterface $productRepository,
  56. CategoryRepositoryInterface $categoryRepository,
  57. Config $eavConfig,
  58. $connectionName = null,
  59. TableResolver $tableResolver = null,
  60. DimensionCollectionFactory $dimensionCollectionFactory = null
  61. ) {
  62. $this->productRepository = $productRepository;
  63. $this->categoryRepository = $categoryRepository;
  64. $this->eavConfig = $eavConfig;
  65. parent::__construct(
  66. $context,
  67. $storeManager,
  68. $metadataPool,
  69. $connectionName,
  70. $tableResolver,
  71. $dimensionCollectionFactory
  72. );
  73. }
  74. /**
  75. * Retrieve all attributes for given product ids
  76. *
  77. * @param int $productId
  78. * @param array $indexData
  79. * @return array
  80. * @since 100.1.0
  81. */
  82. public function getFullProductIndexData($productId, $indexData)
  83. {
  84. $productAttributes = [];
  85. $attributeCodes = $this->eavConfig->getEntityAttributeCodes(ProductAttributeInterface::ENTITY_TYPE_CODE);
  86. $product = $this->productRepository->getById($productId);
  87. foreach ($attributeCodes as $attributeCode) {
  88. $value = $product->getData($attributeCode);
  89. $attribute = $this->eavConfig->getAttribute(
  90. ProductAttributeInterface::ENTITY_TYPE_CODE,
  91. $attributeCode
  92. );
  93. $frontendInput = $attribute->getFrontendInput();
  94. if (in_array($attribute->getAttributeId(), array_keys($indexData))) {
  95. if (is_array($indexData[$attribute->getAttributeId()])) {
  96. if (isset($indexData[$attribute->getAttributeId()][$productId])) {
  97. $value = $indexData[$attribute->getAttributeId()][$productId];
  98. } else {
  99. $value = implode(' ', $indexData[$attribute->getAttributeId()]);
  100. }
  101. } else {
  102. $value = $indexData[$attribute->getAttributeId()];
  103. }
  104. }
  105. if ($value) {
  106. $productAttributes[$attributeCode] = $value;
  107. if ($frontendInput == 'select') {
  108. foreach ($attribute->getOptions() as $option) {
  109. if ($option->getValue() == $value) {
  110. $productAttributes[$attributeCode . '_value'] = $option->getLabel();
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return $productAttributes;
  117. }
  118. /**
  119. * Prepare full category index data for products.
  120. *
  121. * @param int $storeId
  122. * @param null|array $productIds
  123. * @return array
  124. * @since 100.1.0
  125. */
  126. public function getFullCategoryProductIndexData($storeId = null, $productIds = null)
  127. {
  128. $categoryPositions = $this->getCategoryProductIndexData($storeId, $productIds);
  129. $categoryData = [];
  130. foreach ($categoryPositions as $productId => $positions) {
  131. foreach ($positions as $categoryId => $position) {
  132. $category = $this->categoryRepository->get($categoryId, $storeId);
  133. $categoryName = $category->getName();
  134. $categoryData[$productId][] = [
  135. 'id' => $categoryId,
  136. 'name' => $categoryName,
  137. 'position' => $position
  138. ];
  139. }
  140. }
  141. return $categoryData;
  142. }
  143. }