예제 #1
0
 /**
  * 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;
 }
 /**
  * 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();
 }