# Load all variants of a product (PHP)

**URL:** <https://forum.shopware.com/t/load-all-variants-of-a-product-php/90872>\
**Category:** Shopware 6 (English)\
**Tags:** programming\
**Created:** [14. Oktober 2021 um 10:49 UTC](https://forum.shopware.com/t/load-all-variants-of-a-product-php/90872 "2021-10-14T10:49:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Kohl](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Simon\_Kohl](https://forum.shopware.com/u/Simon_Kohl)\
**Post date:** [14. Oktober 2021 um 10:49 UTC](https://forum.shopware.com/t/load-all-variants-of-a-product-php/90872/1 "2021-10-14T10:49:27Z")

</div>

I am trying to load all variations of a product.

While using google I found this code snippet:

```auto
class ProductViewSubscriber implements EventSubscriberInterface
{
    private EntityRepositoryInterface $productRepository;

    public function __construct(EntityRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function getVariantsOfProduct(SalesChannelProductEntity $product, Context $context): EntitySearchResult
    {
        $parentProduct = $product->getParent();
        if ($parentProduct != null) {
            // We need the parent product to find all variants
            $product = $parentProduct;
        }
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('product.parentId',
            $product->getUniqueIdentifier()));
        return $this->productRepository->search($criteria, $context);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            ProductPageLoadedEvent::class => ['addVariants'],
        ];
    }

    public function addVariants(ProductPageLoadedEvent $pEvent)
    {
        $products = $this->getVariantsOfProduct($product = $pEvent->getPage()->getProduct(), $pEvent->getContext());
        $matchingVariants = $products->getElements();
    }
}

```

But using the code snippet only an empty list is returned, though there are definitely variants to that product. How can I fix this code?

---

<div class="post-metadata">

**Author:** ![moschadr](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@moschadr](https://forum.shopware.com/u/moschadr)\
**Post date:** [2. November 2021 um 12:58 UTC](https://forum.shopware.com/t/load-all-variants-of-a-product-php/90872/2 "2021-11-02T12:58:52Z")

</div>

change `new EqualsFilter('product.parentId',` to `new EqualsFilter('parentId',`
