public function getRelatedPurchasablesFromArray(array $purchasables, $limit)
    {
        $categories = [];

        /**
         * @var PurchasableInterface $product
         */
        foreach ($purchasables as $purchasable) {
            $category = $purchasable->getPrincipalCategory();
            if (
                $category instanceof CategoryInterface &&
                !in_array($category, $categories)
            ) {
                $categories[] = $category;
            }
        }

        if (empty($categories)) {
            return [];
        }

        return $this
            ->purchasableRepository
            ->createQueryBuilder('p')
            ->where('p.principalCategory IN(:categories)')
            ->andWhere('p NOT IN(:purchasables)')
            ->andWhere('p.enabled = :enabled')
            ->setParameters([
                'categories' => $categories,
                'purchasables' => $purchasables,
                'enabled' => true,
            ])
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }
public function getRelatedPurchasablesFromArray(array $purchasables, $limit)
    {
        $categories = [];

        /**
         * @var PurchasableInterface $product
         */
        foreach ($purchasables as $purchasable) {
            $category = $purchasable->getPrincipalCategory();
            if (
                $category instanceof CategoryInterface &&
                !in_array($category, $categories)
            ) {
                $categories[] = $category;
            }
        }

        if (empty($categories)) {
            return [];
        }

        return $this
            ->purchasableRepository
            ->createQueryBuilder('p')
            ->where('p.principalCategory IN(:categories)')
            ->andWhere('p NOT IN(:purchasables)')
            ->andWhere('p.enabled = :enabled')
            ->setParameters([
                'categories' => $categories,
                'purchasables' => $purchasables,
                'enabled' => true,
            ])
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }
public function getRelatedPurchasablesFromArray ( array $ purchasables , $ limit ) { $ categories = [ ] ; /**
         * @var PurchasableInterface $product
         */ foreach ( $ purchasables as $ purchasable ) { $ category = $ purchasable -> getPrincipalCategory ( ) ; if ( $ category instanceof CategoryInterface && ! in_array ( $ category , $ categories ) ) { $ categories [ ] = $ category ; } } if ( empty ( $ categories ) ) { return [ ] ; } return $ this -> purchasableRepository -> createQueryBuilder ( 'p' ) -> where ( 'p.principalCategory IN(:categories)' ) -> andWhere ( 'p NOT IN(:purchasables)' ) -> andWhere ( 'p.enabled = :enabled' ) -> setParameters ( [ 'categories' => $ categories , 'purchasables' => $ purchasables , 'enabled' => true , ] ) -> setMaxResults ( $ limit ) -> getQuery ( ) -> getResult ( ) ; }
Given a Collection of Purchasables, return a collection of related
purchasables.

@param PurchasableInterface[] $purchasables Purchasable
@param int                    $limit        Limit of elements retrieved

@return array Related products
Given a Collection of Purchasables return a collection of related purchasables .