Skip to content

Commit 2eed023

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96241' into 2.2-develop-pr20
2 parents 59676f4 + b419a63 commit 2eed023

File tree

1 file changed

+81
-49
lines changed

1 file changed

+81
-49
lines changed

app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ protected function _construct()
8080
}
8181

8282
/**
83+
* Map data for associated entities
84+
*
8385
* @param string $entityType
8486
* @param string $objectField
8587
* @throws \Magento\Framework\Exception\LocalizedException
@@ -114,6 +116,8 @@ protected function mapAssociatedEntities($entityType, $objectField)
114116
}
115117

116118
/**
119+
* Add website ids and customer group ids to rules data
120+
*
117121
* @return $this
118122
* @throws \Exception
119123
* @since 100.1.0
@@ -158,60 +162,15 @@ public function setValidationFilter(
158162

159163
$connection = $this->getConnection();
160164
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-
170165
$noCouponWhereCondition = $connection->quoteInto(
171-
'main_table.coupon_type = ? ',
166+
'main_table.coupon_type = ?',
172167
\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON
173168
);
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);
211170

212171
$select->where(
213-
$noCouponWhereCondition . ' OR ((' . $orWhereCondition . ') AND ' . $andWhereCondition . ')',
214-
null,
172+
$noCouponWhereCondition . ' OR main_table.rule_id IN (?)',
173+
$relatedRulesIds,
215174
Select::TYPE_CONDITION
216175
);
217176
} else {
@@ -227,6 +186,75 @@ public function setValidationFilter(
227186
return $this;
228187
}
229188

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+
230258
/**
231259
* Filter collection by website(s), customer group(s) and date.
232260
* Filter collection to only active rules.
@@ -366,6 +394,8 @@ public function addCustomerGroupFilter($customerGroupId)
366394
}
367395

368396
/**
397+
* Getter for _associatedEntitiesMap property
398+
*
369399
* @return array
370400
* @deprecated 100.1.0
371401
*/
@@ -380,6 +410,8 @@ private function getAssociatedEntitiesMap()
380410
}
381411

382412
/**
413+
* Getter for dateApplier property
414+
*
383415
* @return DateApplier
384416
* @deprecated 100.1.0
385417
*/

0 commit comments

Comments
 (0)