Skip to content

Commit 7ca6964

Browse files
mitrpakamglaman
authored andcommitted
Issue #2855815: Add custom coupon storage, provide loadByCode method.
1 parent 42796fc commit 7ca6964

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion;
4+
5+
use Drupal\commerce\CommerceContentEntityStorage;
6+
7+
/**
8+
* Defines the coupon storage.
9+
*/
10+
class CouponStorage extends CommerceContentEntityStorage implements CouponStorageInterface {
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function loadByCode($code) {
16+
$coupons = $this->loadByProperties(['code' => $code]);
17+
18+
return reset($coupons);
19+
}
20+
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion;
4+
5+
use Drupal\Core\Entity\ContentEntityStorageInterface;
6+
7+
/**
8+
* Defines the interface for coupon storage.
9+
*/
10+
interface CouponStorageInterface extends ContentEntityStorageInterface {
11+
12+
/**
13+
* Loads the coupon for the given coupon code.
14+
*
15+
* @param string $code
16+
* The coupon code.
17+
*
18+
* @return \Drupal\commerce_promotion\Entity\CouponInterface
19+
* The coupon.
20+
*/
21+
public function loadByCode($code);
22+
23+
}

modules/promotion/src/Entity/Coupon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* id = "commerce_promotion_coupon",
1414
* label = @Translation("Coupon"),
1515
* handlers = {
16+
* "storage" = "Drupal\commerce_promotion\CouponStorage",
1617
* "views_data" = "Drupal\views\EntityViewsData",
1718
* "form" = {
1819
* "add" = "Drupal\Core\Entity\ContentEntityForm",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Drupal\Tests\commerce_promotion\Kernel;
4+
5+
use Drupal\commerce_promotion\Entity\Coupon;
6+
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
7+
8+
/**
9+
* Tests coupon storage.
10+
*
11+
* @group commerce
12+
*/
13+
class CouponStorageTest extends CommerceKernelTestBase {
14+
15+
/**
16+
* The coupon storage.
17+
*
18+
* @var \Drupal\commerce_promotion\CouponStorageInterface
19+
*/
20+
protected $couponStorage;
21+
22+
/**
23+
* Modules to enable.
24+
*
25+
* @var array
26+
*/
27+
public static $modules = [
28+
'entity_reference_revisions',
29+
'profile',
30+
'state_machine',
31+
'commerce_order',
32+
'commerce_product',
33+
'commerce_promotion',
34+
];
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected function setUp() {
40+
parent::setUp();
41+
42+
$this->installEntitySchema('profile');
43+
$this->installEntitySchema('commerce_order');
44+
$this->installEntitySchema('commerce_order_type');
45+
$this->installEntitySchema('commerce_promotion');
46+
$this->installEntitySchema('commerce_promotion_coupon');
47+
$this->installConfig([
48+
'profile',
49+
'commerce_order',
50+
'commerce_promotion',
51+
]);
52+
53+
$this->couponStorage = $this->container->get('entity_type.manager')->getStorage('commerce_promotion_coupon');
54+
}
55+
56+
/**
57+
* Loads a coupon by its code.
58+
*/
59+
public function testLoadByCode() {
60+
$coupon_code = $this->randomMachineName();
61+
$coupon = Coupon::create([
62+
'code' => $coupon_code,
63+
'status' => TRUE,
64+
]);
65+
$coupon->save();
66+
67+
$coupon_loaded = $this->couponStorage->loadByCode($coupon_code);
68+
$this->assertEquals($coupon->id(), $coupon_loaded->id());
69+
}
70+
71+
}

0 commit comments

Comments
 (0)