/** * 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 */ 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(); }
/** * Test get home purchasables. * * @dataProvider dataGetOfferPurchasables */ public function testGetOfferPurchasables($count, $numberExpected, $useStock) { $purchasable = $this->find('purchasable', 2); $oldStock = $purchasable->getStock(); $purchasable->setStock(0); $this->flush($purchasable); $purchasables = $this->purchasableRepository->getOfferPurchasables($count, $useStock); $this->assertTrue(is_array($purchasables)); $this->assertCount($numberExpected, $purchasables); $purchasable->setStock($oldStock); $this->flush($purchasable); }