commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[5.x]: Fetching variants per site returns extra records

Open johnnynotsolucky opened this issue 1 year ago • 0 comments

What happened?

Description

Potentially not Commerce related, but Craft related.

When attempting to fetch variants for more than one site, the results return the variantCount * sites * sites.

> \craft\commerce\elements\Variant::find()->site(['default'])->count();
= "91"

> \craft\commerce\elements\Variant::find()->site(['default', 'siteA'])->count(); // 91 * 2 * 2
= "364"

> \craft\commerce\elements\Variant::find()->site(['default', 'siteA', 'siteB'])->count(); // 91 * 3 * 3
= "819"

The data returned seems to be duplicated:

> array_map(fn ($x) => ['variantId' => $x->id, 'siteId' => $x->site->id], \craft\commerce\elements\Variant::find()->id(579742)->site('*')->all());
= [
    [
      "variantId" => 579742,
      "siteId" => 3,
    ],
    [
      "variantId" => 579742,
      "siteId" => 2,
    ],
    [
      "variantId" => 579742,
      "siteId" => 1,
    ],
    [
      "variantId" => 579742,
      "siteId" => 3,
    ],
    [
      "variantId" => 579742,
      "siteId" => 2,
    ],
    [
      "variantId" => 579742,
      "siteId" => 1,
    ],
    [
      "variantId" => 579742,
      "siteId" => 3,
    ],
    [
      "variantId" => 579742,
      "siteId" => 2,
    ],
    [
      "variantId" => 579742,
      "siteId" => 1,
    ],
  ]

As the count example above suggests, if I just fetch a variant for single site then it correctly returns a single record.

Steps to reproduce

  1. Tested with above snippets in craft shell

Expected behavior

The query Variant::find()->id(579742)->site('*')->all() returns a single Variant for each site.

Actual behavior

See above.

Craft CMS version

[email protected]

Craft Commerce version

Branch 5.0

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

johnnynotsolucky avatar Mar 05 '24 18:03 johnnynotsolucky