Skip to content

Commit a7f6412

Browse files
committed
magento#12817: Coupon code with canceled order.
1 parent cda7835 commit a7f6412

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

app/code/Magento/SalesRule/Model/ResourceModel/Coupon/Usage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function updateCustomerCouponTimesUsed($customerId, $couponId, $increment
4545

4646
$timesUsed = $connection->fetchOne($select, [':coupon_id' => $couponId, ':customer_id' => $customerId]);
4747

48-
if ($timesUsed > 0) {
48+
if ($timesUsed !== false) {
4949
$this->getConnection()->update(
5050
$this->getMainTable(),
5151
['times_used' => $timesUsed + ($increment ? 1 : -1)],

app/code/Magento/SalesRule/Observer/SalesOrderAfterCancelObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function execute(EventObserver $observer)
9191

9292
if ($ruleCustomer->getId()) {
9393
if ($ruleCustomer->getTimesUsed() > 0) {
94-
$ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + 1);
94+
$ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() - 1);
9595
}
9696
} else {
9797
$ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(0);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\SalesRule\Model\ResourceModel\Coupon;
8+
9+
use Magento\Framework\DataObject;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\SalesRule\Model\Coupon;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
/**
15+
* Tests accounting of coupon usages.
16+
*
17+
* @magentoDbIsolation enabled
18+
* @magentoAppIsolation enabled
19+
*/
20+
class UsageTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var Coupon
29+
*/
30+
private $coupon;
31+
32+
/**
33+
* @var Usage
34+
*/
35+
private $usage;
36+
37+
/**
38+
* @var DataObject
39+
*/
40+
private $couponUsage;
41+
42+
/**
43+
* Tests incrementing and decrementing of coupon usages.
44+
*
45+
* @magentoDataFixture Magento/Customer/_files/customer.php
46+
* @magentoDataFixture Magento/SalesRule/_files/coupons_limited.php
47+
*/
48+
public function testUpdateCustomerCouponTimesUsed()
49+
{
50+
$customerId = 1;
51+
$couponCode = 'one_usage';
52+
53+
$this->coupon->loadByCode($couponCode);
54+
55+
$testCases = [
56+
['increment' => true, 'expected' => 1],
57+
['increment' => false, 'expected' => 0],
58+
['increment' => false, 'expected' => 0],
59+
['increment' => true, 'expected' => 1],
60+
['increment' => true, 'expected' => 2],
61+
];
62+
63+
foreach ($testCases as $testCase) {
64+
$this->usage->updateCustomerCouponTimesUsed($customerId, $this->coupon->getId(), $testCase['increment']);
65+
$this->usage->loadByCustomerCoupon($this->couponUsage, $customerId, $this->coupon->getId());
66+
67+
self::assertEquals(
68+
$testCase['expected'],
69+
$this->couponUsage->getTimesUsed()
70+
);
71+
}
72+
}
73+
74+
protected function setUp()
75+
{
76+
$this->objectManager = Bootstrap::getObjectManager();
77+
$this->coupon = $this->objectManager->get(Coupon::class);
78+
$this->usage = $this->objectManager->get(Usage::class);
79+
$this->couponUsage = $this->objectManager->get(DataObject::class);
80+
}
81+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\SalesRule\Model\Coupon;
8+
9+
require_once 'rules.php';
10+
11+
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
12+
\Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
13+
);
14+
$items = array_values($collection->getItems());
15+
16+
/** @var Coupon $coupon */
17+
$coupon = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Coupon::class);
18+
$coupon->setRuleId($items[0]->getId())
19+
->setCode('one_usage')
20+
->setType(0)
21+
->setUsageLimit(1)
22+
->save();
23+
24+
$coupon = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Coupon::class);
25+
$coupon->setRuleId($items[1]->getId())
26+
->setCode('one_usage_per_customer')
27+
->setType(0)
28+
->setUsagePerCustomer(1)
29+
->save();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\SalesRule\Model\Coupon;
7+
8+
$couponCodes = [
9+
'one_usage',
10+
'one_usage_per_customer',
11+
];
12+
13+
/** @var Coupon $coupon */
14+
$coupon = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Coupon::class);
15+
16+
foreach ($couponCodes as $couponCode) {
17+
$coupon->loadByCode($couponCode);
18+
$coupon->delete();
19+
}
20+
21+
require_once 'rules_rollback.php';

0 commit comments

Comments
 (0)