Skip to content

Commit

Permalink
Merge branch 'main' into 5.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/elements/conditions/addresses/PostalCodeFormulaConditionRule.php
  • Loading branch information
lukeholder committed Apr 2, 2024
2 parents d0c2cef + 77a3164 commit 67c3008
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
45 changes: 45 additions & 0 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Throwable;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\mutex\Mutex;
use yii\web\BadRequestHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
Expand Down Expand Up @@ -52,6 +53,16 @@ class CartController extends BaseFrontEndController
*/
protected ?User $_currentUser = null;

/**
* @var Mutex|null
*/
private ?Mutex $_mutex = null;

/**
* @var string|null
*/
private ?string $_mutexLockName = null;

/**
* @throws InvalidConfigException
*/
Expand Down Expand Up @@ -92,10 +103,36 @@ public function actionUpdateCart(): ?Response
{
$this->requirePostRequest();
$isSiteRequest = $this->request->getIsSiteRequest();
$isConsoleRequest = $this->request->getIsConsoleRequest();
$currentUser = Craft::$app->getUser()->getIdentity();
/** @var Plugin $plugin */
$plugin = Plugin::getInstance();

$useMutex = (!$isConsoleRequest && Craft::$app->getRequest()->getBodyParam('number')) || (!$isConsoleRequest && $plugin->getCarts()->getHasSessionCartNumber());

if ($useMutex) {
$lockOrderNumber = null;
if ($bodyNumber = Craft::$app->getRequest()->getBodyParam('number')) {
$lockOrderNumber = $bodyNumber;
} elseif (!$isConsoleRequest) {
$request = Craft::$app->getRequest();
$requestCookies = $request->getCookies();
$cookieNumber = $requestCookies->getValue($plugin->getCarts()->cartCookie['name']);

if ($cookieNumber) {
$lockOrderNumber = $cookieNumber;
}
}

if ($lockOrderNumber) {
$this->_mutexLockName = "order:$lockOrderNumber";
$this->_mutex = Craft::$app->getMutex();
if (!$this->_mutex->acquire($this->_mutexLockName, 5)) {
throw new Exception('Unable to acquire a lock for saving of Order: ' . $lockOrderNumber);
}
}
}

// Get the cart from the request or from the session.
// When we are about to update the cart, we consider it a real cart at this point, and want to actually create it in the DB.
$this->_cart = $this->_getCart(true);
Expand Down Expand Up @@ -489,6 +526,10 @@ private function _returnCart(): ?Response
$error = Craft::t('commerce', 'Unable to update cart.');
$message = $this->request->getValidatedBodyParam('failMessage') ?? $error;

if ($this->_mutex && $this->_mutexLockName) {
$this->_mutex->release($this->_mutexLockName);
}

return $this->asModelFailure(
$this->_cart,
$message,
Expand All @@ -509,6 +550,10 @@ private function _returnCart(): ?Response
$this->_cartVariable => $this->_cart,
]);

if ($this->_mutex && $this->_mutexLockName) {
$this->_mutex->release($this->_mutexLockName);
}

return $this->asModelSuccess(
$this->_cart,
$message,
Expand Down
34 changes: 34 additions & 0 deletions src/controllers/PaymentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ public function actionPay(): ?Response

$number = $this->request->getParam('number');

$useMutex = $number || (!$isCpRequest && $plugin->getCarts()->getHasSessionCartNumber());

if ($useMutex) {
$lockOrderNumber = null;
if ($number) {
$lockOrderNumber = $number;
} elseif (!$isCpRequest) {
$request = Craft::$app->getRequest();
$requestCookies = $request->getCookies();
$cookieNumber = $requestCookies->getValue($plugin->getCarts()->cartCookie['name']);

if ($cookieNumber) {
$lockOrderNumber = $cookieNumber;
}
}

if ($lockOrderNumber) {
$lockName = "order:$lockOrderNumber";
$mutex = Craft::$app->getMutex();
if (!$mutex->acquire($lockName, 5)) {
throw new Exception('Unable to acquire a lock for saving of Order: ' . $lockOrderNumber);
}
}
}


if ($number !== null) {
$order = $plugin->getOrders()->getOrderByNumber($number);

Expand Down Expand Up @@ -375,6 +401,10 @@ public function actionPay(): ?Response

$error = Craft::t('commerce', 'Something changed with the order before payment, please review your order and submit payment again.');

if ($useMutex && isset($mutex, $lockName)) {
$mutex->release($lockName);
}

return $this->asModelFailure(
$paymentForm,
$error,
Expand All @@ -390,6 +420,10 @@ public function actionPay(): ?Response
}
}

if ($useMutex && isset($mutex, $lockName)) {
$mutex->release($lockName);
}

$redirect = '';
$redirectData = [];
$transaction = null;
Expand Down
6 changes: 6 additions & 0 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,12 @@ private function _saveAdjustments(): void
$previousAdjustment->delete();
}
}

// Make sure all other adjustments have been cleaned up.
Db::delete(
Table::ORDERADJUSTMENTS,
['and', ['orderId' => $this->id], ['not', ['id' => $newAdjustmentIds]]]
);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
class PostalCodeFormulaConditionRule extends BaseTextConditionRule implements ElementConditionRuleInterface
{

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -77,14 +76,14 @@ public function operators(): array
public function inputHtml(): string
{
return Html::hiddenLabel($this->getLabel(), 'value') .
Cp::textareaHtml([
'type' => $this->inputType(),
'id' => 'value',
'name' => 'value',
'code' => 'value',
'value' => $this->value,
'autocomplete' => false,
'class' => 'fullwidth code',
]);
Cp::textareaHtml([
'type' => $this->inputType(),
'id' => 'value',
'name' => 'value',
'code' => 'value',
'value' => $this->value,
'autocomplete' => false,
'class' => 'fullwidth code',
]);
}
}

0 comments on commit 67c3008

Please sign in to comment.