PriceFieldsProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Adapter\BatchDataMapper;
  7. use Magento\Elasticsearch\Model\ResourceModel\Index;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
  10. use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider;
  11. use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
  14. /**
  15. * Provide data mapping for price fields
  16. */
  17. class PriceFieldsProvider implements AdditionalFieldsProviderInterface
  18. {
  19. /**
  20. * @var Index
  21. */
  22. private $resourceIndex;
  23. /**
  24. * @var DataProvider
  25. */
  26. private $dataProvider;
  27. /**
  28. * @var StoreManagerInterface
  29. */
  30. private $storeManager;
  31. /**
  32. * @var AttributeProvider
  33. */
  34. private $attributeAdapterProvider;
  35. /**
  36. * @var ResolverInterface
  37. */
  38. private $fieldNameResolver;
  39. /**
  40. * @param Index $resourceIndex
  41. * @param DataProvider $dataProvider
  42. * @param StoreManagerInterface $storeManager
  43. * @param AttributeProvider|null $attributeAdapterProvider
  44. * @param ResolverInterface|null $fieldNameResolver
  45. */
  46. public function __construct(
  47. Index $resourceIndex,
  48. DataProvider $dataProvider,
  49. StoreManagerInterface $storeManager,
  50. AttributeProvider $attributeAdapterProvider = null,
  51. ResolverInterface $fieldNameResolver = null
  52. ) {
  53. $this->resourceIndex = $resourceIndex;
  54. $this->dataProvider = $dataProvider;
  55. $this->storeManager = $storeManager;
  56. $this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
  57. ->get(AttributeProvider::class);
  58. $this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
  59. ->get(ResolverInterface::class);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function getFields(array $productIds, $storeId)
  65. {
  66. $priceData = $this->dataProvider->getSearchableAttribute('price')
  67. ? $this->resourceIndex->getPriceIndexData($productIds, $storeId)
  68. : [];
  69. $fields = [];
  70. foreach ($productIds as $productId) {
  71. $fields[$productId] = $this->getProductPriceData($productId, $storeId, $priceData);
  72. }
  73. return $fields;
  74. }
  75. /**
  76. * Prepare price index for product
  77. *
  78. * @param int $productId
  79. * @param int $websiteId
  80. * @param array $priceIndexData
  81. * @return array
  82. */
  83. private function getProductPriceData($productId, $websiteId, array $priceIndexData)
  84. {
  85. $result = [];
  86. if (array_key_exists($productId, $priceIndexData)) {
  87. $productPriceIndexData = $priceIndexData[$productId];
  88. $priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
  89. foreach ($productPriceIndexData as $customerGroupId => $price) {
  90. $fieldName = $this->fieldNameResolver->getFieldName(
  91. $priceAttribute,
  92. ['customerGroupId' => $customerGroupId, 'websiteId' => $websiteId]
  93. );
  94. $result[$fieldName] = sprintf('%F', $price);
  95. }
  96. }
  97. return $result;
  98. }
  99. }