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 committed Mar 20, 2017
1 parent e2f9909 commit 4492660
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 @@ -6,9 +6,12 @@
*/

use Drupal\commerce\BundleFieldDefinition;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;

/**
* Implements hook_theme().
Expand Down Expand Up @@ -56,6 +59,40 @@ function commerce_order_local_tasks_alter(&$definitions) {
}
}

/**
* 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 4492660

Please sign in to comment.