Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Index.php
Go to the documentation of this file.
1 <?php
7 
17 
24 {
29  protected $productRepository;
30 
36 
41  protected $eavConfig;
42 
56  public function __construct(
57  Context $context,
63  $connectionName = null,
64  TableResolver $tableResolver = null,
65  DimensionCollectionFactory $dimensionCollectionFactory = null
66  ) {
67  $this->productRepository = $productRepository;
68  $this->categoryRepository = $categoryRepository;
69  $this->eavConfig = $eavConfig;
70  parent::__construct(
71  $context,
75  $tableResolver,
76  $dimensionCollectionFactory
77  );
78  }
79 
88  public function getFullProductIndexData($productId, $indexData)
89  {
90  $productAttributes = [];
91  $attributeCodes = $this->eavConfig->getEntityAttributeCodes(ProductAttributeInterface::ENTITY_TYPE_CODE);
92  $product = $this->productRepository->getById($productId);
93  foreach ($attributeCodes as $attributeCode) {
94  $value = $product->getData($attributeCode);
95  $attribute = $this->eavConfig->getAttribute(
98  );
99  $frontendInput = $attribute->getFrontendInput();
100  if (in_array($attribute->getAttributeId(), array_keys($indexData))) {
101  if (is_array($indexData[$attribute->getAttributeId()])) {
102  if (isset($indexData[$attribute->getAttributeId()][$productId])) {
103  $value = $indexData[$attribute->getAttributeId()][$productId];
104  } else {
105  $value = implode(' ', $indexData[$attribute->getAttributeId()]);
106  }
107  } else {
108  $value = $indexData[$attribute->getAttributeId()];
109  }
110  }
111  if ($value) {
112  $productAttributes[$attributeCode] = $value;
113  if ($frontendInput == 'select') {
114  foreach ($attribute->getOptions() as $option) {
115  if ($option->getValue() == $value) {
116  $productAttributes[$attributeCode . '_value'] = $option->getLabel();
117  }
118  }
119  }
120  }
121  }
122  return $productAttributes;
123  }
124 
133  public function getFullCategoryProductIndexData($storeId = null, $productIds = null)
134  {
135  $categoryPositions = $this->getCategoryProductIndexData($storeId, $productIds);
136  $categoryData = [];
137 
138  foreach ($categoryPositions as $productId => $positions) {
139  foreach ($positions as $categoryId => $position) {
140  $category = $this->categoryRepository->get($categoryId, $storeId);
141  $categoryName = $category->getName();
142  $categoryData[$productId][] = [
143  'id' => $categoryId,
144  'name' => $categoryName,
145  'position' => $position
146  ];
147  }
148  }
149  return $categoryData;
150  }
151 }
getFullCategoryProductIndexData($storeId=null, $productIds=null)
Definition: Index.php:133
getFullProductIndexData($productId, $indexData)
Definition: Index.php:88
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
getCategoryProductIndexData($storeId=null, $productIds=null)
Definition: Index.php:158
__construct(Context $context, StoreManagerInterface $storeManager, MetadataPool $metadataPool, ProductRepositoryInterface $productRepository, CategoryRepositoryInterface $categoryRepository, Config $eavConfig, $connectionName=null, TableResolver $tableResolver=null, DimensionCollectionFactory $dimensionCollectionFactory=null)
Definition: Index.php:56