-
Notifications
You must be signed in to change notification settings - Fork 174
Guard catalog pricing rule lookup during migrations #4309
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
Open
boboldehampsink
wants to merge
6
commits into
craftcms:5.x
Choose a base branch
from
boboldehampsink:fix/catalog-pricing-rules-migration-table-check
base: 5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−38
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc622ab
Guard catalog pricing rules query during migrations
boboldehampsink 9ed2fab
Guard Commerce element queries during migrations
boboldehampsink 4328f19
Guard purchasable detail columns during migrations
boboldehampsink cf7b800
Backfill donation purchasable records during migration
boboldehampsink 7051b0a
Remove redundant store table guard
boboldehampsink f382275
Address migration guard review feedback
boboldehampsink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -655,11 +655,15 @@ public function onPromotion(bool|null $value = true): static | |
| protected function afterPrepare(): bool | ||
| { | ||
| // Store dependent related joins to the sub query need to be done after the `elements_sites` is joined in the base `ElementQuery` class. | ||
| $this->subQuery->leftJoin(['sitestores' => Table::SITESTORES], '[[elements_sites.siteId]] = [[sitestores.siteId]]'); | ||
| $this->subQuery->leftJoin(['purchasables_stores' => Table::PURCHASABLES_STORES], '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| $hasStoreTables = $this->_storeTablesExist(); | ||
|
|
||
| if ($hasStoreTables) { | ||
| $this->subQuery->leftJoin(['sitestores' => Table::SITESTORES], '[[elements_sites.siteId]] = [[sitestores.siteId]]'); | ||
| $this->subQuery->leftJoin(['purchasables_stores' => Table::PURCHASABLES_STORES], '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| } | ||
|
|
||
| // Only do the extra catalog pricing query join if we have catalog pricing rules. | ||
| if (Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| if ($hasStoreTables && Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| $customerId = $this->forCustomer; | ||
| if ($customerId === null) { | ||
| $customerId = Craft::$app->getUser()->getIdentity()?->id; | ||
|
|
@@ -675,7 +679,9 @@ protected function afterPrepare(): bool | |
| $this->subQuery->leftJoin(['catalogprices' => $catalogPricesQuery], '[[catalogprices.purchasableId]] = [[commerce_purchasables.id]] AND [[catalogprices.storeId]] = [[sitestores.storeId]]'); | ||
| } | ||
|
|
||
| $this->subQuery->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| if (Craft::$app->getDb()->tableExists(Table::INVENTORYITEMS)) { | ||
| $this->subQuery->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| } | ||
|
|
||
| return parent::afterPrepare(); | ||
| } | ||
|
|
@@ -685,33 +691,73 @@ protected function afterPrepare(): bool | |
| */ | ||
| protected function beforePrepare(): bool | ||
| { | ||
| $hasStoreTables = $this->_storeTablesExist(); | ||
| $hasInventoryTable = Craft::$app->getDb()->tableExists(Table::INVENTORYITEMS); | ||
| $hasPurchasableDetailColumns = $this->_purchasableDetailColumnsExist(); | ||
|
|
||
| $this->joinElementTable('commerce_purchasables'); | ||
| $this->query->addSelect([ | ||
| 'commerce_purchasables.sku', | ||
| 'commerce_purchasables.width', | ||
| 'commerce_purchasables.height', | ||
| 'commerce_purchasables.length', | ||
| 'commerce_purchasables.weight', | ||
| 'commerce_purchasables.taxCategoryId', | ||
| 'purchasables_stores.availableForPurchase', | ||
| 'purchasables_stores.basePrice', | ||
| 'purchasables_stores.basePromotionalPrice', | ||
| 'purchasables_stores.freeShipping', | ||
| 'purchasables_stores.maxQty', | ||
| 'purchasables_stores.minQty', | ||
| 'purchasables_stores.inventoryTracked', | ||
| 'purchasables_stores.allowOutOfStockPurchases', | ||
| 'purchasables_stores.promotable', | ||
| 'purchasables_stores.shippingCategoryId', | ||
| 'inventoryitems.id as inventoryItemId', | ||
| ]); | ||
|
|
||
| $this->query->leftJoin(Table::SITESTORES . ' sitestores', '[[elements_sites.siteId]] = [[sitestores.siteId]]'); | ||
| $this->query->leftJoin(Table::PURCHASABLES_STORES . ' purchasables_stores', '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| $this->query->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| if ($hasPurchasableDetailColumns) { | ||
| $this->query->addSelect([ | ||
| 'commerce_purchasables.width', | ||
| 'commerce_purchasables.height', | ||
| 'commerce_purchasables.length', | ||
| 'commerce_purchasables.weight', | ||
| 'commerce_purchasables.taxCategoryId', | ||
| ]); | ||
| } else { | ||
| $this->query->addSelect([ | ||
| new Expression('NULL as [[width]]'), | ||
| new Expression('NULL as [[height]]'), | ||
| new Expression('NULL as [[length]]'), | ||
| new Expression('NULL as [[weight]]'), | ||
| new Expression('NULL as [[taxCategoryId]]'), | ||
| ]); | ||
| } | ||
|
|
||
| if ($hasStoreTables) { | ||
| $this->query->addSelect([ | ||
| 'purchasables_stores.availableForPurchase', | ||
| 'purchasables_stores.basePrice', | ||
| 'purchasables_stores.basePromotionalPrice', | ||
| 'purchasables_stores.freeShipping', | ||
| 'purchasables_stores.maxQty', | ||
| 'purchasables_stores.minQty', | ||
| 'purchasables_stores.inventoryTracked', | ||
| 'purchasables_stores.allowOutOfStockPurchases', | ||
| 'purchasables_stores.promotable', | ||
| 'purchasables_stores.shippingCategoryId', | ||
| ]); | ||
|
|
||
| $this->query->leftJoin(Table::SITESTORES . ' sitestores', '[[elements_sites.siteId]] = [[sitestores.siteId]]'); | ||
| $this->query->leftJoin(Table::PURCHASABLES_STORES . ' purchasables_stores', '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| } else { | ||
| $this->query->addSelect([ | ||
| new Expression('NULL as [[availableForPurchase]]'), | ||
| new Expression('NULL as [[basePrice]]'), | ||
| new Expression('NULL as [[basePromotionalPrice]]'), | ||
| new Expression('NULL as [[freeShipping]]'), | ||
| new Expression('NULL as [[maxQty]]'), | ||
| new Expression('NULL as [[minQty]]'), | ||
| new Expression('NULL as [[inventoryTracked]]'), | ||
| new Expression('NULL as [[allowOutOfStockPurchases]]'), | ||
| new Expression('NULL as [[promotable]]'), | ||
| new Expression('NULL as [[shippingCategoryId]]'), | ||
|
Comment on lines
+744
to
+748
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in f382275. The fallback selects |
||
| ]); | ||
| } | ||
|
|
||
| if ($hasInventoryTable) { | ||
| $this->query->addSelect(['inventoryitems.id as inventoryItemId']); | ||
| $this->query->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]'); | ||
| } else { | ||
| $this->query->addSelect([new Expression('NULL as [[inventoryItemId]]')]); | ||
| } | ||
|
|
||
| // Only do the extra catalog pricing query join if we have catalog pricing rules. | ||
| if (Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| if ($hasStoreTables && Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| $this->query->addSelect([ | ||
| 'subquery.price', | ||
| 'subquery.promotionalPrice as promotionalPrice', | ||
|
|
@@ -743,7 +789,7 @@ protected function beforePrepare(): bool | |
| if (isset($this->salePrice)) { | ||
| $this->subQuery->andWhere(Db::parseNumericParam('catalogprices.salePrice' , $this->salePrice)); | ||
| } | ||
| } else { | ||
| } elseif ($hasStoreTables) { | ||
| // If Catalog pricing rules are not being used | ||
| $this->query->addSelect([ | ||
| 'purchasables_stores.basePrice as price', | ||
|
|
@@ -777,6 +823,17 @@ protected function beforePrepare(): bool | |
| if (isset($this->salePrice)) { | ||
| $this->subQuery->andWhere(Db::parseNumericParam(new Expression('CASE WHEN [[purchasables_stores.basePromotionalPrice]] < [[purchasables_stores.basePrice]] THEN [[purchasables_stores.basePromotionalPrice]] ELSE [[purchasables_stores.basePrice]] END') , $this->salePrice)); | ||
| } | ||
| } else { | ||
| $this->query->addSelect([ | ||
| new Expression('NULL as [[price]]'), | ||
| new Expression('NULL as [[promotionalPrice]]'), | ||
| new Expression('NULL as [[salePrice]]'), | ||
| new Expression('NULL as [[catalogPricingRuleId]]'), | ||
| ]); | ||
|
|
||
| if ($this->_hasStoreTableParams()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (isset($this->sku)) { | ||
|
|
@@ -811,6 +868,10 @@ protected function beforePrepare(): bool | |
| } | ||
|
|
||
| if (isset($this->taxCategoryId)) { | ||
| if (!$hasPurchasableDetailColumns) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->taxCategoryId instanceof Query) { | ||
| $taxCategoryWhere = ['exists', $this->taxCategoryId]; | ||
| } else { | ||
|
|
@@ -821,6 +882,10 @@ protected function beforePrepare(): bool | |
| } | ||
|
|
||
| if ($this->width !== false) { | ||
| if (!$hasPurchasableDetailColumns) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->width === null) { | ||
| $this->subQuery->andWhere(['commerce_purchasables.width' => $this->width]); | ||
| } else { | ||
|
|
@@ -829,6 +894,10 @@ protected function beforePrepare(): bool | |
| } | ||
|
|
||
| if ($this->height !== false) { | ||
| if (!$hasPurchasableDetailColumns) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->height === null) { | ||
| $this->subQuery->andWhere(['commerce_purchasables.height' => $this->height]); | ||
| } else { | ||
|
|
@@ -837,6 +906,10 @@ protected function beforePrepare(): bool | |
| } | ||
|
|
||
| if ($this->length !== false) { | ||
| if (!$hasPurchasableDetailColumns) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->length === null) { | ||
| $this->subQuery->andWhere(['commerce_purchasables.length' => $this->length]); | ||
| } else { | ||
|
|
@@ -845,6 +918,10 @@ protected function beforePrepare(): bool | |
| } | ||
|
|
||
| if ($this->weight !== false) { | ||
| if (!$hasPurchasableDetailColumns) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->weight === null) { | ||
| $this->subQuery->andWhere(['commerce_purchasables.weight' => $this->weight]); | ||
| } else { | ||
|
|
@@ -880,7 +957,7 @@ protected function beforePrepare(): bool | |
| */ | ||
| public function populate($rows): array | ||
| { | ||
| if (!empty($rows) && Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| if (!empty($rows) && $this->_storeTablesExist() && Plugin::getInstance()->getCatalogPricingRules()->hasCatalogPricingRules()) { | ||
| $row = ArrayHelper::firstValue($rows); | ||
| $store = Plugin::getInstance()->getStores()->getStoreBySiteId($row['siteId']); | ||
| $purchasableIds = ArrayHelper::getColumn($rows, 'id'); | ||
|
|
@@ -920,4 +997,36 @@ public function populate($rows): array | |
|
|
||
| return parent::populate($rows); | ||
| } | ||
|
|
||
| private function _storeTablesExist(): bool | ||
| { | ||
| $db = Craft::$app->getDb(); | ||
|
|
||
| return $db->tableExists(Table::SITESTORES) && | ||
| $db->tableExists(Table::PURCHASABLES_STORES); | ||
| } | ||
|
|
||
| private function _hasStoreTableParams(): bool | ||
| { | ||
| return isset($this->price) || | ||
| isset($this->promotionalPrice) || | ||
| isset($this->onPromotion) || | ||
| isset($this->salePrice) || | ||
| isset($this->stock) || | ||
| isset($this->inventoryTracked) || | ||
| isset($this->availableForPurchase) || | ||
| isset($this->shippingCategoryId) || | ||
| isset($this->hasStock); | ||
| } | ||
|
|
||
| private function _purchasableDetailColumnsExist(): bool | ||
| { | ||
| $db = Craft::$app->getDb(); | ||
|
|
||
| return $db->columnExists(Table::PURCHASABLES, 'width') && | ||
| $db->columnExists(Table::PURCHASABLES, 'height') && | ||
| $db->columnExists(Table::PURCHASABLES, 'length') && | ||
| $db->columnExists(Table::PURCHASABLES, 'weight') && | ||
| $db->columnExists(Table::PURCHASABLES, 'taxCategoryId'); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in f382275. The missing-store-table fallback now selects boolean-safe defaults for
availableForPurchaseandfreeShipping(1and0) instead ofNULL, so hydration won’t assign null to non-nullable bool properties.