Skip to content

Commit f226bfa

Browse files
mglamanbojanz
authored andcommitted
Issue #2861664 by mglaman: Order assignment should generate a log to mark the customer conversion (#680)
1 parent d975c7e commit f226bfa

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

modules/log/commerce_log.commerce_log_templates.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ order_canceled:
2323
category: commerce_order
2424
label: 'Order canceled'
2525
template: '<p>The order was canceled.</p>'
26+
order_assigned:
27+
category: commerce_order
28+
label: 'Order assigned'
29+
template: '<p>The order was assigned to {{ user }}.</p>'

modules/log/src/EventSubscriber/OrderEventSubscriber.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\commerce_log\EventSubscriber;
44

5+
use Drupal\commerce_order\Event\OrderAssignEvent;
56
use Drupal\Core\Entity\EntityTypeManagerInterface;
67
use Drupal\state_machine\Event\WorkflowTransitionEvent;
78
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -34,6 +35,7 @@ public static function getSubscribedEvents() {
3435
'commerce_order.validate.pre_transition' => ['onValidateTransition', -100],
3536
'commerce_order.fulfill.pre_transition' => ['onFulfillTransition', -100],
3637
'commerce_order.cancel.pre_transition' => ['onCancelTransition', -100],
38+
'commerce_order.order.assign' => ['onOrderAssign', -100],
3739
];
3840
return $events;
3941
}
@@ -86,4 +88,17 @@ public function onCancelTransition(WorkflowTransitionEvent $event) {
8688
$this->logStorage->generate($order, 'order_canceled')->save();
8789
}
8890

91+
/**
92+
* Creates a log when an order is assigned.
93+
*
94+
* @param \Drupal\commerce_order\Event\OrderAssignEvent $event
95+
* The order assign event.
96+
*/
97+
public function onOrderAssign(OrderAssignEvent $event) {
98+
$order = $event->getOrder();
99+
$this->logStorage->generate($order, 'order_assigned', [
100+
'user' => $event->getAccount()->getDisplayName(),
101+
])->save();
102+
}
103+
89104
}

modules/log/tests/src/Kernel/OrderIntegrationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Drupal\Tests\commerce_log\Kernel;
44

55
use Drupal\commerce_order\Entity\Order;
6+
use Drupal\commerce_order\Entity\OrderInterface;
67
use Drupal\commerce_order\Entity\OrderType;
78
use Drupal\commerce_price\Price;
89
use Drupal\commerce_product\Entity\Product;
910
use Drupal\commerce_product\Entity\ProductVariation;
1011
use Drupal\commerce_product\Entity\ProductVariationType;
1112
use Drupal\profile\Entity\Profile;
1213
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
14+
use Drupal\user\Entity\User;
1315

1416
/**
1517
* Tests integration with order events.
@@ -185,4 +187,25 @@ public function testPlaceValidateFulfillLogs() {
185187
$this->assertText('The order was fulfilled.');
186188
}
187189

190+
/**
191+
* Tests that an order assignment log is generated.
192+
*/
193+
public function testOrderAssignedLog() {
194+
// Reassignment is currently only done on user login.
195+
$this->order->setCustomer(User::getAnonymousUser());
196+
$this->order->setRefreshState(OrderInterface::REFRESH_SKIP);
197+
$this->order->save();
198+
$new_user = $this->createUser();
199+
200+
$order_assignment = $this->container->get('commerce_order.order_assignment');
201+
$order_assignment->assign($this->order, $new_user);
202+
203+
$logs = $this->logStorage->loadByEntity($this->order);
204+
$this->assertEquals(1, count($logs));
205+
$log = reset($logs);
206+
$build = $this->logViewBuilder->view($log);
207+
$this->render($build);
208+
$this->assertText("The order was assigned to {$new_user->getDisplayName()}.");
209+
}
210+
188211
}

0 commit comments

Comments
 (0)