Skip to content
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

[4.x] Shipping method performance issue due to order serialization. #3566

Closed
wants to merge 7 commits into from
Closed
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
32 changes: 27 additions & 5 deletions src/models/ShippingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ class ShippingRule extends Model implements ShippingRuleInterface
*/
private ?array $_shippingRuleCategories = null;

/**
* Holds the order serialized as an array used to pass to a twig condition formula.
*
* @var array
*/
private static array $_orderConditionParamsByMethodAndOrderNumber = [];

/**
* @throws InvalidConfigException
*/
Expand Down Expand Up @@ -273,11 +280,7 @@ public function matchOrder(Order $order): bool
$lineItems = $order->getLineItems();

if ($this->orderConditionFormula) {
$fieldsAsArray = $order->getSerializedFieldValues();
$orderAsArray = $order->toArray([], ['lineItems.snapshot', 'shippingAddress', 'billingAddress']);
$orderConditionParams = [
'order' => array_merge($orderAsArray, $fieldsAsArray),
];
$orderConditionParams = $this->_orderConditionParams($order);
if (!Plugin::getInstance()->getFormulas()->evaluateCondition($this->orderConditionFormula, $orderConditionParams, 'Evaluate Shipping Rule Order Condition Formula')) {
return false;
}
Expand Down Expand Up @@ -388,6 +391,25 @@ public function matchOrder(Order $order): bool
return true;
}

private function _orderConditionParams($order): array
{
if (isset(self::$_orderConditionParamsByMethodAndOrderNumber[$this->_getOrderConditionParamsCacheKey($order)])) {
return self::$_orderConditionParamsByMethodAndOrderNumber[$this->_getOrderConditionParamsCacheKey($order)];
}
$fieldsAsArray = $order->getSerializedFieldValues();
$orderAsArray = $order->toArray([], ['lineItems.snapshot', 'shippingAddress', 'billingAddress']);
self::$_orderConditionParamsByMethodAndOrderNumber[$this->_getOrderConditionParamsCacheKey($order)] = [
'order' => array_merge($orderAsArray, $fieldsAsArray),
];

return self::$_orderConditionParamsByMethodAndOrderNumber[$this->_getOrderConditionParamsCacheKey($order)];
}

public function _getOrderConditionParamsCacheKey($order): string
{
return $this->methodId . '-' . $order->number;
}

/**
* @return ShippingRuleCategory[]
* @throws InvalidConfigException
Expand Down
Loading