Skip to content

Commit

Permalink
Merge branch '5.x' into 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 11, 2024
2 parents 74e87d9 + 2427860 commit e2989b9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

# Unreleased

- Fixed a PHP error that could occur when saving a discount. ([#3538](https://github.com/craftcms/commerce/issues/3538))

## 5.0.9 - 2024-06-05

- Product Title fields are no longer shown when “Show the Title field” is disabled and there’s a validation error on the `title` attribute. ([craftcms/cms#13876](https://github.com/craftcms/cms/issues/13876))
Expand Down
9 changes: 9 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@
use craft\controllers\UsersController;
use craft\debug\Module;
use craft\elements\Address;
use craft\elements\db\UserQuery;
use craft\elements\User as UserElement;
use craft\enums\CmsEdition;
use craft\events\CancelableEvent;
use craft\events\DefineBehaviorsEvent;
use craft\events\DefineConsoleActionsEvent;
use craft\events\DefineEditUserScreensEvent;
Expand Down Expand Up @@ -735,6 +737,13 @@ function(DefineBehaviorsEvent $event) {
}
);

Event::on(UserQuery::class, UserQuery::EVENT_AFTER_PREPARE, function(CancelableEvent $event) {
/** @var UserQuery $sender */
$sender = $event->sender;
$sender->query->addSelect(['commerce_customers.primaryBillingAddressId']);
$sender->query->leftJoin(Table::CUSTOMERS . ' commerce_customers', '[[commerce_customers.customerId]] = [[users.id]]');
});

// Add Commerce info to user edit screen
Event::on(UsersController::class, UsersController::EVENT_DEFINE_EDIT_SCREENS, function(DefineEditUserScreensEvent $event) {
$event->screens[CommerceUsersController::SCREEN_COMMERCE] = ['label' => Craft::t('commerce', 'Commerce')];
Expand Down
11 changes: 11 additions & 0 deletions src/behaviors/CustomerBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use craft\commerce\records\Customer;
use craft\elements\Address;
use craft\elements\User;
use craft\events\DefineRulesEvent;
use craft\events\ModelEvent;
use craft\helpers\ArrayHelper;
use RuntimeException;
Expand Down Expand Up @@ -88,9 +89,19 @@ public function events(): array
{
return [
User::EVENT_AFTER_SAVE => 'afterSaveUserHandler',
User::EVENT_DEFINE_RULES => 'defineRules',
];
}

/**
* @param DefineRulesEvent $event
* @throws InvalidConfigException
*/
public function defineRules(DefineRulesEvent $event): void
{
$event->rules[] = [['primaryBillingAddressId', 'primaryShippingAddressId'], 'safe'];
}

/**
* @param ModelEvent $event
* @return void
Expand Down
19 changes: 19 additions & 0 deletions src/elements/conditions/orders/DiscountOrderCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ class DiscountOrderCondition extends OrderCondition implements HasStoreInterface
{
use StoreTrait;

/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['storeId'], 'safe'];

return $rules;
}

/**
* @return array
*/
protected function config(): array
{
return array_merge(parent::config(), $this->toArray(['storeId']));
}

/**
* @inheritdoc
*/
Expand Down
36 changes: 29 additions & 7 deletions src/services/PaymentCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
*/
class PaymentCurrencies extends Component
{
/**
* @var null|Collection<PaymentCurrency>[]
*/
private ?array $_allPaymentCurrencies = null;

/**
* Get payment currency by its ID.
*
Expand All @@ -65,14 +70,31 @@ public function getAllPaymentCurrencies(?int $storeId = null): Collection
{
$storeId = $storeId ?? Plugin::getInstance()->getStores()->getCurrentStore()->id;

$rows = $this->_createPaymentCurrencyQuery()
->orderBy(['iso' => SORT_ASC])
->where(['storeId' => $storeId])
->all();
if ($this->_allPaymentCurrencies === null || !isset($this->_allPaymentCurrencies[$storeId])) {
$results = $this->_createPaymentCurrencyQuery()
->orderBy(['iso' => SORT_ASC])
->where(['storeId' => $storeId])
->all();

return Collection::make($rows)->map(function($row) {
return new PaymentCurrency($row);
});
if ($this->_allPaymentCurrencies === null) {
$this->_allPaymentCurrencies = [];
}

foreach ($results as $result) {
$paymentCurrency = Craft::createObject([
'class' => PaymentCurrency::class,
'attributes' => $result,
]);

if (!isset($this->_allPaymentCurrencies[$paymentCurrency->storeId])) {
$this->_allPaymentCurrencies[$paymentCurrency->storeId] = collect();
}

$this->_allPaymentCurrencies[$paymentCurrency->storeId]->push($paymentCurrency);
}
}

return $this->_allPaymentCurrencies[$storeId] ?? collect();
}

/**
Expand Down

0 comments on commit e2989b9

Please sign in to comment.