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

[5.x] Fixed variants propagating to incorrect sites #3688

Merged
merged 3 commits into from
Sep 25, 2024
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
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public string $schemaVersion = '5.1.0.2';
public string $schemaVersion = '5.1.0.3';

/**
* @inheritdoc
Expand Down
14 changes: 14 additions & 0 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,20 @@ public static function gqlScopesByContext(mixed $context): array
return ['productTypes.' . $context->uid];
}

/**
* @inheritdoc
*/
public function getSupportedSites(): array
{
$owner = $this->getOwner();

if (!$owner) {
return [Craft::$app->getSites()->getPrimarySite()->id];
}

return $this->getOwner()->getSupportedSites();
}

/**
* @inheritdoc
* @throws Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace craft\commerce\migrations;

use craft\db\Migration;
use craft\db\Query;

/**
* m240923_132625_remove_orphaned_variants_sites migration.
*/
class m240923_132625_remove_orphaned_variants_sites extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Find all existing combinations of product and site IDs
$allProductsSites = (new Query())
->select(['elementId', 'siteId'])
->from('{{%elements_sites}}' . ' es')
->innerJoin('{{%commerce_products}}' . ' p', '[[es.elementId]] = [[p.id]]')
->collect();

// Group them by product ID
$siteIdsByProductId = $allProductsSites->groupBy('elementId')->map(function($row) {
return collect($row)->pluck('siteId')->toArray();
}
);

// Find all existing combinations of variant and site IDs
$allVariantsSites = (new Query())
->select(['es.id', 'elementId', 'siteId', 'primaryOwnerId'])
->from('{{%elements_sites}}' . ' es')
->innerJoin('{{%commerce_variants}}' . ' v', '[[es.elementId]] = [[v.id]]')
->collect();

// Find all variants that are not associated with any of their product's sites
$orphanedVariantsSites = array_values($allVariantsSites->filter(function($row) use ($siteIdsByProductId) {
return !in_array($row['siteId'], $siteIdsByProductId[$row['primaryOwnerId']]);
})->map(fn($row) => $row['id'])->toArray());

if (empty($orphanedVariantsSites)) {
return true;
}

// Bulk delete the orphaned variants' site rows (if any) 1000 at a time
foreach (array_chunk($orphanedVariantsSites, 1000) as $chunk) {
$this->delete('{{%elements_sites}}', ['id' => $chunk]);
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m240923_132625_remove_orphaned_variants_sites cannot be reverted.\n";
return false;
}
}
Loading