diff --git a/uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php b/uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php index 8aa3069..3f26aa8 100644 --- a/uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php +++ b/uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php @@ -102,16 +102,12 @@ class CustomerInfoPane extends CheckoutPanePluginBase { * {@inheritdoc} */ public function process(OrderInterface $order, array $form, FormStateInterface $form_state) { - $user = \Drupal::currentUser(); - $cart_config = \Drupal::config('uc_cart.settings'); + if (\Drupal::currentUser()->isAnonymous()) { + $cart_config = \Drupal::config('uc_cart.settings'); - $pane = $form_state->getValue(['panes', 'customer']); - $order->setEmail($pane['primary_email']); + $pane = $form_state->getValue(['panes', 'customer']); + $order->setEmail($pane['primary_email']); - if ($user->isAuthenticated()) { - $order->setOwnerId($user->id()); - } - else { // Check if the email address is already taken. $mail_taken = (bool) \Drupal::entityQuery('user') ->condition('mail', $pane['primary_email']) diff --git a/uc_cart/src/Tests/CartCheckoutTest.php b/uc_cart/src/Tests/CartCheckoutTest.php index 8157a9f..880e148 100644 --- a/uc_cart/src/Tests/CartCheckoutTest.php +++ b/uc_cart/src/Tests/CartCheckoutTest.php @@ -19,7 +19,11 @@ class CartCheckoutTest extends UbercartTestBase { public static $modules = array('uc_payment', 'uc_payment_pack'); - /** Authenticated but unprivileged user. */ + /** + * Authenticated but unprivileged user. + * + * @var \Drupal\user\Entity\User + */ protected $customer; /** @@ -213,9 +217,11 @@ class CartCheckoutTest extends UbercartTestBase { public function testAuthenticatedCheckout() { $this->drupalLogin($this->customer); $this->addToCart($this->product); - $this->checkout(); + $order = $this->checkout(); $this->assertRaw('Your order is complete!'); $this->assertRaw('While logged in'); + $this->assertEqual($order->getOwnerId(), $this->customer->id(), 'Order has the correct user ID.'); + $this->assertEqual($order->getEmail(), $this->customer->getEmail(), 'Order has the correct email address.'); // Check that cart is now empty. $this->drupalGet('cart'); diff --git a/uc_order/src/Entity/Order.php b/uc_order/src/Entity/Order.php index 19282b6..6076251 100644 --- a/uc_order/src/Entity/Order.php +++ b/uc_order/src/Entity/Order.php @@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\uc_order\OrderInterface; use Drupal\uc_store\Address; +use Drupal\user\Entity\User; use Drupal\user\UserInterface; /** @@ -88,6 +89,30 @@ class Order extends ContentEntityBase implements OrderInterface { /** * {@inheritdoc} */ + public static function preCreate(EntityStorageInterface $storage, array &$values) { + parent::preCreate($storage, $values); + + // Set default values. + $store_config = \Drupal::config('uc_store.settings'); + $values += [ + 'order_status' => uc_order_state_default('in_checkout'), + 'currency' => $store_config->get('currency.code'), + 'billing_country' => $store_config->get('address.country'), + 'delivery_country' => $store_config->get('address.country'), + 'created' => REQUEST_TIME, + ]; + + // Take the primary email address from the user, if necessary. + if (empty($values['primary_email']) && !empty($values['uid'])) { + if ($account = User::load($values['uid'])) { + $values['primary_email'] = $account->getEmail(); + } + } + } + + /** + * {@inheritdoc} + */ public function preSave(EntityStorageInterface $storage) { $this->order_total->value = $this->getTotal(); $this->product_count->value = $this->getProductCount(); @@ -472,7 +497,7 @@ class Order extends ContentEntityBase implements OrderInterface { ->setLabel(t('Customer')) ->setDescription(t('The user that placed the order.')) ->setSetting('target_type', 'user') - ->setSetting('default_value', 0); + ->setDefaultValueCallback('Drupal\uc_order\Entity\Order::getCurrentUserId'); $fields['order_status'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Order status')) ->setDescription(t('The uc_order_status entity ID indicating the order status')) @@ -604,4 +629,16 @@ class Order extends ContentEntityBase implements OrderInterface { return $fields; } + /** + * Default value callback for 'uid' base field definition. + * + * @see ::baseFieldDefinitions() + * + * @return array + * An array of default values. + */ + public static function getCurrentUserId() { + return array(\Drupal::currentUser()->id()); + } + } diff --git a/uc_order/src/OrderStorage.php b/uc_order/src/OrderStorage.php index cc6264d..332fcfc 100644 --- a/uc_order/src/OrderStorage.php +++ b/uc_order/src/OrderStorage.php @@ -8,7 +8,6 @@ namespace Drupal\uc_order; use Drupal\Core\Entity\Sql\SqlContentEntityStorage; -use Drupal\user\Entity\User; /** * Controller class for orders. @@ -18,45 +17,6 @@ class OrderStorage extends SqlContentEntityStorage { /** * {@inheritdoc} */ - public function create(array $values = array()) { - $store_config = \Drupal::config('uc_store.settings'); - - // Set the primary email address. - if (empty($values['primary_email']) && !empty($values['uid'])) { - if ($account = User::load($values['uid'])) { - $values['primary_email'] = $account->mail; - } - } - - // Set the default order status. - if (empty($values['order_status'])) { - $values['order_status'] = uc_order_state_default('in_checkout'); - } - - // Set the default currency. - if (empty($values['currency'])) { - $values['currency'] = $store_config->get('currency.code'); - } - - // Set the default country codes. - if (empty($values['billing_country'])) { - $values['billing_country'] = $store_config->get('address.country'); - } - if (empty($values['delivery_country'])) { - $values['delivery_country'] = $store_config->get('address.country'); - } - - // Set the created time to now. - if (empty($values['created'])) { - $values['created'] = REQUEST_TIME; - } - - return parent::create($values); - } - - /** - * {@inheritdoc} - */ public function getSchema() { $schema = parent::getSchema(); // @todo Create a numeric field type and use that instead. diff --git a/uc_store/src/Tests/UbercartTestBase.php b/uc_store/src/Tests/UbercartTestBase.php index 20536b6..bb58427 100644 --- a/uc_store/src/Tests/UbercartTestBase.php +++ b/uc_store/src/Tests/UbercartTestBase.php @@ -240,6 +240,9 @@ abstract class UbercartTestBase extends WebTestBase { /** * Executes the checkout process. + * + * @return \Drupal\uc_order\Entity\Order|FALSE + * The created order, or FALSE if the order could not be created. */ protected function checkout($edit = []) { $this->drupalPostForm('cart', [], 'Checkout');