Skip to content

Commit

Permalink
Issue #2808813: New user account should own previous guest checkout o…
Browse files Browse the repository at this point in the history
…rder(s).
  • Loading branch information
steveoliver authored and Shean Hoxie committed Mar 27, 2018
1 parent 9276e9a commit 8565b2f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
37 changes: 37 additions & 0 deletions modules/order/commerce_order.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
* Defines the Order entity and associated features.
*/

use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\commerce_order\Plugin\Field\FieldFormatter\PriceCalculatedFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\entity\BundleFieldDefinition;
use Drupal\Core\Session\AccountInterface;

/**
* Implements hook_theme().
Expand Down Expand Up @@ -68,6 +71,40 @@ function commerce_order_field_formatter_info_alter(array &$info) {
$info['commerce_price_calculated']['provider'] = 'commerce_order';
}

/**
* Implements hook_ENTITY_TYPE_insert() for user entities.
*
* - Assigns any previous guest orders to new user account.
*/
function commerce_order_user_insert(AccountInterface $user) {
$order_ids = \Drupal::entityQuery('commerce_order')
->condition('mail', $user->getEmail())
->execute();
if ($order_ids) {
$orders = Order::loadMultiple($order_ids);
foreach ($orders as $order) {
$order->setCustomer($user);
$order->save();
}
}
}

/**
* 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().
*
Expand Down
33 changes: 33 additions & 0 deletions modules/order/tests/src/Functional/OrderAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\Tests\commerce_order\Functional;

use Drupal\commerce_order\Entity\Order;

/**
* Tests the order account (owner) functionality.
*
* @group commerce
*/
class OrderAccountTest extends OrderBrowserTestBase {

/**
* Tests assigning guest orders to newly created users (after checkout).
*/
public function testNewAccountOwnsOrderAfterGuestCheckout() {
$order_item = $this->createEntity('commerce_order_item', [
'type' => 'product_variation',
]);
$order = $this->createEntity('commerce_order', [
'type' => 'default',
'uid' => 0,
'mail' => '[email protected]',
'order_items' => [$order_item],
]);
$this->assertEmpty($order->getOwnerId(), 'The guest order has no owner account.');
$user = $this->createUser([], 'guest');
$order = Order::load($order->id());
$this->assertEquals($user->id(), $order->getOwnerId(), 'New user account owns previous guest order.');
}

}

0 comments on commit 8565b2f

Please sign in to comment.