Skip to content

Commit

Permalink
Merge pull request #3076 from craftcms/feature/fix-propagation-on-new…
Browse files Browse the repository at this point in the history
…-site

Fix multi-site propagation
  • Loading branch information
lukeholder authored Feb 1, 2023
2 parents 8bff0ba + 286f132 commit f40f602
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed a PHP error that occurred when retrieving a discount via a null coupon code. ([#3045](https://github.com/craftcms/commerce/issues/3045))
- Fixed a bug that could cause a large number of shipping rule category queries.
- Fixed a PHP error that occurred when re-saving all products that had never finished propagating to all sites. ([#1954](https://github.com/craftcms/commerce/issues/1954))
- Fixed a PHP error that would occur when calling `craft\commerce\services\ProductTypes::getEditableProductTypes()` while not logged in.
- Fixed a PHP error that occurred when saving an invalid shipping method.
- Added support for searching orders by customer name. ([#3050](https://github.com/craftcms/commerce/issues/3050))
Expand Down
15 changes: 14 additions & 1 deletion src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,21 @@ public function getProduct(): ?Product
->trashed(null)
->one();

// A variant should always have a product owner of the same site.
// Sometimes there may not be a product of that site if it doesn't exist yet,
// which usually only happens during propagation of the product, or if propagation queue jobs fail after making a new site.
if ($product === null) {
throw new InvalidConfigException('Invalid product ID: ' . $this->productId);
/** @var Product|null $product */
$product = Product::find()
->id($this->productId)
->status(null)
->trashed(null)
->one();
}

// If we still don't have a product, there is something configured wrong.
if ($product === null) {
throw new InvalidConfigException('Invalid product ID: ' . $this->productId . ', Try re-saving all products.');
}

return $this->_product = $product;
Expand Down
31 changes: 17 additions & 14 deletions src/services/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Craft;
use craft\commerce\elements\Product;
use craft\events\SiteEvent;
use craft\queue\jobs\ResaveElements;
use craft\helpers\Queue;
use craft\queue\jobs\PropagateElements;
use yii\base\Component;

/**
Expand Down Expand Up @@ -38,20 +39,22 @@ public function getProductById(int $id, int $siteId = null): ?Product
*/
public function afterSaveSiteHandler(SiteEvent $event): void
{
$queue = Craft::$app->getQueue();
$siteId = $event->oldPrimarySiteId;
$elementTypes = [
Product::class,
];
if ($event->isNew) {
$oldPrimarySiteId = $event->oldPrimarySiteId;
$elementTypes = [
Product::class,
];

foreach ($elementTypes as $elementType) {
$queue->push(new ResaveElements([
'elementType' => $elementType,
'criteria' => [
'siteId' => $siteId,
'status' => null,
],
]));
foreach ($elementTypes as $elementType) {
Queue::push(new PropagateElements([
'elementType' => $elementType,
'criteria' => [
'siteId' => $oldPrimarySiteId,
'status' => null,
],
'siteId' => null, // all sites
]));
}
}
}
}

0 comments on commit f40f602

Please sign in to comment.