/**
  * Overrides the product categories assigning the one saved as principal
  * category.
  *
  * @param ProductInterface $product The product being saved
  */
 protected function fixProductCategory(ProductInterface $product)
 {
     $principalCategory = $product->getPrincipalCategory();
     if ($principalCategory instanceof CategoryInterface) {
         $categories = new ArrayCollection();
         $categories->add($principalCategory);
         $product->setCategories($categories);
     }
 }
 /**
  * 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;
 }
 /**
  * Fixes the product categories.
  *
  * @param ProductInterface $product The product to fix.
  */
 public function fixProduct(ProductInterface $product)
 {
     $principalCategory = $product->getPrincipalCategory();
     $categories = $product->getCategories();
     if (!empty($principalCategory) && !$categories->contains($principalCategory)) {
         /**
          * The product has a principal category but this one is not assigned
          * as product category so this one is added.
          */
         $categories->add($principalCategory);
         $product->setCategories($categories);
     } elseif (empty($principalCategory) && 0 < $categories->count()) {
         /**
          * The product does not have principal category but has categories
          * assigned so the first category is assigned as principal category.
          */
         $product->setPrincipalCategory($categories->first());
     }
 }
示例#4
0
 /**
  * Get the principalCategory.
  *
  * @return CategoryInterface Principal category
  */
 public function getPrincipalCategory()
 {
     return $this->product->getPrincipalCategory();
 }