Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PriceFieldsProvider.php
Go to the documentation of this file.
1 <?php
8 
16 
21 {
25  private $resourceIndex;
26 
30  private $dataProvider;
31 
35  private $storeManager;
36 
40  private $attributeAdapterProvider;
41 
45  private $fieldNameResolver;
46 
54  public function __construct(
55  Index $resourceIndex,
56  DataProvider $dataProvider,
57  StoreManagerInterface $storeManager,
58  AttributeProvider $attributeAdapterProvider = null,
59  ResolverInterface $fieldNameResolver = null
60  ) {
61  $this->resourceIndex = $resourceIndex;
62  $this->dataProvider = $dataProvider;
63  $this->storeManager = $storeManager;
64  $this->attributeAdapterProvider = $attributeAdapterProvider ?: ObjectManager::getInstance()
65  ->get(AttributeProvider::class);
66  $this->fieldNameResolver = $fieldNameResolver ?: ObjectManager::getInstance()
67  ->get(ResolverInterface::class);
68  }
69 
73  public function getFields(array $productIds, $storeId)
74  {
75  $priceData = $this->dataProvider->getSearchableAttribute('price')
76  ? $this->resourceIndex->getPriceIndexData($productIds, $storeId)
77  : [];
78 
79  $fields = [];
80  foreach ($productIds as $productId) {
81  $fields[$productId] = $this->getProductPriceData($productId, $storeId, $priceData);
82  }
83 
84  return $fields;
85  }
86 
95  private function getProductPriceData($productId, $websiteId, array $priceIndexData)
96  {
97  $result = [];
98  if (array_key_exists($productId, $priceIndexData)) {
99  $productPriceIndexData = $priceIndexData[$productId];
100  $priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
101  foreach ($productPriceIndexData as $customerGroupId => $price) {
102  $fieldName = $this->fieldNameResolver->getFieldName(
103  $priceAttribute,
104  ['customerGroupId' => $customerGroupId, 'websiteId' => $websiteId]
105  );
106  $result[$fieldName] = sprintf('%F', $price);
107  }
108  }
109 
110  return $result;
111  }
112 }
$fields
Definition: details.phtml:14
$storeManager
__construct(Index $resourceIndex, DataProvider $dataProvider, StoreManagerInterface $storeManager, AttributeProvider $attributeAdapterProvider=null, ResolverInterface $fieldNameResolver=null)
$price