Skip to content

Commit

Permalink
Fixed variants propagating to incorrect sites
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Sep 23, 2024
1 parent 8710f1c commit 05f65bf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed a bug where variants weren’t respecting their owner’s propagation method.
- Fixed a PHP error that could occur when creating a new product.

## 5.1.2 - 2024-09-19
Expand Down
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 @@ -886,6 +886,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
63 changes: 63 additions & 0 deletions src/migrations/m240923_132625_remove_orphaned_variants_sites.php
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;
}
}

0 comments on commit 05f65bf

Please sign in to comment.