Shopware 6中如何从产品流ID中获取产品?

How to get products from the product stream ID in Shopware 6?

我正在尝试创建一个自定义命令,我想在其中利用 Shopware 6 的动态产品组功能来获取与特定产品组(a.k.a 产品流)中的过滤器相匹配的产品。

我最初这样做是为了获取流,但我不知道如何应用过滤器来获取产品。

            $streamCriteria = new Criteria($productStreamIds);
            $streamCriteria->addAssociation('productCrossSellings');
            $streamCriteria->addAssociation('productExports');
            $streamCriteria->addAssociation('filters');

            $streams = $this->streamRepository->search($streamCriteria, $context)->getEntities();
            /** @var ProductStreamEntity $stream */
            foreach ($streams as $stream) {
                foreach ($stream->getFilters() as $filter) {
                    $productCriteria->addAggregation($filter); // DOES NOT WORK
                    $productCriteria->addFilter($filter); // DOES NOT WORK EITHER
                }
            }

我尝试获取 filters,它为我提供了产品流中应用的过滤器数组,但由于预期类型不同,它不能直接应用于 criteria->addFilter()

这是我得到的过滤器数组:

Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterCollection^ {#10304
  #elements: array:5 [
    "2c6a8a44d9c544f2812024f6414d56f9" => Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterEntity^ {#11014
      #type: "multi"
      #field: null
      #operator: "OR"
      #value: null
      #productStreamId: "160274948d684a73afad0bde6aee19e8"
      #parentId: null
      #productStream: null
      #queries: null
      #parent: null
      #position: 0
      #parameters: null
      #customFields: null
      #_uniqueIdentifier: "2c6a8a44d9c544f2812024f6414d56f9"
      #versionId: null
      #translated: []
      #createdAt: DateTimeImmutable @1600069477 {#3113
        date: 2020-09-14 07:44:37.521 UTC (+00:00)
      }
      #updatedAt: null
      #_entityName: "product_stream_filter"
      #extensions: array:1 [
        "foreignKeys" => Shopware\Core\Framework\Struct\ArrayStruct^ {#11007
          #data: []
          #apiAlias: null
          #extensions: []
        }
      ]
      #id: "2c6a8a44d9c544f2812024f6414d56f9"
    }
    "6dff7581e91a477ea2eca564a099eb90" => Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterEntity^ {#10832
      #type: "range"
      #field: "width"
      #operator: null
      #value: null
      #productStreamId: "160274948d684a73afad0bde6aee19e8"
      #parentId: "2c6a8a44d9c544f2812024f6414d56f9"
      #productStream: null
      #queries: null
      #parent: null
      #position: 1
      #parameters: array:1 [
        "lte" => 749
      ]
      #customFields: null
      #_uniqueIdentifier: "6dff7581e91a477ea2eca564a099eb90"
      #versionId: null
      #translated: []
      #createdAt: DateTimeImmutable @1600069477 {#2958
        date: 2020-09-14 07:44:37.523 UTC (+00:00)
      }
      #updatedAt: null
      #_entityName: "product_stream_filter"
      #extensions: array:1 [
        "foreignKeys" => Shopware\Core\Framework\Struct\ArrayStruct^ {#10871
          #data: []
          #apiAlias: null
          #extensions: []
        }
      ]
      #id: "6dff7581e91a477ea2eca564a099eb90"
    }
    "754b78c4ec74492bb96dc724f3e7ba6a" => Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterEntity^ {#10766
      #type: "range"
      #field: "weight"
      #operator: null
      #value: null
      #productStreamId: "160274948d684a73afad0bde6aee19e8"
      #parentId: "2c6a8a44d9c544f2812024f6414d56f9"
      #productStream: null
      #queries: null
      #parent: null
      #position: 2
      #parameters: array:1 [
        "gte" => 293
      ]
      #customFields: null
      #_uniqueIdentifier: "754b78c4ec74492bb96dc724f3e7ba6a"
      #versionId: null
      #translated: []
      #createdAt: DateTimeImmutable @1600069477 {#10274
        date: 2020-09-14 07:44:37.525 UTC (+00:00)
      }
      #updatedAt: null
      #_entityName: "product_stream_filter"
      #extensions: array:1 [
        "foreignKeys" => Shopware\Core\Framework\Struct\ArrayStruct^ {#10774
          #data: []
          #apiAlias: null
          #extensions: []
        }
      ]
      #id: "754b78c4ec74492bb96dc724f3e7ba6a"
    }
    "86d2c2bd8df14f8684c8f4c801e5a19a" => Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterEntity^ {#10272
      #type: "range"
      #field: "height"
      #operator: null
      #value: null
      #productStreamId: "160274948d684a73afad0bde6aee19e8"
      #parentId: "2c6a8a44d9c544f2812024f6414d56f9"
      #productStream: null
      #queries: null
      #parent: null
      #position: 3
      #parameters: array:1 [
        "gte" => 746
      ]
      #customFields: null
      #_uniqueIdentifier: "86d2c2bd8df14f8684c8f4c801e5a19a"
      #versionId: null
      #translated: []
      #createdAt: DateTimeImmutable @1600069477 {#10313
        date: 2020-09-14 07:44:37.526 UTC (+00:00)
      }
      #updatedAt: null
      #_entityName: "product_stream_filter"
      #extensions: array:1 [
        "foreignKeys" => Shopware\Core\Framework\Struct\ArrayStruct^ {#11009
          #data: []
          #apiAlias: null
          #extensions: []
        }
      ]
      #id: "86d2c2bd8df14f8684c8f4c801e5a19a"
    }
    "9b763069551244179dbc1f285290b673" => Shopware\Core\Content\ProductStream\Aggregate\ProductStreamFilter\ProductStreamFilterEntity^ {#10283
      #type: "equalsAny"
      #field: "id"
      #operator: null
      #value: "c01c974a89eb4f17ab78c48251864eb7|5c0e5bc5f03d42a6980c11bf8ab67428"
      #productStreamId: "160274948d684a73afad0bde6aee19e8"
      #parentId: "2c6a8a44d9c544f2812024f6414d56f9"
      #productStream: null
      #queries: null
      #parent: null
      #position: 0
      #parameters: null
      #customFields: null
      #_uniqueIdentifier: "9b763069551244179dbc1f285290b673"
      #versionId: null
      #translated: []
      #createdAt: DateTimeImmutable @1600069477 {#12983
        date: 2020-09-14 07:44:37.522 UTC (+00:00)
      }
      #updatedAt: null
      #_entityName: "product_stream_filter"
      #extensions: array:1 [
        "foreignKeys" => Shopware\Core\Framework\Struct\ArrayStruct^ {#10356
          #data: []
          #apiAlias: null
          #extensions: []
        }
      ]
      #id: "9b763069551244179dbc1f285290b673"
    }
  ]
  #extensions: []
}

productCrossSellingsproductExports 关联也都返回 null (虽然我不知道这些变量有什么用,但我想也许它正在被使用以某种方式在内部获取该组的关联产品)。

所以我的问题是,我们如何获得与产品组中的过滤器相匹配的产品?

你可以查看\Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder 以及如何在 \Shopware\Core\Content\Product\SalesChannel\CrossSelling\SalesChannelCrossSellingController::loadProductsForCrossSelling

中使用

希望对您有所帮助。