11<?php
2+
23namespace Lof \ProductAttributesGraphQl \Model \Resolver \DataProvider ;
34
5+ use Magento \Catalog \Model \Product \Attribute \Repository ;
46use Magento \Catalog \Model \ProductRepository ;
7+ use Magento \Catalog \Model \ResourceModel \Product \Attribute \CollectionFactory ;
8+ use Magento \Framework \Api \AttributeInterface ;
9+ use Magento \Framework \Exception \NoSuchEntityException ;
510
611class Attributes
712{
813 /**
914 * @var ProductRepository
1015 */
11- protected $ productRepository ;
16+ private $ productRepository ;
17+
18+ /**
19+ * @var CollectionFactory
20+ */
21+ private $ attributeFactory ;
22+
23+ /**
24+ * @var Repository
25+ */
26+ private $ attributeRepository ;
1227
1328 /**
1429 * @param ProductRepository $productRepository
30+ * @param CollectionFactory $attributeFactory
31+ * @param Repository $attributeRepository
1532 */
1633 public function __construct (
1734 ProductRepository $ productRepository ,
18- ){
35+ CollectionFactory $ attributeFactory ,
36+ Repository $ attributeRepository
37+ )
38+ {
1939 $ this ->productRepository = $ productRepository ;
40+ $ this ->attributeFactory = $ attributeFactory ;
41+ $ this ->attributeRepository = $ attributeRepository ;
2042 }
2143
2244 /**
45+ * Get additional attributes by sku
46+ *
2347 * @param $sku
24- * @return \Magento\Framework\Api\ AttributeInterface[]|null
25- * @throws \Magento\Framework\Exception\ NoSuchEntityException
48+ * @return AttributeInterface[]|null
49+ * @throws NoSuchEntityException
2650 */
27- public function getBySku ($ sku )
51+ public function getAdditionalAttributesBySku ($ sku )
2852 {
2953 if (!$ sku ) {
3054 return null ;
@@ -33,7 +57,7 @@ public function getBySku($sku)
3357 $ attributes = $ product ->getAttributes ();
3458 $ additionalAttributes = [];
3559 foreach ($ attributes as $ attribute ) {
36- if ($ attribute ->getIsUserDefined () && $ attribute ->getIsVisibleOnFront ()) {
60+ if ($ attribute ->getIsUserDefined () && $ attribute ->getIsVisibleOnFront ()) {
3761 $ data ['id ' ] = $ attribute ->getId ();
3862 $ data ['code ' ] = $ attribute ->getAttributeCode ();
3963 $ data ['label ' ] = $ attribute ->getStoreLabel ();
@@ -45,4 +69,39 @@ public function getBySku($sku)
4569 }
4670 return $ additionalAttributes ;
4771 }
72+
73+ /**
74+ * Get visible and filterable attributes and options
75+ *
76+ * @return array
77+ * @throws NoSuchEntityException
78+ */
79+ public function getVisibleAttributes ()
80+ {
81+ $ attributeData = [];
82+ $ attributeInfo = $ this ->attributeFactory ->create ();
83+ foreach ($ attributeInfo as $ items ) {
84+ if ($ items ->getIsVisibleOnFront () && $ items ->getIsFilterable ()) {
85+ $ options = $ this ->attributeRepository ->get ($ items ->getAttributeCode ())->getOptions ();
86+ $ optionData = [];
87+ foreach ($ options as $ option ) {
88+ if (!($ option ->getValue () == null ) || !($ option ->getLabel () == " " )) {
89+ $ optionData [] = [
90+ 'label ' => $ option ->getLabel (),
91+ 'value ' => $ option ->getValue (),
92+ ];
93+ }
94+ }
95+ if (!empty ($ optionData )) {
96+ $ attributeData [] = [
97+ 'attribute_code ' => $ items ->getAttributeCode (),
98+ 'attribute_label ' => $ items ->getStoreLabel (),
99+ 'total_count ' => count ($ optionData ),
100+ 'options ' => $ optionData ,
101+ ];
102+ }
103+ }
104+ }
105+ return $ attributeData ;
106+ }
48107}
0 commit comments