Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2906332: Order viewing messages #788

Open
wants to merge 5 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/checkout/commerce_checkout.module
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function commerce_checkout_theme() {
'commerce_checkout_completion_message' => [
'variables' => [
'order_entity' => NULL,
'completion_messages' => NULL,
'payment_instructions' => NULL,
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;

use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;

/**
Expand All @@ -15,14 +17,79 @@
*/
class CompletionMessage extends CheckoutPaneBase {

/**
* @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
*/
private $completionMessags;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->completionMessags = new CompletionMessages();
}

/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$this->preparePaneForm();

$pane_form['#theme'] = 'commerce_checkout_completion_message';
$pane_form['#order_entity'] = $this->order;
$pane_form['#completion_messages'] = $this->completionMessags;

return $pane_form;
}

/**
* Prepares the necessary data for the completion messages.
*/
public function preparePaneForm() {
$this->populateCompletionMessages();
}

/**
* Gets the completion messages.
*/
private function populateCompletionMessages() {
$this->populateCompletionMessagesWithAnonymousMessage();
$this->populateCompletionMessagesWithAuthenticatedMessage();

$this->allowOthersToModifyMessages();
}

/**
* Gets the default completion message.
*/
private function populateCompletionMessagesWithAnonymousMessage() {
if (\Drupal::currentUser()->isAnonymous()) {
$this->completionMessags->addMessage($this->t('Your order number is @number.', ['@number' => $this->order->id()]));
$this->completionMessags->addMessage($this->t('You can view your order on your account page when logged in.'));
}
}

/**
* Populate the completion messages with the logged in message.
*/
private function populateCompletionMessagesWithAuthenticatedMessage() {
if (\Drupal::currentUser()->isAuthenticated()) {
$this->completionMessags->addMessage(
$this->t(
'Your order number is %order_number_with_link.',
['%order_number_with_link' => $this->order->toLink($this->order->id())]
)
);
}
}

/**
* Allow other modules to alter the messages.
*/
private function allowOthersToModifyMessages() {
\Drupal::moduleHandler()
->alter('checkout_completion_messages', $this->completionMessags, $this->order);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;

use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Acts as a container to collect all completion messages.
*
* @package Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane
*/
class CompletionMessages implements \Iterator, \Countable {

/**
* @var \Drupal\Core\StringTranslation\TranslatableMarkup[]
*/
private $messages;

/**
* @var int
*/
private $position;

/**
* Sets up the position to 0.
*/
public function __construct() {
$this->position = 0;
}

/**
* Adds a message to the array.
*
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message
* The message to add.
*/
public function addMessage(TranslatableMarkup $message) {
$this->messages[] = $message;
}

/**
* Gets the current message.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The current message.
*/
public function current() {
return $this->messages[$this->position];
}

/**
* {@inheritdoc}
*/
public function next() {
++$this->position;
}

/**
* {@inheritdoc}
*/
public function key() {
return $this->position;
}

/**
* {@inheritdoc}
*/
public function valid() {
return isset($this->messages[$this->position]);
}

/**
* {@inheritdoc}
*/
public function rewind() {
$this->position = 0;
}

/**
* {@inheritdoc}
*/
public function count() {
return count($this->messages);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
#}
<div class="checkout-complete">
{% for message in completion_messages %}
{{ message }} <br/>
{% endfor %}

{{ 'Your order number is @number.'|t({'@number': order_entity.getOrderNumber}) }} <br>
{{ 'You can view your order on your account page when logged in.'|t }} <br>

Expand Down
4 changes: 2 additions & 2 deletions modules/checkout/tests/src/Functional/CheckoutOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testGuestOrderCheckout() {
$this->assertSession()->pageTextContains('Billing information');
$this->assertSession()->pageTextContains('Order Summary');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');
$this->assertSession()->pageTextContains('0 items');
// Test second order.
$this->drupalGet($this->product->toUrl()->toString());
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testGuestOrderCheckout() {
$this->assertCheckoutProgressStep('Review');

$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 2. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 2.');
$this->assertSession()->pageTextContains('0 items');
}

Expand Down
52 changes: 52 additions & 0 deletions modules/checkout/tests/src/Kernel/CompletionMessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\Tests\commerce_checkout\Kernel;

use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;

/**
* Tests the completion messages class.
*
* @covers \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
*
* @group commerce
*/
class CompletionMessagesTest extends CommerceKernelTestBase {

/**
* @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
*/
private $completionMessages;

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->completionMessages = new CompletionMessages();
}

/**
* Tests add message method.
*/
public function testAddMessage() {
$this->completionMessages->addMessage(t('Message 1'));
$this->completionMessages->addMessage(t('Message 2'));

$this->assertCount(2, $this->completionMessages);
}

/**
* Tests the messages iterator.
*/
public function testMessagesIterator() {
$this->completionMessages->addMessage(t('Message 1'));
$this->completionMessages->addMessage(t('Message 2'));

$this->assertEquals('Message 1', $this->completionMessages->current()->render());
$this->completionMessages->next();
$this->assertEquals('Message 2', $this->completionMessages->current()->render());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function testCheckoutWithExistingPaymentMethod() {
$this->assertSession()->pageTextContains('Frederick Pabst');
$this->assertSession()->pageTextContains('Pabst Blue Ribbon Dr');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');

$order = Order::load(1);
$this->assertEquals('onsite', $order->get('payment_gateway')->target_id);
Expand Down Expand Up @@ -317,7 +317,7 @@ public function testCheckoutWithNewPaymentMethod() {
$this->assertSession()->pageTextContains('Johnny Appleseed');
$this->assertSession()->pageTextContains('123 New York Drive');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');

$order = Order::load(1);
$this->assertEquals('onsite', $order->get('payment_gateway')->target_id);
Expand Down Expand Up @@ -360,7 +360,7 @@ public function testCheckoutWithDeclinedPaymentMethod() {
$this->assertSession()->pageTextContains('Visa ending in 1111');
$this->assertSession()->pageTextContains('Expires 2/2020');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextNotContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextNotContains('Your order number is 1.');
$this->assertSession()->pageTextContains('We encountered an error processing your payment method. Please verify your details and try again.');
$this->assertSession()->addressEquals('checkout/1/order_information');

Expand Down Expand Up @@ -393,7 +393,7 @@ public function testCheckoutWithOffsiteRedirectPost() {
$this->assertSession()->pageTextContains('Johnny Appleseed');
$this->assertSession()->pageTextContains('123 New York Drive');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');
$order = Order::load(1);
$this->assertEquals('offsite', $order->get('payment_gateway')->target_id);

Expand Down Expand Up @@ -440,7 +440,7 @@ public function testCheckoutWithOffsiteRedirectGet() {
$this->assertSession()->pageTextContains('Johnny Appleseed');
$this->assertSession()->pageTextContains('123 New York Drive');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');
$order = Order::load(1);
$this->assertEquals('offsite', $order->get('payment_gateway')->target_id);

Expand Down Expand Up @@ -483,7 +483,7 @@ public function testFailedCheckoutWithOffsiteRedirectGet() {
$this->assertSession()->pageTextContains('Johnny FAIL');
$this->assertSession()->pageTextContains('123 New York Drive');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextNotContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextNotContains('Your order number is 1.');
$this->assertSession()->pageTextContains('We encountered an unexpected error processing your payment. Please try again later.');
$this->assertSession()->addressEquals('checkout/1/order_information');

Expand Down Expand Up @@ -516,7 +516,7 @@ public function testCheckoutWithManual() {
$this->assertSession()->pageTextContains('Johnny Appleseed');
$this->assertSession()->pageTextContains('123 New York Drive');
$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');
$this->assertSession()->pageTextContains('Sample payment instructions.');
$order = Order::load(1);
$this->assertEquals('manual', $order->get('payment_gateway')->target_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function testCheckout() {
$this->assertSession()->pageTextContains('$899.10');

$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');

$order_storage = $this->container->get('entity_type.manager')->getStorage('commerce_order');
$order_storage->resetCache([$this->cart->id()]);
Expand All @@ -259,7 +259,7 @@ public function testCheckoutWithMainSubmit() {
$this->assertSession()->pageTextContains('$899.10');

$this->submitForm([], 'Pay and complete purchase');
$this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this->assertSession()->pageTextContains('Your order number is 1.');

$order_storage = $this->container->get('entity_type.manager')->getStorage('commerce_order');
$order_storage->resetCache([$this->cart->id()]);
Expand Down