Skip to content

Commit

Permalink
move referee category, if exists, to the beginning of the category id…
Browse files Browse the repository at this point in the history
…s array to give it higher priority.
  • Loading branch information
itaymesh committed Feb 3, 2022
1 parent 0c057a1 commit a431e8c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
88 changes: 66 additions & 22 deletions Plugin/Magento/Catalog/ViewModel/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,32 @@ class Breadcrumbs
private $catalogData;

/**
* @var
* @var StoreManagerInterface
*/
private $storeManagerInterface;
private StoreManagerInterface $storeManager;

/**
* Breadcrumbs constructor.
* @param StoreManagerInterface $storeManagerInterface
* @param Data $catalogData
*/

/**
* Instance of category collection.
*
* @var Collection
*/
protected Collection $categoryCollection;

public function __construct(
StoreManagerInterface $storeManagerInterface,
Data $catalogData
) {
Data $catalogData,
Collection $categoryCollection
)
{
$this->storeManager = $storeManagerInterface;
$this->catalogData = $catalogData;
$this->categoryCollection = $categoryCollection;
}

/**
Expand All @@ -60,24 +71,20 @@ public function afterGetJsonConfigurationHtmlEscaped(NativeBreadcrumbs $subject,
/**
* @param Product $product
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws LocalizedException
*/
public function getFullCategoryPath(Product $product): ?array
{
/** @var Collection $collection */
$collection = $product->getCategoryCollection();
$rootCategoryId = $this->getRootCategoryId();
try {
$collection
->addAttributeToFilter('path', ['like' => "1/{$rootCategoryId}/%"])
->addAttributeToSelect('name')
->addAttributeToSelect('include_in_menu')
->addAttributeToSelect('is_active')
->setOrder('level', 'DESC');
} catch (LocalizedException $e) {
return null;
$collection = $this->getProductCategories($product);

if ($collection->count() == 0) {
return [];
}

$pool = [];

$pool = [];
$targetCategory = null;

/** @var Category $category */
Expand Down Expand Up @@ -117,14 +124,14 @@ public function getFullCategoryPath(Product $product): ?array

if ($targetCategory) {
$pathInStore = $category->getPathInStore();
$pathIds = array_reverse(explode(',', $pathInStore));
$pathIds = array_reverse(explode(',', $pathInStore));

foreach ($pathIds as $categoryId) {
if (isset($pool[$categoryId]) && $pool[$categoryId]->getName()) {
$category = $pool[$categoryId];
$path[] = [
$path[] = [
'label' => $category->getName(),
'link' => $category->getUrl(),
'link' => $category->getUrl(),
];
}
}
Expand All @@ -133,16 +140,53 @@ public function getFullCategoryPath(Product $product): ?array
return $path;
}

/**
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws LocalizedException
*/
private function getProductCategories(Product $product)
{

$rootCategoryId = $this->getRootCategoryId();

$collection = $product->getCategoryCollection()
->addAttributeToFilter('path', ['like' => "1/{$rootCategoryId}/%"])
->addAttributeToSelect('name')
->addAttributeToSelect('is_active')
->setOrder('level', 'DESC');


if ($collection->count() < 2) {
return $collection;
}

/**
* move referee category, if exists, to the beginning of the category ids array to give it higher priority.
*/
if ($categoryId = $product->getCategoryId()) {
$currentCategory = $collection->getItemById($categoryId);
$collection->removeItemByKey($categoryId);
$categories = $collection->getItems();
$collection->removeAllItems();
$collection->addItem($currentCategory);
foreach ($categories as $category) {
$collection->addItem($category);
}
}


return $collection;
}

/**
* @return int
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getRootCategoryId()
private function getRootCategoryId(): int
{
// get store group id for current store
$storeGroupId = $this->storeManager->getStore()->getStoreGroupId();
// get root category id
$rootCategoryId = $this->storeManager->getGroup($storeGroupId)->getRootCategoryId();
return $rootCategoryId;
return (int)$this->storeManager->getGroup($storeGroupId)->getRootCategoryId();
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Studio Raz Magento 2 Breadcrumb Module",
"type": "magento2-module",
"prefer-stable": true,
"version": "0.0.5",
"version": "1.0.0",
"license": [
"proprietary"
],
Expand Down

0 comments on commit a431e8c

Please sign in to comment.