Huzaifa Mustafa
Huzaifa Mustafa

Reputation: 322

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

I am trying to create a custom command in which I want to utilize the Dynamic Product Group feature of Shopware 6 to fetch the products which match the filters in a specific product group (a.k.a product streams).

I am doing this to fetch the streams initially, but I am lost as to how to apply the filters to fetch the products.

            $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
                }
            }

I tried to get the filters which gives me the array of filters that are applied in the product stream, but it is not directly applicable to the criteria->addFilter() due to different expected types.

This is the filters array that I get:

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: []
}

The productCrossSellings and productExports associations are all returning null as well (although I do not know what is the use of these variables, but I thought maybe it is being used somehow internally to get the associated products for that group).

So my question is, that how do we get the products that match the filters in a product group ?

Upvotes: 0

Views: 2333

Answers (1)

Valerio8787
Valerio8787

Reputation: 905

You can check \Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder and how it is used in \Shopware\Core\Content\Product\SalesChannel\CrossSelling\SalesChannelCrossSellingController::loadProductsForCrossSelling

I hope it would be helpful.

Upvotes: 1

Related Questions