Skip to content

Commit

Permalink
Revert "Performance improvement order condition on shipping"
Browse files Browse the repository at this point in the history
This reverts commit 9fc1878.
  • Loading branch information
lukeholder committed Sep 4, 2024
1 parent 9fc1878 commit b92e1be
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/models/ShippingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ function($attribute) {
if (!$order) {
$order = new Order();
}
$orderAsArray = Plugin::getInstance()->getShippingMethods()->getSerializedOrderForMatchingRules($order);
$orderConditionParams = [
'order' => $orderAsArray,
'order' => $order->toArray([], ['lineItems.snapshot', 'shippingAddress', 'billingAddress']),
];
if (!Plugin::getInstance()->getFormulas()->validateConditionSyntax($this->{$attribute}, $orderConditionParams)) {
$this->addError($attribute, Craft::t('commerce', 'Invalid order condition syntax.'));
Expand Down Expand Up @@ -274,9 +273,10 @@ public function matchOrder(Order $order): bool
$lineItems = $order->getLineItems();

if ($this->orderConditionFormula) {
$orderAsArray = Plugin::getInstance()->getShippingMethods()->getSerializedOrderForMatchingRules($order);
$fieldsAsArray = $order->getSerializedFieldValues();
$orderAsArray = $order->toArray([], ['lineItems.snapshot', 'shippingAddress', 'billingAddress']);
$orderConditionParams = [
'order' => $orderAsArray,
'order' => array_merge($orderAsArray, $fieldsAsArray),
];
if (!Plugin::getInstance()->getFormulas()->evaluateCondition($this->orderConditionFormula, $orderConditionParams, 'Evaluate Shipping Rule Order Condition Formula')) {
return false;
Expand Down
27 changes: 0 additions & 27 deletions src/services/ShippingMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ class ShippingMethods extends Component
*/
private ?array $_allShippingMethods = null;

/**
* @var array
*/
private array $_serializedOrdersByNumber = [];

/**
* Returns the Commerce managed shipping methods stored in the database.
*
Expand Down Expand Up @@ -146,31 +141,9 @@ public function getMatchingShippingMethods(Order $order): array
$shippingMethods[$method->getHandle()] = $method; // Keep the key being the handle of the method for front-end use.
}

// Clear the memoized data so next time we watch to match rules, we get fresh data.
$this->_serializedOrdersByNumber = [];

return $shippingMethods;
}

/**
* Creates an order as an array for matching rules.
* We do this centrally here so that we can clear the memoized data centrally.
*
* @param Order $order
* @return array
*/
public function getSerializedOrderForMatchingRules(Order $order): array
{
if (isset($this->_serializedOrdersByNumber[$order->number])) {
return $this->_serializedOrdersByNumber[$order->number];
}

$fieldsAsArray = $order->getSerializedFieldValues();
$orderAsArray = $order->toArray([], ['lineItems.snapshot', 'shippingAddress', 'billingAddress']);
$this->_serializedOrdersByNumber[$order->number] = array_merge($orderAsArray, $fieldsAsArray);
return $this->_serializedOrdersByNumber[$order->number];
}

/**
* Get a matching shipping rule for Order and shipping method.
*
Expand Down

0 comments on commit b92e1be

Please sign in to comment.