Skip to content

Commit d96c0e5

Browse files
authored
Issue #2855104 by mglaman, niko-: Orders should have a field containing the coupons that have been redeemed
1 parent 64064bf commit d96c0e5

4 files changed

Lines changed: 233 additions & 1 deletion

File tree

modules/promotion/commerce_promotion.module

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Provides a UI for managing promotions.
66
*/
77

8-
use Drupal\Core\Form\FormStateInterface;
8+
use Drupal\Core\Entity\EntityTypeInterface;
9+
use Drupal\Core\Field\BaseFieldDefinition;
910
use Drupal\Core\Render\Element;
1011

1112
/**
@@ -50,3 +51,31 @@ function template_preprocess_commerce_promotion(array &$variables) {
5051
$variables['promotion'][$key] = $variables['elements'][$key];
5152
}
5253
}
54+
55+
/**
56+
* Implements hook_entity_base_field_info().
57+
*/
58+
function commerce_promotion_entity_base_field_info(EntityTypeInterface $entity_type) {
59+
if ($entity_type->id() == 'commerce_order') {
60+
$fields['coupons'] = BaseFieldDefinition::create('entity_reference')
61+
->setLabel(t('Coupons'))
62+
->setDescription(t('Coupons which have been applied to order.'))
63+
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
64+
->setRequired(FALSE)
65+
->setSetting('target_type', 'commerce_promotion_coupon')
66+
->setSetting('handler', 'default')
67+
->setTranslatable(FALSE)
68+
->setDisplayOptions('form', [
69+
'type' => 'entity_reference_autocomplete',
70+
'weight' => 5,
71+
'settings' => [
72+
'match_operator' => 'CONTAINS',
73+
'size' => '60',
74+
'autocomplete_type' => 'tags',
75+
'placeholder' => '',
76+
],
77+
]);
78+
79+
return $fields;
80+
}
81+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Post update functions for Promotion.
6+
*/
7+
8+
/**
9+
* Adds coupons field to orders.
10+
*/
11+
function commerce_promotion_post_update_1() {
12+
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
13+
$order_definition = $entity_definition_update->getEntityType('commerce_order');
14+
$fields = commerce_promotion_entity_base_field_info($order_definition);
15+
$entity_definition_update->installFieldStorageDefinition('coupons', 'commerce_order', 'commerce_promotion', $fields['coupons']);
16+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Drupal\Tests\commerce_promotion\Kernel;
4+
5+
use Drupal\commerce_order\Entity\Order;
6+
use Drupal\commerce_order\Entity\OrderItemType;
7+
use Drupal\commerce_promotion\Entity\Coupon;
8+
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
9+
10+
/**
11+
* Tests coupon integration with orders.
12+
*
13+
* @group commerce
14+
*/
15+
class CouponOrderIntegrationTest extends CommerceKernelTestBase {
16+
17+
/**
18+
* The test order.
19+
*
20+
* @var \Drupal\commerce_order\Entity\OrderInterface
21+
*/
22+
protected $order;
23+
24+
/**
25+
* Modules to enable.
26+
*
27+
* @var array
28+
*/
29+
public static $modules = [
30+
'entity_reference_revisions',
31+
'profile',
32+
'state_machine',
33+
'commerce_order',
34+
'commerce_product',
35+
'commerce_promotion',
36+
];
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function setUp() {
42+
parent::setUp();
43+
44+
$this->installEntitySchema('profile');
45+
$this->installEntitySchema('commerce_order');
46+
$this->installEntitySchema('commerce_order_type');
47+
$this->installEntitySchema('commerce_order_item');
48+
$this->installEntitySchema('commerce_promotion');
49+
$this->installEntitySchema('commerce_promotion_coupon');
50+
$this->installConfig([
51+
'profile',
52+
'commerce_order',
53+
'commerce_promotion',
54+
]);
55+
56+
$this->user = $this->createUser();
57+
58+
OrderItemType::create([
59+
'id' => 'test',
60+
'label' => 'Test',
61+
'orderType' => 'default',
62+
])->save();
63+
64+
$this->order = Order::create([
65+
'type' => 'default',
66+
'state' => 'completed',
67+
'mail' => 'test@example.com',
68+
'ip_address' => '127.0.0.1',
69+
'order_number' => '6',
70+
'store_id' => $this->store,
71+
'uid' => $this->user,
72+
'order_items' => [],
73+
]);
74+
}
75+
76+
/**
77+
* Tests the coupons field added to orders.
78+
*/
79+
public function testOrderCouponField() {
80+
$coupon1 = Coupon::create([
81+
'code' => $this->randomString(),
82+
'status' => TRUE,
83+
]);
84+
$coupon1->save();
85+
$coupon2 = Coupon::create([
86+
'code' => $this->randomString(),
87+
'status' => TRUE,
88+
]);
89+
$coupon2->save();
90+
91+
$this->order->get('coupons')->appendItem($coupon1);
92+
$this->order->get('coupons')->appendItem($coupon2);
93+
$this->order->save();
94+
95+
$this->assertSame($coupon2, $this->order->get('coupons')->offsetGet(1)->entity);
96+
97+
}
98+
99+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Drupal\Tests\commerce_promotion\Kernel;
4+
5+
use Drupal\commerce_order\Entity\Order;
6+
use Drupal\commerce_order\Entity\OrderItemType;
7+
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
8+
9+
/**
10+
* Tests coupon field definition updated to orders.
11+
*
12+
* @group commerce
13+
*/
14+
class CouponsFieldPostUpdateTest extends CommerceKernelTestBase {
15+
16+
/**
17+
* The test order.
18+
*
19+
* @var \Drupal\commerce_order\Entity\OrderInterface
20+
*/
21+
protected $order;
22+
23+
/**
24+
* Modules to enable.
25+
*
26+
* @var array
27+
*/
28+
public static $modules = [
29+
'entity_reference_revisions',
30+
'profile',
31+
'state_machine',
32+
'commerce_order',
33+
'commerce_product',
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_order_item');
46+
$this->installConfig([
47+
'profile',
48+
'commerce_order',
49+
]);
50+
51+
$this->user = $this->createUser();
52+
53+
OrderItemType::create([
54+
'id' => 'test',
55+
'label' => 'Test',
56+
'orderType' => 'default',
57+
])->save();
58+
59+
$this->order = Order::create([
60+
'type' => 'default',
61+
'state' => 'completed',
62+
'mail' => 'test@example.com',
63+
'ip_address' => '127.0.0.1',
64+
'order_number' => '6',
65+
'store_id' => $this->store,
66+
'uid' => $this->user,
67+
'order_items' => [],
68+
]);
69+
$this->order->save();
70+
}
71+
72+
/**
73+
* Tests that commerce_promotion_post_update_1 works.
74+
*/
75+
public function testPostUpdate1() {
76+
$this->assertFalse($this->order->hasField('coupons'));
77+
78+
$this->installModule('commerce_promotion');
79+
$post_update_registry = $this->container->get('update.post_update_registry');
80+
foreach ($post_update_registry->getModuleUpdateFunctions('commerce_promotion') as $function) {
81+
$function();
82+
}
83+
84+
$this->order = $this->reloadEntity($this->order);
85+
$this->assertTrue($this->order->hasField('coupons'));
86+
}
87+
88+
}

0 commit comments

Comments
 (0)