Magento 2-明智的全层次产品面包屑


9

如果我直接从主页或搜索页面打开产品,那么面包屑显示如下:

首页>我的产品

而如果我转到类别,然后是子类别,然后打开产品,它将显示面包屑,如下所示:

首页>类别1>类别2>我的产品

无论我从哪里进入产品页面,都需要使面包屑具有完整的层次结构。

请帮我实现这个


如果产品已分配到两个或更多相同级别的子类别中,该怎么办?
Mohit Kumar Arora

仍然应该有一种方法:)
Magedev2301

Answers:


10

首先,您必须在模块中添加块

路径: app\code\Vendor\Module\Block\Crumbblock.php

<?php
namespace Vendor\Module\Block;

use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\Store;
use Magento\Framework\Registry;

class Crumbblock extends \Magento\Framework\View\Element\Template
{

    /**
     * Catalog data
     *
     * @var Data
     */
    protected $_catalogData = null;

    /**
     * @param Context $context
     * @param Data $catalogData
     * @param array $data
     */
    public function __construct(
        Context $context, 
        Data $catalogData, 
        Registry $registry,
        array $data = [])
    {
        $this->_catalogData = $catalogData; 
        $this->registry = $registry;
        parent::__construct($context, $data);
    }

    public function getCrumbs()
    {
        $evercrumbs = array();

        $evercrumbs[] = array(
            'label' => 'Home',
            'title' => 'Go to Home Page',
            'link' => $this->_storeManager->getStore()->getBaseUrl()
        );

        $path = $this->_catalogData->getBreadcrumbPath();
        $product = $this->registry->registry('current_product');
        $categoryCollection = clone $product->getCategoryCollection();
        $categoryCollection->clear();
        $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
        $categoryCollection->setPageSize(1);
        $breadcrumbCategories = $categoryCollection->getFirstItem()->getParentCategories();
        foreach ($breadcrumbCategories as $category) {
            $evercrumbs[] = array(
                'label' => $category->getName(),
                'title' => $category->getName(),
                'link' => $category->getUrl()
            );
        }


        $evercrumbs[] = array(
                'label' => $product->getName(),
                'title' => $product->getName(),
                'link' => ''
            );

        return $evercrumbs;
    }
}

之后,catalog_product_view.xml在模块中添加布局文件,路径应为:app\code\Vendor\Module\view\frontend\layout\catalog_product_view.xml

<?xml version="1.0"?>
<page>
    <body>
    <referenceBlock name="breadcrumbs" remove="true" />
    <referenceContainer name="page.top">
        <block class="Vendor\Module\Block\Crumbblock" name="crumbs" as="crumbs" template="Vendor_Module::crumbs.phtml" />
    </referenceContainer>
    </body>
</page>

之后,在模块路径中添加phtml文件应类似于: app\code\Vendor\Module\view\frontend\templates\crumbs.phtml

<?php $crumbs = $block->getCrumbs(); ?>
<?php if ($crumbs && is_array($crumbs)) : ?>
<div class="container">
    <div class="breadcrumbs">
        <ul class="items">
            <?php
            foreach ($crumbs as $crumbName => $crumbInfo) : ?>
                <li class="item <?php echo $crumbName == 0 ? "home" : ""; ?>">
                    <?php if ($crumbInfo['link']) : ?>
                        <a href="<?= /* @escapeNotVerified */
                        $crumbInfo['link'] ?>" title="<?= $block->escapeHtml($crumbInfo['title']) ?>">
                            <?= $block->escapeHtml($crumbInfo['label']) ?>
                        </a>
                    <?php else: ?>
                        <strong><?= $block->escapeHtml($crumbInfo['label']) ?></strong>
                    <?php endif; ?>
                </li>
            <?php
            endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

我希望这个能帮上忙

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.