Skip to content

Commit

Permalink
Merge branch '5.x' into 5.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/translations/nl/commerce.php
  • Loading branch information
nfourtythree committed Sep 3, 2024
2 parents 0bacac1 + 74b11b7 commit a295069
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 32 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@
### System
- Craft Commerce now requires Craft CMS 5.2 or later.

## Unreleased

- Fixed a SQL error that could occur when updating Commerce.

## 5.0.18 - 2024-08-28

- Fixed a PHP error that could occur when default addresses were set on a cart. ([#3641](https://github.com/craftcms/commerce/issues/3641))
- Fixed a bug were the “Auto Set New Cart Addresses” store setting was not persisting when saved.

## 5.0.17 - 2024-08-21

- Fixed a bug where variant indexes weren’t displaying promotion prices as currency values.
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,14 +1000,14 @@ private function _registerCacheTypes(): void
try {
FileHelper::createDirectory($path);
} catch (\Exception $e) {
Craft::error($e->getMessage());
Craft::$app->getErrorHandler()->logException($e);
}

Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, static function(RegisterCacheOptionsEvent $e) use ($path) {
try {
FileHelper::createDirectory($path);
} catch (\Exception $e) {
Craft::error($e->getMessage());
Craft::$app->getErrorHandler()->logException($e);
}

$e->options[] = [
Expand Down
4 changes: 3 additions & 1 deletion src/base/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,16 +987,18 @@ public function afterSave(bool $isNew): void
// added to inventory before it is saved as a permanent variant.
if ($purchasableId) {
// Set the inventory item data
/** @var InventoryItem|null $inventoryItem */
$inventoryItem = InventoryItemRecord::find()->where(['purchasableId' => $purchasableId])->one();
if (!$inventoryItem) {
$inventoryItem = new InventoryItemRecord();
$inventoryItem->purchasableId = $purchasableId;
$inventoryItem->countryCodeOfOrigin = '';
$inventoryItem->administrativeAreaCodeOfOrigin = '';
$inventoryItem->harmonizedSystemCode = '';
$inventoryItem->save();
}

$inventoryItem->save();
$this->inventoryItemId = $inventoryItem->id;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1486,28 +1486,28 @@ private function _updateOrder(Order $order, $orderRequestData, bool $tryAutoSet

$order->autoSetAddresses();
} else {
$getAddress = static function($address, $orderId, $title) {
if ($address && ($address['id'] && ($address['ownerId'] != $orderId || isset($address['_copy'])))) {
$getAddress = static function($address, Order $order, $title) {
if ($address && ($address['id'] && ($address['ownerId'] != $order->id || isset($address['_copy'])))) {
if (isset($address['_copy'])) {
unset($address['_copy']);
}
$address = Craft::$app->getElements()->getElementById($address['id'], Address::class);
$address = Craft::$app->getElements()->duplicateElement($address, [
'ownerId' => $orderId,
'primaryOwnerId' => $orderId,
'owner' => $order,
'primaryOwner' => $order,
'title' => $title,
]);
} elseif ($address && ($address['id'] && $address['ownerId'] == $orderId)) {
} elseif ($address && ($address['id'] && $address['ownerId'] == $order->id)) {
/** @var Address|null $address */
$address = Address::find()->ownerId($address['ownerId'])->id($address['id'])->one();
}

return $address;
};
$billingAddress = $getAddress($submittedBillingAddress, $orderRequestData['order']['id'], Craft::t('commerce', 'Billing Address'));
$billingAddress = $getAddress($submittedBillingAddress, $order, Craft::t('commerce', 'Billing Address'));
$order->setBillingAddress($billingAddress);

$shippingAddress = $getAddress($submittedShippingAddress, $orderRequestData['order']['id'], Craft::t('commerce', 'Shipping Address'));
$shippingAddress = $getAddress($submittedShippingAddress, $order, Craft::t('commerce', 'Shipping Address'));
$order->setShippingAddress($shippingAddress);

if (isset($orderRequestData['order']['sourceBillingAddressId'])) {
Expand Down
14 changes: 10 additions & 4 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1617,8 +1617,8 @@ public function autoSetAddresses(): bool
if (!$this->_shippingAddress && !$this->shippingAddressId && $primaryShippingAddress = $user->getPrimaryShippingAddress()) {
$this->sourceShippingAddressId = $primaryShippingAddress->id;
$shippingAddress = Craft::$app->getElements()->duplicateElement($primaryShippingAddress, [
'ownerId' => $this->id,
'primaryOwnerId' => $this->id,
'owner' => $this,
'primaryOwner' => $this,
]);
$this->setShippingAddress($shippingAddress);
$autoSetOccurred = true;
Expand All @@ -1627,8 +1627,8 @@ public function autoSetAddresses(): bool
if (!$this->_billingAddress && !$this->billingAddressId && $primaryBillingAddress = $user->getPrimaryBillingAddress()) {
$this->sourceBillingAddressId = $primaryBillingAddress->id;
$billingAddress = Craft::$app->getElements()->duplicateElement($primaryBillingAddress, [
'ownerId' => $this->id,
'primaryOwnerId' => $this->id,
'owner' => $this,
'primaryOwner' => $this,
]);
$this->setBillingAddress($billingAddress);
$autoSetOccurred = true;
Expand Down Expand Up @@ -3059,6 +3059,12 @@ public function getOrderAdjustments(): array
*/
public function setAdjustments(array $adjustments): void
{
$this->_orderAdjustments = [];

foreach ($adjustments as $adjustment) {
$adjustment->setOrder($this);
}

$this->_orderAdjustments = $adjustments;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ public function getExclusiveQueryParams(): array
*/
public function modifyQuery(Query $query): void
{
if (!$this->customerId) {
return;
}
return;

// $query->andWhere(['purchasableId' => $ids]);
// Doesn't modify the query as the modification
// of the query happens in `CatalogPricingCondition::modifyQuery()` for this rule
}
}
2 changes: 1 addition & 1 deletion src/elements/traits/OrderValidatorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function validatePaymentSourceId(string $attribute, $params, Validator $v
// this will confirm the payment source is valid and belongs to the orders customer
$this->getPaymentSource();
} catch (InvalidConfigException $e) {
Craft::error($e->getMessage());
Craft::$app->getErrorHandler()->logException($e);
$validator->addError($this, $attribute, Craft::t('commerce', 'Invalid payment source ID: {value}'));
}
}
Expand Down
38 changes: 37 additions & 1 deletion src/migrations/m221122_055725_multi_store.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,42 @@ public function safeUp(): bool
$this->addColumn(Table::STORES, 'primary', $this->boolean()->defaultValue(false)->notNull());
}

$config = ['name' => 'Primary Store', 'handle' => 'primaryStore', 'primary' => true];
$config = [
'name' => 'Primary Store',
'handle' => 'primaryStore',
'primary' => true,
];

$this->update(table: Table::STORES,
columns: $config,
condition: ['id' => $storeSettings['id']],
updateTimestamp: false
);

$configKeys = [
'name',
'handle',
'primary',
'allowCheckoutWithoutPayment',
'allowEmptyCartOnCheckout',
'allowPartialPaymentOnCheckout',
'autoSetCartShippingMethodOption',
'autoSetNewCartAddresses',
'autoSetPaymentSource',
'freeOrderPaymentStrategy',
'minimumTotalPriceStrategy',
'orderReferenceFormat',
'requireBillingAddressAtCheckout',
'requireShippingAddressAtCheckout',
'requireShippingMethodSelectionAtCheckout',
'useBillingAddressForTax',
'validateOrganizationTaxIdAsVatId',
];

$config = (new Query())
->select($configKeys)
->from([Table::STORES])
->one();

$this->update(table: Table::STORES,
columns: $config,
Expand All @@ -75,6 +110,7 @@ public function safeUp(): bool
->from([Table::STORES])
->scalar();


// Make project config updates
$projectConfig = Craft::$app->getProjectConfig();

Expand Down
3 changes: 2 additions & 1 deletion src/migrations/m231019_110814_update_variant_ownership.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\db\Migration;
use craft\db\Query;
use craft\db\Table as CraftTable;
use yii\db\Expression;

/**
* m231019_110814_update_variant_ownership migration.
Expand All @@ -22,7 +23,7 @@ public function safeUp(): bool
->select([
'id as elementId',
'productId as ownerId',
'sortOrder',
new Expression('CASE WHEN [[sortOrder]] is NULL THEN 1 ELSE [[sortOrder]] END as [[sortOrder]]'),
])
->from([Table::VARIANTS])
->all();
Expand Down
2 changes: 1 addition & 1 deletion src/models/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function getConfig(): array
'allowEmptyCartOnCheckout' => $this->getAllowEmptyCartOnCheckout(false),
'allowPartialPaymentOnCheckout' => $this->getAllowPartialPaymentOnCheckout(false),
'autoSetCartShippingMethodOption' => $this->getAutoSetCartShippingMethodOption(false),
'autoSetNewCartAddresses' => $this->getAutoSetCartShippingMethodOption(false),
'autoSetNewCartAddresses' => $this->getAutoSetNewCartAddresses(false),
'autoSetPaymentSource' => $this->getAutoSetPaymentSource(false),
'freeOrderPaymentStrategy' => $this->getFreeOrderPaymentStrategy(false),
'handle' => $this->handle,
Expand Down
14 changes: 8 additions & 6 deletions src/services/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ private function _saveAddressesFromOrder(Order $order): void
$newSourceShippingAddressId = $newAddress->id;
} else {
if ($saveBillingAddress) {
$newBillingAddress = Craft::$app->getElements()->duplicateElement(
$order->getBillingAddress(),
$newBillingAddress = Craft::$app->getElements()->duplicateElement($order->getBillingAddress(),
[
'primaryOwner' => $order->getCustomer(),
'owner' => $order->getCustomer(),
Expand Down Expand Up @@ -354,9 +353,9 @@ private function _saveAddressesFromOrder(Order $order): void
// Manually update the order DB record to avoid looped element saves
if ($newSourceBillingAddressId || $newSourceShippingAddressId) {
\craft\commerce\records\Order::updateAll([
'sourceBillingAddressId' => $order->sourceBillingAddressId,
'sourceShippingAddressId' => $order->sourceShippingAddressId,
],
'sourceBillingAddressId' => $order->sourceBillingAddressId,
'sourceShippingAddressId' => $order->sourceShippingAddressId,
],
[
'id' => $order->id,
]
Expand Down Expand Up @@ -394,7 +393,10 @@ private function _activateUserFromOrder(Order $order): void
}

if ($billingAddress || $shippingAddress) {
$newAttributes = ['primaryOwner' => $user];
$newAttributes = [
'owner' => $user,
'primaryOwner' => $user,
];

// If there is only one address make sure we don't add duplicates to the user
if ($order->hasMatchingAddresses()) {
Expand Down
12 changes: 10 additions & 2 deletions src/services/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,21 @@ public function afterSaveAddressHandler(ModelEvent $event): void
foreach ($carts as $cart) {
// Update the billing address
if ($cart->sourceBillingAddressId === $address->id) {
$newBillingAddress = Craft::$app->getElements()->duplicateElement($address, ['primaryOwner' => $cart, 'title' => Craft::t('commerce', 'Billing Address')]);
$newBillingAddress = Craft::$app->getElements()->duplicateElement($address, [
'primaryOwner' => $cart,
'owner' => $cart,
'title' => Craft::t('commerce', 'Billing Address'),
]);
$cart->billingAddressId = $newBillingAddress->id;
}

// Update the shipping address
if ($cart->sourceShippingAddressId === $address->id) {
$newShippingAddress = Craft::$app->getElements()->duplicateElement($address, ['primaryOwner' => $cart, 'title' => Craft::t('commerce', 'Shipping Address')]);
$newShippingAddress = Craft::$app->getElements()->duplicateElement($address, [
'primaryOwner' => $cart,
'owner' => $cart,
'title' => Craft::t('commerce', 'Shipping Address'),
]);
$cart->shippingAddressId = $newShippingAddress->id;
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function processPayment(Order $order, BasePaymentForm $form, ?string &$re
$this->_saveTransaction($transaction);
}

Craft::error($e->getMessage());
Craft::$app->getErrorHandler()->logException($e);
throw new PaymentException($e->getMessage(), $e->getCode(), $e);
}
}
Expand Down Expand Up @@ -550,7 +550,7 @@ private function _capture(Transaction $parent): Transaction
$child->message = $e->getMessage();
$this->_saveTransaction($child);

Craft::error($e->getMessage());
Craft::$app->getErrorHandler()->logException($e);
}

return $child;
Expand Down

0 comments on commit a295069

Please sign in to comment.