Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving query performance for coupon code check #2973

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions app/code/core/Mage/SalesRule/Model/Resource/Rule/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ public function setValidationFilter($websiteId, $customerGroupId, $couponCode =
['code']
);

$noCouponCondition = $connection->quoteInto(
'main_table.coupon_type = ? ',
Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON
);

// sub request
$subSelect = clone $select;
$subSelect->distinct();
$subSelect->reset(Zend_Db_Select::COLUMNS)->columns('main_table.rule_id');
$orWhereConditions = [
$connection->quoteInto(
'(main_table.coupon_type = ? AND rule_coupons.type = 0)',
Expand All @@ -102,10 +101,18 @@ public function setValidationFilter($websiteId, $customerGroupId, $couponCode =
),
];
$orWhereCondition = implode(' OR ', $orWhereConditions);
$select->where(
$noCouponCondition . ' OR ((' . $orWhereCondition . ') AND rule_coupons.code = ?)',
$subSelect->where(
'(' . $orWhereCondition . ') AND rule_coupons.code = ?',
$couponCode
);
$subSelect->order('sort_order ' . self::SORT_ORDER_ASC);

// main request
$select->where(
'main_table.coupon_type = ? OR main_table.rule_id IN (' . $subSelect . ')',
Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON
);
$select->group('main_table.rule_id');
} else {
$this->addFieldToFilter('main_table.coupon_type', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON);
}
Expand Down
Loading