PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable/Attribute/Collection.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 299 lines | 167 code | 23 blank | 109 comment | 6 complexity | 954dae47a0a1ff9593cec40d5367f97f MD5 | raw file
  1. <?php
  2. /**
  3. * Catalog Configurable Product Attribute Collection
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
  9. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  10. /**
  11. * @SuppressWarnings(PHPMD.LongVariable)
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  15. {
  16. /**
  17. * Configurable attributes label table name
  18. *
  19. * @var string
  20. */
  21. protected $_labelTable;
  22. /**
  23. * Product instance
  24. *
  25. * @var \Magento\Catalog\Model\Product
  26. */
  27. protected $_product;
  28. /**
  29. * Catalog data
  30. *
  31. * @var \Magento\Catalog\Helper\Data
  32. */
  33. protected $_catalogData = null;
  34. /**
  35. * Catalog product type configurable
  36. *
  37. * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
  38. */
  39. protected $_productTypeConfigurable;
  40. /**
  41. * Store manager
  42. *
  43. * @var \Magento\Store\Model\StoreManagerInterface
  44. */
  45. protected $_storeManager;
  46. /**
  47. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  48. * @param \Psr\Log\LoggerInterface $logger
  49. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  50. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  51. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  52. * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $catalogProductTypeConfigurable
  53. * @param \Magento\Catalog\Helper\Data $catalogData
  54. * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute $resource
  55. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  56. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  57. */
  58. public function __construct(
  59. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  60. \Psr\Log\LoggerInterface $logger,
  61. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  62. \Magento\Framework\Event\ManagerInterface $eventManager,
  63. \Magento\Store\Model\StoreManagerInterface $storeManager,
  64. \Magento\ConfigurableProduct\Model\Product\Type\Configurable $catalogProductTypeConfigurable,
  65. \Magento\Catalog\Helper\Data $catalogData,
  66. \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute $resource,
  67. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
  68. ) {
  69. $this->_storeManager = $storeManager;
  70. $this->_productTypeConfigurable = $catalogProductTypeConfigurable;
  71. $this->_catalogData = $catalogData;
  72. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  73. }
  74. /**
  75. * Initialize connection and define table names
  76. *
  77. * @return void
  78. */
  79. protected function _construct()
  80. {
  81. $this->_init(
  82. 'Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute',
  83. 'Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute'
  84. );
  85. $this->_labelTable = $this->getTable('catalog_product_super_attribute_label');
  86. }
  87. /**
  88. * Set Product filter (Configurable)
  89. *
  90. * @param \Magento\Catalog\Model\Product $product
  91. * @return $this
  92. */
  93. public function setProductFilter($product)
  94. {
  95. $this->_product = $product;
  96. return $this->addFieldToFilter('product_id', $product->getId());
  97. }
  98. /**
  99. * Get product type
  100. *
  101. * @return \Magento\ConfigurableProduct\Model\Product\Type\Configurable
  102. */
  103. private function getProductType()
  104. {
  105. return $this->_productTypeConfigurable;
  106. }
  107. /**
  108. * Set order collection by Position
  109. *
  110. * @param string $dir
  111. * @return $this
  112. */
  113. public function orderByPosition($dir = self::SORT_ORDER_ASC)
  114. {
  115. $this->setOrder('position ', $dir);
  116. return $this;
  117. }
  118. /**
  119. * Retrieve Store Id
  120. *
  121. * @return int
  122. */
  123. public function getStoreId()
  124. {
  125. return (int)$this->_product->getStoreId();
  126. }
  127. /**
  128. * After load collection process
  129. *
  130. * @return $this
  131. */
  132. protected function _afterLoad()
  133. {
  134. parent::_afterLoad();
  135. \Magento\Framework\Profiler::start('TTT1:' . __METHOD__, ['group' => 'TTT1', 'method' => __METHOD__]);
  136. $this->_addProductAttributes();
  137. \Magento\Framework\Profiler::stop('TTT1:' . __METHOD__);
  138. \Magento\Framework\Profiler::start('TTT2:' . __METHOD__, ['group' => 'TTT2', 'method' => __METHOD__]);
  139. $this->_addAssociatedProductFilters();
  140. \Magento\Framework\Profiler::stop('TTT2:' . __METHOD__);
  141. \Magento\Framework\Profiler::start('TTT3:' . __METHOD__, ['group' => 'TTT3', 'method' => __METHOD__]);
  142. $this->_loadLabels();
  143. \Magento\Framework\Profiler::stop('TTT3:' . __METHOD__);
  144. \Magento\Framework\Profiler::start('TTT4:' . __METHOD__, ['group' => 'TTT4', 'method' => __METHOD__]);
  145. $this->loadOptions();
  146. \Magento\Framework\Profiler::stop('TTT4:' . __METHOD__);
  147. return $this;
  148. }
  149. /**
  150. * Add product attributes to collection items
  151. *
  152. * @return $this
  153. */
  154. protected function _addProductAttributes()
  155. {
  156. foreach ($this->_items as $item) {
  157. $productAttribute = $this->getProductType()->getAttributeById(
  158. $item->getAttributeId(),
  159. $this->getProduct()
  160. );
  161. $item->setProductAttribute($productAttribute);
  162. }
  163. return $this;
  164. }
  165. /**
  166. * Add Associated Product Filters (From Product Type Instance)
  167. *
  168. * @return $this
  169. */
  170. public function _addAssociatedProductFilters()
  171. {
  172. $this->getProductType()->getUsedProducts(
  173. $this->getProduct(),
  174. $this->getColumnValues('attribute_id') // Filter associated products
  175. );
  176. return $this;
  177. }
  178. /**
  179. * Load attribute labels
  180. *
  181. * @return $this
  182. */
  183. protected function _loadLabels()
  184. {
  185. if ($this->count()) {
  186. $useDefaultCheck = $this->getConnection()->getCheckSql(
  187. 'store.use_default IS NULL',
  188. 'def.use_default',
  189. 'store.use_default'
  190. );
  191. $labelCheck = $this->getConnection()->getCheckSql('store.value IS NULL', 'def.value', 'store.value');
  192. $select = $this->getConnection()->select()->from(
  193. ['def' => $this->_labelTable]
  194. )->joinLeft(
  195. ['store' => $this->_labelTable],
  196. $this->getConnection()->quoteInto(
  197. 'store.product_super_attribute_id = def.product_super_attribute_id AND store.store_id = ?',
  198. $this->getStoreId()
  199. ),
  200. ['use_default' => $useDefaultCheck, 'label' => $labelCheck]
  201. )->where(
  202. 'def.product_super_attribute_id IN (?)',
  203. array_keys($this->_items)
  204. )->where(
  205. 'def.store_id = ?',
  206. 0
  207. );
  208. $result = $this->getConnection()->fetchAll($select);
  209. foreach ($result as $data) {
  210. $this->getItemById($data['product_super_attribute_id'])->setLabel($data['label']);
  211. $this->getItemById($data['product_super_attribute_id'])->setUseDefault($data['use_default']);
  212. }
  213. }
  214. return $this;
  215. }
  216. /**
  217. * @return void
  218. */
  219. protected function loadOptions()
  220. {
  221. $usedProducts = $this->getProductType()->getUsedProducts($this->getProduct());
  222. if ($usedProducts) {
  223. foreach ($this->_items as $item) {
  224. $values = [];
  225. $productAttribute = $item->getProductAttribute();
  226. if (!$productAttribute instanceof AbstractAttribute) {
  227. continue;
  228. }
  229. $itemId = $item->getId();
  230. $options = $this->getIncludedOptions($usedProducts, $productAttribute);
  231. foreach ($options as $option) {
  232. foreach ($usedProducts as $associatedProduct) {
  233. $attributeCodeValue = $associatedProduct->getData($productAttribute->getAttributeCode());
  234. if (!empty($option['value']) && $option['value'] == $attributeCodeValue) {
  235. $values[$itemId . ':' . $option['value']] = [
  236. 'value_index' => $option['value'],
  237. 'label' => $option['label'],
  238. 'product_super_attribute_id' => $itemId,
  239. 'default_label' => $option['label'],
  240. 'store_label' => $option['label'],
  241. 'use_default_value' => true,
  242. ];
  243. }
  244. }
  245. }
  246. $values = array_values($values);
  247. $item->setOptions($values);
  248. }
  249. }
  250. }
  251. /**
  252. * Get options for all product attribute values from used products
  253. *
  254. * @param \Magento\Catalog\Model\Product[] $usedProducts
  255. * @param AbstractAttribute $productAttribute
  256. * @return array
  257. */
  258. protected function getIncludedOptions(array $usedProducts, AbstractAttribute $productAttribute)
  259. {
  260. $attributeValues = [];
  261. foreach ($usedProducts as $associatedProduct) {
  262. $attributeValues[] = $associatedProduct->getData($productAttribute->getAttributeCode());
  263. }
  264. $options = $productAttribute->getSource()->getSpecificOptions(array_unique($attributeValues));
  265. return $options;
  266. }
  267. /**
  268. * Retrieve product instance
  269. *
  270. * @return \Magento\Catalog\Model\Product
  271. */
  272. public function getProduct()
  273. {
  274. return $this->_product;
  275. }
  276. }