craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
227 stars 170 forks source link

Variant queries don't respect `->site()` #2647

Closed joshuapease closed 2 years ago

joshuapease commented 2 years ago

Description

I'm trying to query a subset of variants that are enabled for a certain site.

Variant::find()
    ->limit(10)
    ->site($siteId)
    ->all();

Since the "Enable for site" Lightswitch is at the product level, I suppose this makes sense, but... variants are directly tied to a product, so my assumption would be that using ->site() would return only Variants who's product is for that specific site (or that currentSite would be the default is site wasn't explicitly added).

Curious if there are any workarounds or if this is something that could be added in a future release.

Thanks for your help!

Steps to reproduce

  1. Create multiple Craft sites
  2. Create multiple Product/Variant combos that are enabled for specific sites
  3. Query Variants for a particular site.

Additional info

Composer.json

"akeneo/api-php-client": "^6.0",
"am-impact/amcommand": "3.1.4",
"clubstudioltd/craft-asset-rev": "6.0.2",
"craftcms/aws-s3": "1.3.0",
"craftcms/cms": "3.7.19.1",
"craftcms/commerce": "3.4.7",
"craftcms/commerce-stripe": "2.4.0",
"craftcms/feed-me": "4.4.0",
"craftcms/redactor": "2.8.8",
"enovatedesign/craft-style-inliner": "2.3",
"fruitstudios/linkit": "1.1.12.1",
"http-interop/http-factory-guzzle": "^1.0",
"hubspot/api-client": "^2.8",
"matt-west/craft-recaptcha": "1.5.2",
"nystudio107/craft-retour": "3.1.65",
"nystudio107/craft-seomatic": "3.4.17",
"php-http/guzzle6-adapter": "^2.0",
"spatie/craft-ray": "1.1.5",
"studioespresso/craft-scout": "dev-master",
"superbig/craft3-imgix": "2.1.0",
"supercool/tablemaker": "2.0.1",
"surprisehighway/craft-avatax": "2.1.7",
"verbb/navigation": "1.4.22",
"verbb/postie": "2.4.11",
"verbb/super-table": "2.7.0",
"viget/craft-classnames": "1.0.3",
"viget/craft-viget-base": "3.1.2",
"vlucas/phpdotenv": "^3.4.0",
"yiisoft/yii2-redis": "^2.0"
nfourtythree commented 2 years ago

Hi @joshuapease

Thank you for your message.

As you stated the only real multi-site aware feature for variants is indirectly related via the "Enable for site" feature on the product. For example, if you delete a variant it will delete it for all sites. This means that variants themselves are related to all sites.

There is a way for you to perform the query you are looking for, it would look something like this:

{% set variants = craft.variants({
    hasProduct: {
        enabledForSite: true
    }
}).all() %}

<ul>
    {% for v in variants %}
        <li>{{ v.product.title ~ ' - ' ~v.title }}</li>
    {% endfor %}
</ul>

This will return you all the variants that have a product that is enabled for the current site.

Hope this helps, thanks!

joshuapease commented 2 years ago

Thanks so much! I see it in the docs now that I know what I'm looking for.

nfourtythree commented 2 years ago

Hi @joshuapease

Great news, thanks!