Skip to content

Commit 005e904

Browse files
vasikebojanz
authored andcommitted
Issue #2867046 by vasike, bojanz: Don't run order processors if the order has no items
1 parent b9ef8d0 commit 005e904

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

Diff for: modules/order/src/OrderRefresh.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public function needsRefresh(OrderInterface $order) {
126126
* {@inheritdoc}
127127
*/
128128
public function refresh(OrderInterface $order) {
129+
$current_time = $this->time->getCurrentTime();
130+
$order->setChangedTime($current_time);
129131
// Do not remove adjustments added in the user interface.
130132
$adjustments = $order->getAdjustments();
131133
foreach ($adjustments as $key => $adjustment) {
@@ -134,6 +136,10 @@ public function refresh(OrderInterface $order) {
134136
}
135137
}
136138
$order->setAdjustments($adjustments);
139+
// Nothing else can be done while the order is empty.
140+
if (!$order->hasItems()) {
141+
return;
142+
}
137143

138144
$context = new Context($order->getCustomer(), $order->getStore());
139145
foreach ($order->getItems() as $order_item) {
@@ -152,13 +158,11 @@ public function refresh(OrderInterface $order) {
152158
$processor->process($order);
153159
}
154160

155-
$current_time = $this->time->getCurrentTime();
156161
// @todo Evaluate which order items have changed.
157162
foreach ($order->getItems() as $order_item) {
158163
$order_item->setChangedTime($current_time);
159164
$order_item->save();
160165
}
161-
$order->setChangedTime($current_time);
162166
}
163167

164168
}
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Drupal\Tests\commerce_promotion\Kernel;
4+
5+
use Drupal\commerce_price\Price;
6+
use Drupal\commerce_product\Entity\Product;
7+
use Drupal\commerce_product\Entity\ProductVariation;
8+
use Drupal\commerce_promotion\Entity\Promotion;
9+
use Drupal\Tests\commerce_cart\Kernel\CartManagerTestTrait;
10+
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
11+
12+
/**
13+
* Tests the integration between promotions and carts.
14+
*
15+
* @group commerce
16+
*/
17+
class PromotionCartTest extends CommerceKernelTestBase {
18+
19+
use CartManagerTestTrait;
20+
21+
/**
22+
* The cart manager.
23+
*
24+
* @var \Drupal\commerce_cart\CartManager
25+
*/
26+
protected $cartManager;
27+
28+
/**
29+
* The cart provider.
30+
*
31+
* @var \Drupal\commerce_cart\CartProvider
32+
*/
33+
protected $cartProvider;
34+
35+
/**
36+
* Modules to enable.
37+
*
38+
* @var array
39+
*/
40+
public static $modules = [
41+
'entity_reference_revisions',
42+
'profile',
43+
'state_machine',
44+
'commerce_order',
45+
'path',
46+
'commerce_product',
47+
'commerce_promotion',
48+
];
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
protected function setUp() {
54+
parent::setUp();
55+
56+
$this->installEntitySchema('profile');
57+
$this->installEntitySchema('commerce_order');
58+
$this->installEntitySchema('commerce_order_item');
59+
$this->installEntitySchema('commerce_promotion');
60+
$this->installEntitySchema('commerce_product_variation');
61+
$this->installEntitySchema('commerce_product');
62+
$this->installConfig([
63+
'profile',
64+
'commerce_order',
65+
'commerce_product',
66+
'commerce_promotion',
67+
]);
68+
$this->installSchema('commerce_promotion', ['commerce_promotion_usage']);
69+
}
70+
71+
/**
72+
* Tests adding a product with a promotion to the cart.
73+
*/
74+
public function testPromotionCart() {
75+
$this->installCommerceCart();
76+
77+
$variation = ProductVariation::create([
78+
'type' => 'default',
79+
'sku' => strtolower($this->randomMachineName()),
80+
'price' => [
81+
'number' => '10.00',
82+
'currency_code' => 'USD',
83+
],
84+
]);
85+
$variation->save();
86+
$product = Product::create([
87+
'type' => 'default',
88+
'title' => 'My product',
89+
'variations' => [$variation],
90+
]);
91+
$product->save();
92+
93+
$promotion = Promotion::create([
94+
'name' => 'Promotion test',
95+
'order_types' => ['default'],
96+
'stores' => [$this->store->id()],
97+
'offer' => [
98+
'target_plugin_id' => 'commerce_promotion_order_percentage_off',
99+
'target_plugin_configuration' => [
100+
'amount' => '0.10',
101+
],
102+
],
103+
'start_date' => '2017-01-01',
104+
'status' => TRUE,
105+
]);
106+
$promotion->save();
107+
108+
$user = $this->createUser();
109+
/** @var \Drupal\commerce_order\Entity\OrderInterface $cart_order */
110+
$cart = $this->cartProvider->createCart('default', $this->store, $user);
111+
$this->cartManager->addEntity($cart, $variation);
112+
113+
$this->assertEquals(1, count($cart->getAdjustments()));
114+
$this->assertEquals(new Price('9.00', 'USD'), $cart->getTotalPrice());
115+
116+
// Disable the promotion.
117+
$promotion->setEnabled(FALSE);
118+
$promotion->save();
119+
$this->container->get('commerce_order.order_refresh')->refresh($cart);
120+
$this->assertEmpty($cart->getAdjustments());
121+
$this->assertEquals(new Price('10.00', 'USD'), $cart->getTotalPrice());
122+
}
123+
124+
}

0 commit comments

Comments
 (0)