Skip to content

Commit 5fb7746

Browse files
authored
Merge pull request #25 from magefan/11182-remede-logic-to-get-current-category
Used GetCategoryByProductInterface to get current category
2 parents 4e0b53a + 25339c3 commit 5fb7746

1 file changed

Lines changed: 11 additions & 41 deletions

File tree

Model/RuleManager.php

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ class RuleManager
6464
*/
6565
protected $ruleValidator;
6666

67-
/**
68-
* @var \Magento\Catalog\Api\CategoryRepositoryInterface|mixed
69-
*/
70-
protected $categoryRepository;
71-
7267
/**
7368
* @var
7469
*/
@@ -79,6 +74,11 @@ class RuleManager
7974
*/
8075
protected $ruleCollectionFactory;
8176

77+
/**
78+
* @var
79+
*/
80+
protected $getCategoryByProduct;
81+
8282
/**
8383
* @param ProductCollectionFactory $productCollectionFactory
8484
* @param CatalogConfig $catalogConfig
@@ -88,7 +88,6 @@ class RuleManager
8888
* @param RuleRepositoryInterface $ruleRepository
8989
* @param StoreManagerInterface $storeManager
9090
* @param \Magefan\AutoRelatedProduct\Model\ActionValidator $ruleValidator
91-
* @param \Magento\Catalog\Api\CategoryRepositoryInterface|null $categoryRepository
9291
*/
9392
public function __construct
9493
(
@@ -101,7 +100,7 @@ public function __construct
101100
StoreManagerInterface $storeManager,
102101
ActionValidator $ruleValidator,
103102
RuleCollectionFactory $ruleCollectionFactory,
104-
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository = null
103+
\Magefan\Community\Api\GetCategoryByProductInterface $getCategoryByProduct = null
105104
) {
106105
$this->productCollectionFactory = $productCollectionFactory;
107106
$this->catalogConfig = $catalogConfig;
@@ -112,8 +111,8 @@ public function __construct
112111
$this->storeManager = $storeManager;
113112
$this->ruleValidator = $ruleValidator;
114113
$this->ruleCollectionFactory = $ruleCollectionFactory;
115-
$this->categoryRepository = $categoryRepository ?:\Magento\Framework\App\ObjectManager::getInstance()
116-
->get(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
114+
$this->getCategoryByProduct = $getCategoryByProduct ?:\Magento\Framework\App\ObjectManager::getInstance()
115+
->get(\Magefan\Community\Api\GetCategoryByProductInterface::class);
117116
}
118117

119118
/**
@@ -204,40 +203,11 @@ protected function addOutOfStockFilter(RuleInterface $rule): void
204203
*/
205204
protected function addProductsFromTheSameCategoryFilter($currentCategory, $currentProduct, RuleInterface $rule): void
206205
{
207-
$productCategoryId = false;
208-
209-
if ($currentCategory) {
210-
$productCategoryId = $currentCategory->getId();
211-
} elseif ($currentProduct) {
212-
$categoryIds = $currentProduct->getCategoryIds();
213-
214-
if ($categoryIds) {
215-
216-
$productCategory = null;
217-
$level = -1;
218-
$rootCategoryId = $this->storeManager->getStore()->getRootCategoryId();
219-
220-
foreach ($categoryIds as $categoryId) {
221-
try {
222-
$category = $this->categoryRepository->get($categoryId);
223-
if ($category->getIsActive()
224-
&& $category->getLevel() > $level
225-
&& in_array($rootCategoryId, $category->getPathIds())
226-
) {
227-
$level = $category->getLevel();
228-
$productCategory = $category;
229-
}
230-
} catch (\Exception $e) {}
231-
}
232-
233-
if ($productCategory) {
234-
$productCategoryId = $productCategory->getId();
235-
}
236-
}
237-
}
206+
$category = $this->getCategoryByProduct->execute($currentProduct);
207+
$productCategoryId = $category ? $category->getId() : false;
238208

239209
if ($productCategoryId) {
240-
$this->_itemCollection->addCategoriesFilter(['eq' => $productCategoryId]);
210+
$this->_itemCollection->addCategoriesFilter(['in' => [$productCategoryId]]);
241211
}
242212
}
243213

0 commit comments

Comments
 (0)