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

WIP discount performance improvements #3221

Merged
merged 14 commits into from
Aug 17, 2023
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
- Fixed a bug where address changes weren’t being synced to carts using them as a source. ([#3178](https://github.com/craftcms/commerce/issues/3178))
- Removed the htmx option from the`commerce/example-templates` command.
- Removed the color option from the`commerce/example-templates` command.
- Improved performance of discount recalculation.
- Added `craft\commerce\models\Discount::hasOrderCondition()`.
- Added `craft\commerce\models\Discount::hasCustomerCondition()`.
- Added `craft\commerce\models\Discount::hasBillingAddressCondition()`.
- Added `craft\commerce\models\Discount::hasShippingAddressCondition()`.
- Deprecated `craft\commerce\elements\Order::setEmail()`. `Order::setCustomer()` should be used instead.

## 4.2.11 - 2023-06-05
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public string $schemaVersion = '4.2.5';
public string $schemaVersion = '4.2.6';

/**
* @inheritdoc
Expand Down
49 changes: 49 additions & 0 deletions src/migrations/m230719_082348_discount_nullable_conditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace craft\commerce\migrations;

use craft\commerce\db\Table;
use craft\commerce\elements\conditions\addresses\DiscountAddressCondition;
use craft\commerce\elements\conditions\customers\DiscountCustomerCondition;
use craft\commerce\elements\conditions\orders\DiscountOrderCondition;
use craft\db\Migration;
use craft\helpers\Json;

/**
* m230719_082348_discount_nullable_conditions migration.
*/
class m230719_082348_discount_nullable_conditions extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$orderCondition = new DiscountOrderCondition();
$orderConditionConfig = Json::encode($orderCondition->getConfig());

$this->update(Table::DISCOUNTS, ['orderCondition' => null], ['orderCondition' => $orderConditionConfig], [], false);

$customerCondition = new DiscountCustomerCondition();
$customerConditionConfig = Json::encode($customerCondition->getConfig());

$this->update(Table::DISCOUNTS, ['customerCondition' => null], ['customerCondition' => $customerConditionConfig], [], false);

$addressCondition = new DiscountAddressCondition();
$addressConditionConfig = Json::encode($addressCondition->getConfig());

$this->update(Table::DISCOUNTS, ['billingAddressCondition' => null], ['billingAddressCondition' => $addressConditionConfig], [], false);
$this->update(Table::DISCOUNTS, ['shippingAddressCondition' => null], ['shippingAddressCondition' => $addressConditionConfig], [], false);

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m230719_082348_discount_nullable_conditions cannot be reverted.\n";
return false;
}
}
117 changes: 116 additions & 1 deletion src/models/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,31 @@ public function getOrderCondition(): ElementConditionInterface
return $condition;
}

/**
* @return bool
* @since 4.3.0
*/
public function hasOrderCondition(): bool
{
if ($this->_orderCondition === null) {
return false;
}

return !empty($this->getOrderCondition()->getConditionRules());
}

/**
* @param ElementConditionInterface|string|array $condition
* @return void
* @throws InvalidConfigException
*/
public function setOrderCondition(ElementConditionInterface|string|array $condition): void
{
if (empty($condition)) {
$this->_orderCondition = null;
return;
}

if (is_string($condition)) {
$condition = Json::decodeIfJson($condition);
}
Expand Down Expand Up @@ -316,11 +335,30 @@ public function getCustomerCondition(): ElementConditionInterface
}

/**
* @param ElementConditionInterface|string|array $condition
* @return bool
* @since 4.3.0
*/
public function hasCustomerCondition(): bool
{
if ($this->_customerCondition === null) {
return false;
}

return !empty($this->getCustomerCondition()->getConditionRules());
}

/**
* @param ElementConditionInterface|string $condition
* @return void
* @throws InvalidConfigException
*/
public function setCustomerCondition(ElementConditionInterface|string|array $condition): void
{
if (empty($condition)) {
$this->_customerCondition = null;
return;
}

if (is_string($condition)) {
$condition = Json::decodeIfJson($condition);
}
Expand Down Expand Up @@ -348,12 +386,31 @@ public function getShippingAddressCondition(): ElementConditionInterface
return $condition;
}

/**
* @return bool
* @since 4.3.0
*/
public function hasShippingAddressCondition(): bool
{
if ($this->_shippingAddressCondition === null) {
return false;
}

return !empty($this->getShippingAddressCondition()->getConditionRules());
}

/**
* @param ElementConditionInterface|string|array $condition
* @return void
* @throws InvalidConfigException
*/
public function setShippingAddressCondition(ElementConditionInterface|string|array $condition): void
{
if (empty($condition)) {
$this->_shippingAddressCondition = null;
return;
}

if (is_string($condition)) {
$condition = Json::decodeIfJson($condition);
}
Expand Down Expand Up @@ -381,12 +438,31 @@ public function getBillingAddressCondition(): ElementConditionInterface
return $condition;
}

/**
* @return bool
* @since 4.3.0
*/
public function hasBillingAddressCondition(): bool
{
if ($this->_billingAddressCondition === null) {
return false;
}

return !empty($this->getBillingAddressCondition()->getConditionRules());
}

/**
* @param ElementConditionInterface|string|array $condition
* @return void
* @throws InvalidConfigException
*/
public function setBillingAddressCondition(ElementConditionInterface|string|array $condition): void
{
if (empty($condition)) {
$this->_billingAddressCondition = null;
return;
}

if (is_string($condition)) {
$condition = Json::decodeIfJson($condition);
}
Expand Down Expand Up @@ -556,6 +632,45 @@ function($attribute) {
}
},
],
[[
'id',
'allPurchasables',
'allCategories',
'purchasableIds',
'categoryIds',
'name',
'description',
'couponFormat',
'orderCondition',
'customerCondition',
'billingAddressCondition',
'shippingAddressCondition',
'perUserLimit',
'perEmailLimit',
'totalDiscountUseLimit',
'totalDiscountUses',
'dateFrom',
'dateTo',
'purchaseTotal',
'orderConditionFormula',
'purchaseQty',
'maxPurchaseQty',
'baseDiscount',
'baseDiscountType',
'perItemDiscount',
'percentDiscount',
'percentageOffSubject',
'excludeOnSale',
'hasFreeShippingForMatchingItems',
'hasFreeShippingForOrder',
'categoryRelationshipType',
'enabled',
'stopProcessing',
'dateCreated',
'dateUpdated',
'ignoreSales',
'appliedTo',
], 'safe'],
];
}

Expand Down
Loading