Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions src/Plugin/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\View\Result\Page;
use Magento\Catalog\Model\CategoryRepository;

class View
{
Expand All @@ -46,6 +47,10 @@ class View
* @var PageFactory
*/
private $resultPage;
/**
* @var CategoryRepository
*/
private $categoryRepository;


/**
Expand All @@ -54,17 +59,20 @@ class View
* @param Registry $registry
* @param Collection $collection
* @param PageFactory $resultPage
* @param CategoryRepository $categoryRepository
*/
public function __construct(
StoreManager $storeManager,
Registry $registry,
Collection $collection,
PageFactory $resultPage)
PageFactory $resultPage,
CategoryRepository $categoryRepository)
{
$this->storeManager = $storeManager;
$this->registry = $registry;
$this->collection = $collection;
$this->resultPage = $resultPage;
$this->categoryRepository = $categoryRepository;
}

public function afterExecute(MagentoView $subject, $result)
Expand Down Expand Up @@ -99,52 +107,44 @@ public function afterExecute(MagentoView $subject, $result)
$pageMainTitle->setPageTitle($product->getName());
}

if(null == $product->getCategory() || null == $product->getCategory()->getPath()){
$breadcrumbsBlock->addCrumb(
'cms_page',
[
'label' => $product->getName(),
'title' => $product->getName(),
]
);
return $result;
}

$categories = $product->getCategory()->getPath();
$categoriesids = explode('/', $categories);

$categoriesCollection = null;
try {
$categoriesCollection = $this->collection
->addFieldToFilter('entity_id', array('in' => $categoriesids))
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('include_in_menu')
->addAttributeToSelect('is_active')
->addAttributeToSelect('is_anchor');
$productPath = null;
if (null == $product->getCategory() || null == $product->getCategory()->getPath()) {
foreach ($product->getCategoryIds() as $categoryId) {
$category = $this->categoryRepository->get($categoryId);
if ($category->getIsActive() && $category->isInRootCategoryList()) {
$productPath = $category->getPath();
break;
}
}
} else {
$productPath = $product->getCategory()->getPath();
}
if(null == $productPath){
$breadcrumbsBlock->addCrumb(
'cms_page',
[
'label' => $product->getName(),
'title' => $product->getName(),
]
);
return $result;
}
$categoriesIds = explode('/', $productPath);
foreach ($categoriesIds as $categoryId) {
$category = $this->categoryRepository->get($categoryId);
if ($category->getIsActive() && $category->isInRootCategoryList()) {
$path = [
'label' => $category->getName(),
'link' => $category->getUrl() ? $category->getUrl() : ''
];
$breadcrumbsBlock->addCrumb('category' . $categoryId, $path);
}
}
} catch (LocalizedException $e) {
return $result;
}

foreach ($categoriesCollection->getItems() as $category) {
if ($category->getIsActive() && $category->isInRootCategoryList()) {
$categoryId = $category->getId();
$path = [
'label' => $category->getName(),
'link' => $category->getUrl() ? $category->getUrl() : ''
];
$breadcrumbsBlock->addCrumb('category' . $categoryId, $path);
}
}

$breadcrumbsBlock->addCrumb(
'cms_page',
[
'label' => $product->getName(),
'title' => $product->getName(),
]
);

return $result;
}

Expand Down