10
10
use Magento \Catalog \Model \Product as ProductModel ;
11
11
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
12
12
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
13
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
14
+ use Magento \Eav \Api \AttributeRepositoryInterface ;
13
15
use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
14
16
use Magento \Framework \Api \SearchCriteriaInterface ;
15
17
use Magento \Framework \UrlInterface ;
19
21
use Magmodules \Reloadify \Model \RequestLog \CollectionFactory as RequestLogCollectionFactory ;
20
22
use Magento \Catalog \Model \Product \Visibility ;
21
23
use Magento \Store \Model \StoreManagerInterface ;
22
- use Magento \Catalog \Model \Product \Type ;
23
24
24
25
/**
25
26
* Product web API service class
@@ -69,6 +70,10 @@ class Product
69
70
* @var StoreManagerInterface
70
71
*/
71
72
private $ storeManager ;
73
+ /**
74
+ * @var AttributeRepositoryInterface
75
+ */
76
+ private $ attributeRepository ;
72
77
73
78
private $ mediaPath = '' ;
74
79
@@ -90,7 +95,8 @@ public function __construct(
90
95
ConfigRepository $ configRepository ,
91
96
CollectionProcessorInterface $ collectionProcessor ,
92
97
Visibility $ productVisibility ,
93
- StoreManagerInterface $ storeManager
98
+ StoreManagerInterface $ storeManager ,
99
+ AttributeRepositoryInterface $ attributeRepository
94
100
) {
95
101
$ this ->productsCollectionFactory = $ productsCollectionFactory ;
96
102
$ this ->reviewCollectionFactory = $ reviewCollectionFactory ;
@@ -99,6 +105,7 @@ public function __construct(
99
105
$ this ->collectionProcessor = $ collectionProcessor ;
100
106
$ this ->productVisibility = $ productVisibility ;
101
107
$ this ->storeManager = $ storeManager ;
108
+ $ this ->attributeRepository = $ attributeRepository ;
102
109
}
103
110
104
111
/**
@@ -113,23 +120,33 @@ public function execute(int $storeId, array $extra = [], SearchCriteriaInterface
113
120
$ data = [];
114
121
$ collection = $ this ->getCollection ($ storeId , $ extra , $ searchCriteria );
115
122
$ ean = $ this ->configRepository ->getEan ($ storeId );
123
+ $ eanType = $ this ->attributeRepository ->get (ProductAttributeInterface::ENTITY_TYPE_CODE , $ ean )
124
+ ->getFrontendInput ();
116
125
$ name = $ this ->configRepository ->getName ($ storeId );
126
+ $ nameType = $ this ->attributeRepository ->get (ProductAttributeInterface::ENTITY_TYPE_CODE , $ name )
127
+ ->getFrontendInput ();
117
128
$ sku = $ this ->configRepository ->getSku ($ storeId );
129
+ $ skuType = $ this ->attributeRepository ->get (ProductAttributeInterface::ENTITY_TYPE_CODE , $ sku )
130
+ ->getFrontendInput ();
118
131
$ brand = $ this ->configRepository ->getBrand ($ storeId );
132
+ $ brandType = $ this ->attributeRepository ->get (ProductAttributeInterface::ENTITY_TYPE_CODE , $ brand )
133
+ ->getFrontendInput ();
119
134
$ description = $ this ->configRepository ->getDescription ($ storeId );
135
+ $ descriptionType = $ this ->attributeRepository
136
+ ->get (ProductAttributeInterface::ENTITY_TYPE_CODE , $ description )->getFrontendInput ();
120
137
121
138
foreach ($ collection as $ product ) {
122
139
$ data [] = [
123
140
"id " => $ product ->getId (),
124
- "name " => $ this ->getAttributeValue ($ product , $ name ),
141
+ "name " => $ this ->getAttributeValue ($ product , $ name, $ nameType ),
125
142
'product_type ' => $ product ->getTypeId (),
126
- "ean " => $ this ->getAttributeValue ($ product , $ ean ),
143
+ "ean " => $ this ->getAttributeValue ($ product , $ ean, $ eanType ),
127
144
"short_description " => $ product ->getShortDescription (),
128
- "description " => $ this ->getAttributeValue ($ product , $ description ),
145
+ "description " => $ this ->getAttributeValue ($ product , $ description, $ descriptionType ),
129
146
"price " => $ product ->getPrice (),
130
147
"url " => $ product ->getProductUrl (),
131
- "sku " => $ this ->getAttributeValue ($ product , $ sku ),
132
- "brand " => $ this ->getAttributeValue ($ product , $ brand ),
148
+ "sku " => $ this ->getAttributeValue ($ product , $ sku, $ skuType ),
149
+ "brand " => $ this ->getAttributeValue ($ product , $ brand, $ brandType ),
133
150
"main_image " => $ this ->getMainImage ($ product ),
134
151
"visible " => (bool )((int )$ product ->getVisibility () - 1 ),
135
152
"variant_ids " => $ this ->getVariants ($ product ),
@@ -149,12 +166,12 @@ public function execute(int $storeId, array $extra = [], SearchCriteriaInterface
149
166
* @param $attribute
150
167
* @return mixed|string
151
168
*/
152
- private function getAttributeValue ($ product , $ attribute )
169
+ private function getAttributeValue ($ product , $ attribute, $ type )
153
170
{
154
171
$ value = '' ;
155
- if ($ attribute ) {
156
- if ($ dropdownValue = $ product -> getAttributeText ( $ attribute )) {
157
- $ value = $ dropdownValue ;
172
+ if ($ attribute && $ type ) {
173
+ if (( $ type == ' select ' ) || ( $ type == ' multiselect ' )) {
174
+ $ value = $ product -> getAttributeText ( $ attribute ) ;
158
175
} else {
159
176
$ value = $ product ->getData ($ attribute );
160
177
}
0 commit comments