Skip to content
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed a bug where inactive carts’ search index rows weren’t being purged. ([#4344](https://github.com/craftcms/commerce/issues/4344))
- Fixed a bug where a transfer field layout would delete the order field layout. ([#4345](https://github.com/craftcms/commerce/issues/4345))
- Fixed a bug where generating a PDF or cart-load URL from a console request returned a blank URL. ([#4343](https://github.com/craftcms/commerce/pull/4343))
- Fixed a bug where completed orders could unintentionally have their recalculation mode set to "all". ([#4342](https://github.com/craftcms/commerce/issues/4342))
- Fixed [low-severity](https://github.com/craftcms/cms/security/policy#severity--remediation) business logic vulnerability. (GHSA-gcqr-xrx9-grcf)

## 5.7.1 - 2026-07-22
Expand Down
7 changes: 5 additions & 2 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1782,8 +1782,11 @@ public function updateOrderPaidInformation(): void
$this->trigger(self::EVENT_AFTER_ORDER_AUTHORIZED);
}

// restore recalculation lock state
$this->setRecalculationMode($originalRecalculationMode);
// Restore the original recalculation mode, unless this call completed the order
// a completed order must stay locked at `RECALCULATION_MODE_NONE` rather than reverting to its cart mode.
if (!$this->isCompleted) {
$this->setRecalculationMode($originalRecalculationMode);
}
}

/**
Expand Down
115 changes: 115 additions & 0 deletions tests/unit/adjusters/ShippingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craftcommercetests\unit\adjusters;

use Codeception\Test\Unit;
use craft\base\Event;
use craft\commerce\adjusters\Shipping;
use craft\commerce\elements\Order;
use craft\commerce\events\RegisterAvailableShippingMethodsEvent;
use craft\commerce\models\LineItem;
use craft\commerce\models\ShippingMethod;
use craft\commerce\services\ShippingMethods;
use UnitTester;

/**
* ShippingTest
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
*/
class ShippingTest extends Unit
{
/**
* @var UnitTester
*/
protected UnitTester $tester;

/**
* Toggled mid-test to simulate the registration handler matching the
* order, then failing to on a later call.
*
* @var bool
*/
private bool $_thirdPartyMethodMatches = true;

/**
* @inheritdoc
*/
protected function _before(): void
{
parent::_before();

$this->_thirdPartyMethodMatches = true;

// Simulate a third-party plugin registering a shipping method by
// re-evaluating live availability on every call, instead of
// returning a static, persisted method.
Event::on(
ShippingMethods::class,
ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS,
function(RegisterAvailableShippingMethodsEvent $event) {
$shippingMethods = $event->getShippingMethods();
$shippingMethods->push($this->make(ShippingMethod::class, [
// Plugins must set this themselves on methods they register -
// `Order::getAvailableShippingMethodOptions()` silently drops any
// `ShippingMethod` whose `storeId` doesn't match the order's.
'storeId' => $event->order->storeId,
'handle' => 'thirdPartyFlatRate',
'name' => 'Third Party Flat Rate',
'getIsEnabled' => true,
'getMatchingShippingRule' => fn(Order $order) => null,
'getPriceForOrder' => fn(Order $order) => 8.99,
'matchOrder' => fn(Order $order) => $this->_thirdPartyMethodMatches,
]));
}
);
}

/**
* @inheritdoc
*/
protected function _after(): void
{
Event::off(ShippingMethods::class, ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS);

parent::_after();
}

/**
* Confirms `Shipping::adjust()` correctly drops the adjustment (no error)
* when a registered method stops matching. Correct cart behaviour, and
* not, by itself, a bug - see
* {@see \craftcommercetests\unit\elements\order\OrderRecalculationTest}
* for the recalculation-lock bug this scenario was originally written to
* demonstrate.
*/
public function testAdjusterDropsAdjustmentWhenMethodDoesNotMatch(): void
{
$lineItem = $this->make(LineItem::class, [
'id' => 1,
'qty' => 1,
'price' => 50,
'getIsShippable' => true,
]);

$order = new Order();
$order->shippingMethodHandle = 'thirdPartyFlatRate';
$order->setLineItems([$lineItem]);

$adjuster = new Shipping();

$firstPass = $adjuster->adjust($order);
self::assertCount(1, $firstPass, 'Shipping adjustment should be present while the method matches.');
self::assertEquals(8.99, $firstPass[0]->amount);

$this->_thirdPartyMethodMatches = false;

$secondPass = $adjuster->adjust($order);
self::assertSame([], $secondPass, 'No adjustment, no exception - this part is expected.');
}
}
Loading
Loading