Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- `\craft\commerce\services\LineItems::saveLineItem` and `\craft\commerce\services\OrderAdjustments::saveOrderAdjustment` no longer throws an exception when a line item is not found. Instead, it returns `false` and logs an error. ([#3901](https://github.com/craftcms/commerce/pull/3901))
- Fixed a PHP error that could occur when sending emails. ([#4017](https://github.com/craftcms/commerce/issues/4017))

## 5.3.13 - 2025-05-21
Expand All @@ -10,6 +11,7 @@
- Fixed a bug where discounts’ related entries weren’t being returned for non-primary sites. ([#4010](https://github.com/craftcms/commerce/issues/4010))
- Fixed a bug where Edit Order pages weren’t displaying the “Send Email” button.


## 5.3.12 - 2025-05-07

- Fixed a PHP error that could occur when calculating discount adjustments. ([#3997](https://github.com/craftcms/commerce/issues/3997))
Expand Down
12 changes: 0 additions & 12 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use craft\commerce\Plugin;
use craft\commerce\records\LineItem as LineItemRecord;
use craft\commerce\records\Order as OrderRecord;
use craft\commerce\records\OrderAdjustment as OrderAdjustmentRecord;
use craft\commerce\records\OrderNotice as OrderNoticeRecord;
use craft\commerce\records\Transaction as TransactionRecord;
use craft\db\Query;
Expand Down Expand Up @@ -3609,11 +3608,6 @@ public function getMetadata(): array
*/
private function _saveAdjustments(): void
{
/** @var null|array|OrderAdjustmentRecord[] $previousAdjustments */
$previousAdjustments = OrderAdjustmentRecord::find()
->where(['orderId' => $this->id])
->all();

$newAdjustmentIds = [];

foreach ($this->getAdjustments() as $adjustment) {
Expand All @@ -3623,12 +3617,6 @@ private function _saveAdjustments(): void
$adjustment->orderId = $this->id;
}

foreach ($previousAdjustments as $previousAdjustment) {
if (!in_array($previousAdjustment->id, $newAdjustmentIds, false)) {
$previousAdjustment->delete();
}
}

// Make sure all other adjustments have been cleaned up.
Db::delete(
Table::ORDERADJUSTMENTS,
Expand Down
9 changes: 4 additions & 5 deletions src/services/LineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,19 @@ public function resolveCustomLineItem(Order $order, string $sku, array $options
*
* @param LineItem $lineItem The line item to save.
* @param bool $runValidation Whether the Line Item should be validated.
* @throws Throwable
*/
public function saveLineItem(LineItem $lineItem, bool $runValidation = true): bool
{
$isNewLineItem = !$lineItem->id;

if (!$lineItem->id) {
if ($isNewLineItem) {
$lineItemRecord = new LineItemRecord();
} else {
$lineItemRecord = LineItemRecord::findOne($lineItem->id);

if (!$lineItemRecord) {
throw new Exception(Craft::t('commerce', 'No line item exists with the ID “{id}”',
['id' => $lineItem->id]));
Craft::info('Line Item ID:' . $lineItem->id . ' does not exist and can not be saved.', __METHOD__);
return false;
}
}

Expand All @@ -263,7 +262,7 @@ public function saveLineItem(LineItem $lineItem, bool $runValidation = true): bo
}

if ($runValidation && !$lineItem->validate()) {
Craft::info('Line item not saved due to validation error.', __METHOD__);
Craft::info('Line Item not saved due to validation error(s).', __METHOD__);
return false;
}

Expand Down
16 changes: 8 additions & 8 deletions src/services/OrderAdjustments.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use craft\helpers\Json;
use Throwable;
use yii\base\Component;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\StaleObjectException;

Expand Down Expand Up @@ -146,23 +145,24 @@ public function getAllOrderAdjustmentsByOrderId(int $orderId): array
* Save an order adjustment.
*
* @param bool $runValidation Whether the Order Adjustment should be validated
* @throws Exception
*/
public function saveOrderAdjustment(OrderAdjustment $orderAdjustment, bool $runValidation = true): bool
{
if ($orderAdjustment->id) {
$newAdjustment = !$orderAdjustment->id;

if ($newAdjustment) {
$record = new OrderAdjustmentRecord();
} else {
$record = OrderAdjustmentRecord::findOne($orderAdjustment->id);

if (!$record) {
throw new Exception(Craft::t('commerce', 'No order Adjustment exists with the ID “{id}”',
['id' => $orderAdjustment->id]));
Craft::info('Order Adjustment ID:' . $orderAdjustment->id . ' does not exist and can not be saved.', __METHOD__);
return false;
}
} else {
$record = new OrderAdjustmentRecord();
}

if ($runValidation && !$orderAdjustment->validate()) {
Craft::info('Order Adjustment not saved due to validation error.', __METHOD__);
Craft::info('Order Adjustment not saved due to validation error(s).', __METHOD__);
return false;
}

Expand Down
Loading