Skip to content
Snippets Groups Projects
Commit 080ca52f authored by Bojan Živanović's avatar Bojan Živanović
Browse files

Issue #2823919 by bojanz: Improve order timestamp setting

parent 90a931ad
No related branches found
No related tags found
No related merge requests found
......@@ -39,22 +39,6 @@ function commerce_order_local_tasks_alter(&$definitions) {
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function commerce_order_commerce_order_presave(OrderInterface $order) {
$place_transition = $order->getState()->getWorkflow()->getTransition('place');
if (!$order->isNew() && empty($order->getPlacedTime()) && $place_transition) {
$from_states = $place_transition->getFromStates();
$from_state = reset($from_states);
$from_state = $from_state->getId();
$to_state = $place_transition->getToState()->getId();
if ($order->original->state->value == $from_state && $order->state->value == $to_state) {
$order->setPlacedTime(REQUEST_TIME);
}
}
}
/**
* Implements hook_field_widget_form_alter().
*
......
......@@ -25,3 +25,8 @@ services:
arguments: ['@commerce.availability_manager']
tags:
- { name: commerce_order.order_processor, priority: 100 }
commerce_order.timestamp_event_subscriber:
class: Drupal\commerce_order\EventSubscriber\TimestampEventSubscriber
tags:
- { name: event_subscriber }
......@@ -405,6 +405,15 @@ class Order extends ContentEntityBase implements OrderInterface {
}
}
// Maintain the completed timestamp.
$state = $this->getState()->value;
$original_state = isset($this->original) ? $this->original->getState()->value : '';
if ($state == 'completed' && $original_state != 'completed') {
if (empty($this->getCompletedTime())) {
$this->setCompletedTime(REQUEST_TIME);
}
}
// Refresh draft orders on every save.
if ($this->getState()->value == 'draft' && empty($this->getRefreshState())) {
$this->setRefreshState(self::REFRESH_ON_SAVE);
......
<?php
namespace Drupal\commerce_order\EventSubscriber;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TimestampEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.place.pre_transition' => 'onPlaceTransition',
];
return $events;
}
/**
* Sets the order's placed timestamp.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The transition event.
*/
public function onPlaceTransition(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event->getEntity();
if (empty($order->getPlacedTime())) {
$order->setPlacedTime(REQUEST_TIME);
}
}
}
......@@ -51,9 +51,9 @@ class OrderTest extends OrderBrowserTestBase {
}
/**
* Tests the generation of the 'placed' timestamp.
* Tests the generation of the 'placed' and 'completed' timestamps.
*/
public function testOrderPlaced() {
public function testOrderTimestamps() {
$order_item = $this->createEntity('commerce_order_item', [
'type' => 'default',
]);
......@@ -63,15 +63,15 @@ class OrderTest extends OrderBrowserTestBase {
'mail' => $this->loggedInUser->getEmail(),
'order_items' => [$order_item],
]);
$this->assertNull($order->getPlacedTime());
$order->save();
$this->assertNull($order->getPlacedTime());
// Transitioning the order out of the draft state should set the timestamp.
$this->assertNull($order->getCompletedTime());
// Transitioning the order out of the draft state should set the timestamps.
$transition = $order->getState()->getWorkflow()->getTransition('place');
$order->getState()->applyTransition($transition);
$order->save();
$this->assertEquals($order->getPlacedTime(), REQUEST_TIME);
$this->assertEquals($order->getCompletedTime(), REQUEST_TIME);
}
}
......@@ -126,6 +126,8 @@ class OrderTest extends EntityKernelTestBase {
* @covers ::setCreatedTime
* @covers ::getPlacedTime
* @covers ::setPlacedTime
* @covers ::getCompletedTime
* @covers ::setCompletedTime
*/
public function testOrder() {
$profile = Profile::create([
......@@ -244,6 +246,9 @@ class OrderTest extends EntityKernelTestBase {
$order->setPlacedTime(635879800);
$this->assertEquals(635879800, $order->getPlacedTime());
$order->setCompletedTime(635879900);
$this->assertEquals(635879900, $order->getCompletedTime());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment