11
11
use Magento \Bundle \Model \ResourceModel \Selection \CollectionFactory ;
12
12
use Magento \Bundle \Model \ResourceModel \Selection \Collection as LinkCollection ;
13
13
use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \Exception \NoSuchEntityException ;
15
+ use Magento \Framework \Exception \RuntimeException ;
14
16
use Magento \Framework \GraphQl \Query \EnumLookup ;
15
17
use Magento \Framework \GraphQl \Query \Uid ;
18
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
19
+ use Zend_Db_Select_Exception ;
16
20
17
21
/**
18
22
* Collection to fetch link data at resolution time.
@@ -47,20 +51,29 @@ class Collection
47
51
/** @var Uid */
48
52
private $ uidEncoder ;
49
53
54
+ /**
55
+ * @var ProductRepositoryInterface
56
+ */
57
+ private $ productRepository ;
58
+
50
59
/**
51
60
* @param CollectionFactory $linkCollectionFactory
52
61
* @param EnumLookup $enumLookup
53
62
* @param Uid|null $uidEncoder
63
+ * @param ProductRepositoryInterface|null $productRepository
54
64
*/
55
65
public function __construct (
56
66
CollectionFactory $ linkCollectionFactory ,
57
67
EnumLookup $ enumLookup ,
58
- Uid $ uidEncoder = null
68
+ Uid $ uidEncoder = null ,
69
+ ?ProductRepositoryInterface $ productRepository = null
59
70
) {
60
71
$ this ->linkCollectionFactory = $ linkCollectionFactory ;
61
72
$ this ->enumLookup = $ enumLookup ;
62
73
$ this ->uidEncoder = $ uidEncoder ?: ObjectManager::getInstance ()
63
74
->get (Uid::class);
75
+ $ this ->productRepository = $ productRepository ?: ObjectManager::getInstance ()
76
+ ->get (ProductRepositoryInterface::class);
64
77
}
65
78
66
79
/**
@@ -85,6 +98,9 @@ public function addIdFilters(int $optionId, int $parentId) : void
85
98
*
86
99
* @param int $optionId
87
100
* @return array
101
+ * @throws NoSuchEntityException
102
+ * @throws RuntimeException
103
+ * @throws Zend_Db_Select_Exception
88
104
*/
89
105
public function getLinksForOptionId (int $ optionId ) : array
90
106
{
@@ -101,6 +117,10 @@ public function getLinksForOptionId(int $optionId) : array
101
117
* Fetch link data and return in array format. Keys for links will be their option Ids.
102
118
*
103
119
* @return array
120
+ * @throws NoSuchEntityException
121
+ * @throws RuntimeException
122
+ * @throws Zend_Db_Select_Exception
123
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
104
124
*/
105
125
private function fetch () : array
106
126
{
@@ -123,26 +143,33 @@ private function fetch() : array
123
143
124
144
/** @var Selection $link */
125
145
foreach ($ linkCollection as $ link ) {
146
+ $ productDetails = [];
126
147
$ data = $ link ->getData ();
127
- $ formattedLink = [
128
- 'price ' => $ link ->getSelectionPriceValue (),
129
- 'position ' => $ link ->getPosition (),
130
- 'id ' => $ link ->getSelectionId (),
131
- 'uid ' => $ this ->uidEncoder ->encode ((string ) $ link ->getSelectionId ()),
132
- 'qty ' => (float )$ link ->getSelectionQty (),
133
- 'quantity ' => (float )$ link ->getSelectionQty (),
134
- 'is_default ' => (bool )$ link ->getIsDefault (),
135
- 'price_type ' => $ this ->enumLookup ->getEnumValueFromField (
136
- 'PriceTypeEnum ' ,
137
- (string )$ link ->getSelectionPriceType ()
138
- ) ?: 'DYNAMIC ' ,
139
- 'can_change_quantity ' => $ link ->getSelectionCanChangeQty (),
140
- ];
141
- $ data = array_replace ($ data , $ formattedLink );
142
- if (!isset ($ this ->links [$ link ->getOptionId ()])) {
143
- $ this ->links [$ link ->getOptionId ()] = [];
148
+ if (isset ($ data ['product_id ' ])) {
149
+ $ productDetails = $ this ->productRepository ->getById ($ data ['product_id ' ]);
150
+ }
151
+
152
+ if ($ productDetails && $ productDetails ->getIsSalable ()) {
153
+ $ formattedLink = [
154
+ 'price ' => $ link ->getSelectionPriceValue (),
155
+ 'position ' => $ link ->getPosition (),
156
+ 'id ' => $ link ->getSelectionId (),
157
+ 'uid ' => $ this ->uidEncoder ->encode ((string )$ link ->getSelectionId ()),
158
+ 'qty ' => (float )$ link ->getSelectionQty (),
159
+ 'quantity ' => (float )$ link ->getSelectionQty (),
160
+ 'is_default ' => (bool )$ link ->getIsDefault (),
161
+ 'price_type ' => $ this ->enumLookup ->getEnumValueFromField (
162
+ 'PriceTypeEnum ' ,
163
+ (string )$ link ->getSelectionPriceType ()
164
+ ) ?: 'DYNAMIC ' ,
165
+ 'can_change_quantity ' => $ link ->getSelectionCanChangeQty (),
166
+ ];
167
+ $ data = array_replace ($ data , $ formattedLink );
168
+ if (!isset ($ this ->links [$ link ->getOptionId ()])) {
169
+ $ this ->links [$ link ->getOptionId ()] = [];
170
+ }
171
+ $ this ->links [$ link ->getOptionId ()][] = $ data ;
144
172
}
145
- $ this ->links [$ link ->getOptionId ()][] = $ data ;
146
173
}
147
174
148
175
return $ this ->links ;
0 commit comments