【问题标题】:Cannot filter products by multiple tags in Shopware 6无法在 Shopware 6 中按多个标签过滤产品
【发布时间】:2022-01-04 09:31:08
【问题描述】:

我的问题之前的一点背景:

一个产品有多个标签,比如说 6 个标签,我们想过滤这 4 个标签中有 2 个的产品,但只过滤那些同时有这 2 个标签的产品。有没有办法使用 criteria 做到这一点?过滤器请求示例如下:

选择所有具有(TAG A 和 TAG B)或(TAG C)的产品

标签-A|标签-B,标签-C

我已经尝试过使用EqualsAnyFilter,但它选择了OR,它过滤仅具有标签A的产品-但我希望它是AND过滤器。仅使用 EqualsFilter 似乎不会退回任何产品。

        $criteria = new Criteria();
        $criteria->addAssociation('tags');

        $orFilters = [];

        $orTags = explode(',', 'TAG-A|TAG-B,TAG-C'); // Based on above scenario

        foreach ($orTags as $orTag) {
            $andFilters = [];
            $andTags = explode('|', $orTag);
            
            // Attempt 1 with EqualsAnyFilter
            $andFilters[] = new EqualsAnyFilter('tags.name', $andTags);


            // Attempt 2 with EqualsFilter
            foreach ($andTags as $andTag) {
                $andFilters[] = new EqualsFilter('tags.name', $andTag);
            }


            if ($andFilters) {
                $orFilters[] = new AndFilter($andFilters);
            }
        }

        if ($orFilters) {
            $criteria->addFilter(new OrFilter($orFilters));
        }

但这不会返回所需的结果。有没有办法过滤这样的产品?欢迎任何建议。谢谢!

【问题讨论】:

    标签: php symfony shopware shopware6


    【解决方案1】:

    我可以通过使用ContainsFilter 来解决如下问题:

        foreach ($andTags as $andTag) {
            $andFilters[] = new ContainsFilter('tagIds', $andTag);
        }
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2021-01-01
      相关资源
      最近更新 更多