@@ -80,6 +80,8 @@ protected function _construct()
80
80
}
81
81
82
82
/**
83
+ * Map data for associated entities
84
+ *
83
85
* @param string $entityType
84
86
* @param string $objectField
85
87
* @throws \Magento\Framework\Exception\LocalizedException
@@ -114,6 +116,8 @@ protected function mapAssociatedEntities($entityType, $objectField)
114
116
}
115
117
116
118
/**
119
+ * Add website ids and customer group ids to rules data
120
+ *
117
121
* @return $this
118
122
* @throws \Exception
119
123
* @since 100.1.0
@@ -158,60 +162,15 @@ public function setValidationFilter(
158
162
159
163
$ connection = $ this ->getConnection ();
160
164
if (strlen ($ couponCode )) {
161
- $ select ->joinLeft (
162
- ['rule_coupons ' => $ this ->getTable ('salesrule_coupon ' )],
163
- $ connection ->quoteInto (
164
- 'main_table.rule_id = rule_coupons.rule_id AND main_table.coupon_type != ? ' ,
165
- \Magento \SalesRule \Model \Rule::COUPON_TYPE_NO_COUPON
166
- ),
167
- ['code ' ]
168
- );
169
-
170
165
$ noCouponWhereCondition = $ connection ->quoteInto (
171
- 'main_table.coupon_type = ? ' ,
166
+ 'main_table.coupon_type = ? ' ,
172
167
\Magento \SalesRule \Model \Rule::COUPON_TYPE_NO_COUPON
173
168
);
174
-
175
- $ autoGeneratedCouponCondition = [
176
- $ connection ->quoteInto (
177
- "main_table.coupon_type = ? " ,
178
- \Magento \SalesRule \Model \Rule::COUPON_TYPE_AUTO
179
- ),
180
- $ connection ->quoteInto (
181
- "rule_coupons.type = ? " ,
182
- \Magento \SalesRule \Api \Data \CouponInterface::TYPE_GENERATED
183
- ),
184
- ];
185
-
186
- $ orWhereConditions = [
187
- "( " . implode ($ autoGeneratedCouponCondition , " AND " ) . ") " ,
188
- $ connection ->quoteInto (
189
- '(main_table.coupon_type = ? AND main_table.use_auto_generation = 1 AND rule_coupons.type = 1) ' ,
190
- \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC
191
- ),
192
- $ connection ->quoteInto (
193
- '(main_table.coupon_type = ? AND main_table.use_auto_generation = 0 AND rule_coupons.type = 0) ' ,
194
- \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC
195
- ),
196
- ];
197
-
198
- $ andWhereConditions = [
199
- $ connection ->quoteInto (
200
- 'rule_coupons.code = ? ' ,
201
- $ couponCode
202
- ),
203
- $ connection ->quoteInto (
204
- '(rule_coupons.expiration_date IS NULL OR rule_coupons.expiration_date >= ?) ' ,
205
- $ this ->_date ->date ()->format ('Y-m-d ' )
206
- ),
207
- ];
208
-
209
- $ orWhereCondition = implode (' OR ' , $ orWhereConditions );
210
- $ andWhereCondition = implode (' AND ' , $ andWhereConditions );
169
+ $ relatedRulesIds = $ this ->getCouponRelatedRuleIds ($ couponCode );
211
170
212
171
$ select ->where (
213
- $ noCouponWhereCondition . ' OR (( ' . $ orWhereCondition . ' ) AND ' . $ andWhereCondition . ' ) ' ,
214
- null ,
172
+ $ noCouponWhereCondition . ' OR main_table.rule_id IN (? ) ' ,
173
+ $ relatedRulesIds ,
215
174
Select::TYPE_CONDITION
216
175
);
217
176
} else {
@@ -227,6 +186,75 @@ public function setValidationFilter(
227
186
return $ this ;
228
187
}
229
188
189
+ /**
190
+ * Get rules ids related to coupon code
191
+ *
192
+ * @param string $couponCode
193
+ * @return array
194
+ */
195
+ private function getCouponRelatedRuleIds (string $ couponCode ): array
196
+ {
197
+ $ connection = $ this ->getConnection ();
198
+ $ select = $ connection ->select ()->from (
199
+ ['main_table ' => $ this ->getTable ('salesrule ' )],
200
+ 'rule_id '
201
+ );
202
+ $ select ->joinLeft (
203
+ ['rule_coupons ' => $ this ->getTable ('salesrule_coupon ' )],
204
+ $ connection ->quoteInto (
205
+ 'main_table.rule_id = rule_coupons.rule_id AND main_table.coupon_type != ? ' ,
206
+ \Magento \SalesRule \Model \Rule::COUPON_TYPE_NO_COUPON ,
207
+ null
208
+ )
209
+ );
210
+
211
+ $ autoGeneratedCouponCondition = [
212
+ $ connection ->quoteInto (
213
+ "main_table.coupon_type = ? " ,
214
+ \Magento \SalesRule \Model \Rule::COUPON_TYPE_AUTO
215
+ ),
216
+ $ connection ->quoteInto (
217
+ "rule_coupons.type = ? " ,
218
+ \Magento \SalesRule \Api \Data \CouponInterface::TYPE_GENERATED
219
+ ),
220
+ ];
221
+
222
+ $ orWhereConditions = [
223
+ "( " . implode ($ autoGeneratedCouponCondition , " AND " ) . ") " ,
224
+ $ connection ->quoteInto (
225
+ '(main_table.coupon_type = ? AND main_table.use_auto_generation = 1 AND rule_coupons.type = 1) ' ,
226
+ \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC
227
+ ),
228
+ $ connection ->quoteInto (
229
+ '(main_table.coupon_type = ? AND main_table.use_auto_generation = 0 AND rule_coupons.type = 0) ' ,
230
+ \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC
231
+ ),
232
+ ];
233
+
234
+ $ andWhereConditions = [
235
+ $ connection ->quoteInto (
236
+ 'rule_coupons.code = ? ' ,
237
+ $ couponCode
238
+ ),
239
+ $ connection ->quoteInto (
240
+ '(rule_coupons.expiration_date IS NULL OR rule_coupons.expiration_date >= ?) ' ,
241
+ $ this ->_date ->date ()->format ('Y-m-d ' )
242
+ ),
243
+ ];
244
+
245
+ $ orWhereCondition = implode (' OR ' , $ orWhereConditions );
246
+ $ andWhereCondition = implode (' AND ' , $ andWhereConditions );
247
+
248
+ $ select ->where (
249
+ '( ' . $ orWhereCondition . ') AND ' . $ andWhereCondition ,
250
+ null ,
251
+ Select::TYPE_CONDITION
252
+ );
253
+ $ select ->group ('main_table.rule_id ' );
254
+
255
+ return $ connection ->fetchCol ($ select );
256
+ }
257
+
230
258
/**
231
259
* Filter collection by website(s), customer group(s) and date.
232
260
* Filter collection to only active rules.
@@ -366,6 +394,8 @@ public function addCustomerGroupFilter($customerGroupId)
366
394
}
367
395
368
396
/**
397
+ * Getter for _associatedEntitiesMap property
398
+ *
369
399
* @return array
370
400
* @deprecated 100.1.0
371
401
*/
@@ -380,6 +410,8 @@ private function getAssociatedEntitiesMap()
380
410
}
381
411
382
412
/**
413
+ * Getter for dateApplier property
414
+ *
383
415
* @return DateApplier
384
416
* @deprecated 100.1.0
385
417
*/
0 commit comments