/** * Get products with price reduction. * All products returned are actived and none deleted * * @param integer $limit Product limit. By default, this value is 0 * * @return ArrayCollection Set of products, result of the query */ public function getOfferProducts($limit = 0) { $query = $this->productRepository->createQueryBuilder('p')->where('p.enabled = :enabled')->andWhere('p.reducedPrice > 0')->andWhere('p.reducedPrice IS NOT NULL')->setParameters(['enabled' => true])->orderBy('p.updatedAt', 'DESC'); if ($limit > 0) { $query->setMaxResults($limit); } $results = $query->getQuery()->getResult(); return new ArrayCollection($results); }
/** * Given a specific Product, return a simple collection of related products * * @param ProductInterface $product Product * @param int $limit Limit * * @return ArrayCollection */ public function getRelatedProducts(ProductInterface $product, $limit) { $relatedProducts = new ArrayCollection(); $principalCategory = $product->getPrincipalCategory(); if ($principalCategory instanceof CategoryInterface) { $relatedProducts = $this->productRepository->createQueryBuilder('p')->select('p', 'v', 'o')->leftJoin('p.variants', 'v')->leftJoin('v.options', 'o')->where('p.principalCategory = :principalCategory')->andWhere('p.enabled = :enabled')->setParameters(['principalCategory' => $principalCategory, 'enabled' => true])->getQuery()->getResult(); $relatedProducts = new ArrayCollection($relatedProducts); $relatedProducts->removeElement($product); $relatedProducts = $relatedProducts->slice(0, $limit); } return $relatedProducts; }
/** * Test when getting products from multiple categories. */ public function testGettingProductsFromMultipleCategories() { /** * @var $rootCategory CategoryInterface */ $rootCategory = $this->categoryRepository->findOneBy(['slug' => 'root-category']); $category = $this->categoryRepository->findOneBy(['slug' => 'category']); $products = $this->productRepository->getAllFromCategories([$rootCategory, $category]); $this->assertCount(3, $products, 'It should only return one product on the root category'); }
/** * Test get home products. * * @dataProvider dataGetOfferProducts */ public function testGetOfferProducts($count, $numberExpected, $useStock) { $product = $this->find('product', 2); $oldStock = $product->getStock(); $product->setStock(0); $this->flush($product); $products = $this->productRepository->getOfferProducts($count, $useStock); $this->assertTrue(is_array($products)); $this->assertCount($numberExpected, $products); $product->setStock($oldStock); $this->flush($product); }
/** * Build a basic query given a set of categories and a set of unwanted * products * * @param ProductInterface[] $products Products * @param int $limit Limit * * @return array */ private function getRelatedProductsGivenAnArrayOfProducts(array $products, $limit) { $categories = []; /** * @var ProductInterface $product */ foreach ($products as $product) { $category = $product->getPrincipalCategory(); if ($category instanceof CategoryInterface && !in_array($category, $categories)) { $categories[] = $category; } } if (empty($categories)) { return []; } return $this->productRepository->createQueryBuilder('p')->where("p.principalCategory IN(:categories)")->andWhere("p NOT IN(:products)")->andWhere('p.enabled = :enabled')->setParameters(['categories' => $categories, 'products' => $products, 'enabled' => true])->setMaxResults($limit)->getQuery()->getResult(); }
/** * Checks if there is any product on the store. * * @return boolean */ protected function isThereAnyProduct() { $enabledProduct = $this->productRepository->findOneBy(['enabled' => true]); return $enabledProduct instanceof ProductInterface; }