Skip to content

Commit

Permalink
Issue #2867046 by vasike, bojanz: Don't run order processors if the o…
Browse files Browse the repository at this point in the history
…rder has no items
  • Loading branch information
vasike authored and bojanz committed Apr 21, 2017
1 parent b9ef8d0 commit 1028fc1
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
8 changes: 6 additions & 2 deletions modules/order/src/OrderRefresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function needsRefresh(OrderInterface $order) {
* {@inheritdoc}
*/
public function refresh(OrderInterface $order) {
$current_time = $this->time->getCurrentTime();
$order->setChangedTime($current_time);
// Do not remove adjustments added in the user interface.
$adjustments = $order->getAdjustments();
foreach ($adjustments as $key => $adjustment) {
Expand All @@ -134,6 +136,10 @@ public function refresh(OrderInterface $order) {
}
}
$order->setAdjustments($adjustments);
// Nothing else can be done while the order is emoty.
if (!$order->hasItems()) {
return;
}

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

$current_time = $this->time->getCurrentTime();
// @todo Evaluate which order items have changed.
foreach ($order->getItems() as $order_item) {
$order_item->setChangedTime($current_time);
$order_item->save();
}
$order->setChangedTime($current_time);
}

}
124 changes: 124 additions & 0 deletions modules/promotion/tests/src/Kernel/PromotionCartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Drupal\Tests\commerce_promotion\Kernel;

use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\Tests\commerce_cart\Kernel\CartManagerTestTrait;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;

/**
* Tests the integration between promotions and carts.
*
* @group commerce
*/
class PromotionCartTest extends CommerceKernelTestBase {

use CartManagerTestTrait;

/**
* The cart manager.
*
* @var \Drupal\commerce_cart\CartManager
*/
protected $cartManager;

/**
* The cart provider.
*
* @var \Drupal\commerce_cart\CartProvider
*/
protected $cartProvider;

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'path',
'commerce_product',
'commerce_promotion',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->installEntitySchema('profile');
$this->installEntitySchema('commerce_order');
$this->installEntitySchema('commerce_order_item');
$this->installEntitySchema('commerce_promotion');
$this->installEntitySchema('commerce_product_variation');
$this->installEntitySchema('commerce_product');
$this->installConfig([
'profile',
'commerce_order',
'commerce_product',
'commerce_promotion',
]);
$this->installSchema('commerce_promotion', ['commerce_promotion_usage']);
}

/**
* Tests adding a product with a promotion to the cart.
*/
public function testPromotionCart() {
$this->installCommerceCart();

$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this->randomMachineName()),
'price' => [
'number' => '10.00',
'currency_code' => 'USD',
],
]);
$variation->save();
$product = Product::create([
'type' => 'default',
'title' => 'My product',
'variations' => [$variation],
]);
$product->save();

$promotion = Promotion::create([
'name' => 'Promotion test',
'order_types' => ['default'],
'stores' => [$this->store->id()],
'offer' => [
'target_plugin_id' => 'commerce_promotion_order_percentage_off',
'target_plugin_configuration' => [
'amount' => '0.10',
],
],
'start_date' => '2017-01-01',
'status' => TRUE,
]);
$promotion->save();

$user = $this->createUser();
/** @var \Drupal\commerce_order\Entity\OrderInterface $cart_order */
$cart = $this->cartProvider->createCart('default', $this->store, $user);
$this->cartManager->addEntity($cart, $variation);

$this->assertEquals(1, count($cart->getAdjustments()));
$this->assertEquals(new Price('9.00', 'USD'), $cart->getTotalPrice());

// Disable the promotion.
$promotion->setEnabled(FALSE);
$promotion->save();
$this->container->get('commerce_order.order_refresh')->refresh($cart);
$this->assertEmpty($cart->getAdjustments());
$this->assertEquals(new Price('10.00', 'USD'), $cart->getTotalPrice());
}

}

0 comments on commit 1028fc1

Please sign in to comment.