Skip to content

Issue #2867430 by vasike, bojanz: Order total summary formatter does not handle empty orders #711

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

Closed
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
31 changes: 31 additions & 0 deletions modules/order/commerce_order.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,34 @@ function commerce_order_post_update_6() {
}
}
}

/**
* Revert the 'commerce_order_item_table' view - empty text added.
*/
function commerce_order_post_update_7() {
/** @var \Drupal\commerce\Config\ConfigUpdaterInterface $config_updater */
$config_updater = \Drupal::service('commerce.config_updater');

$views = [
'views.view.commerce_order_item_table',
];
$result = $config_updater->revert($views, FALSE);

$success_results = $result->getSucceeded();
$failure_results = $result->getFailed();
if ($success_results) {
$message = t('Succeeded:') . '<br>';
foreach ($success_results as $success_message) {
$message .= $success_message . '<br>';
}
$message .= '<br>';
}
if ($failure_results) {
$message .= t('Failed:') . '<br>';
foreach ($failure_results as $failure_message) {
$message .= $failure_message . '<br>';
}
}

return $message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,16 @@ display:
sorts: { }
header: { }
footer: { }
empty: { }
empty:
area:
id: area
table: views
field: area
empty: true
content:
value: 'No items added to this order.'
format: plain_text
plugin_id: text
relationships: { }
arguments:
order_id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ public static function create(ContainerInterface $container, array $configuratio
);
}

/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL) {
// Check first if the total price is not empty.
if ($items->isEmpty()) {
return [];
}
return parent::view($items, $langcode);
}

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $items->getEntity();
return [
'#theme' => 'commerce_order_total_summary',
Expand Down
30 changes: 22 additions & 8 deletions modules/order/tests/src/Functional/OrderAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,12 @@ public function testUnlockOrder() {
* Tests that an admin can view an order's details.
*/
public function testAdminOrderView() {
$order_item = $this->createEntity('commerce_order_item', [
'type' => 'default',
'unit_price' => [
'number' => '999',
'currency_code' => 'USD',
],
]);
// First test for order without items.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $this->createEntity('commerce_order', [
'type' => 'default',
'store_id' => $this->store->id(),
'mail' => $this->loggedInUser->getEmail(),
'order_items' => [$order_item],
'state' => 'draft',
'uid' => $this->loggedInUser,
]);
Expand All @@ -214,6 +208,10 @@ public function testAdminOrderView() {
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->loggedInUser->getEmail());

// Test there are no items.
$this->assertSession()->pageTextContains('No items added to this order.');
$this->assertSession()->pageTextNotContains('Total');

// Confirm that the transition buttons are visible and functional.
$workflow = $order->getState()->getWorkflow();
$transitions = $workflow->getAllowedTransitions($order->getState()->value, $order);
Expand All @@ -224,6 +222,22 @@ public function testAdminOrderView() {
$this->assertSession()->buttonNotExists('Place order');
$this->assertSession()->buttonNotExists('Cancel order');

// Test with order items.
$order_item = $this->createEntity('commerce_order_item', [
'type' => 'default',
'unit_price' => [
'number' => '999',
'currency_code' => 'USD',
],
]);
$order->setItems([$order_item]);
$order->save();
$this->drupalGet($order->toUrl()->toString());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextNotContains('No items added to this order.');
$this->assertSession()->pageTextContains('$999.00');
$this->assertSession()->pageTextContains('Total price');

// Logout and check that anonymous users cannot see the order admin screen
// and receive a 403 error code.
$this->drupalLogout();
Expand Down