Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. <?php
  2. namespace VendorModuleBlock;
  3.  
  4. use MagentoCatalogHelperData;
  5. use MagentoFrameworkViewElementTemplateContext;
  6. use MagentoStoreModelStore;
  7. use MagentoFrameworkRegistry;
  8.  
  9. class Crumbblock extends MagentoFrameworkViewElementTemplate
  10. {
  11.  
  12. /**
  13. * Catalog data
  14. *
  15. * @var Data
  16. */
  17. protected $_catalogData = null;
  18.  
  19. /**
  20. * @param Context $context
  21. * @param Data $catalogData
  22. * @param array $data
  23. */
  24. public function __construct(
  25. Context $context,
  26. Data $catalogData,
  27. Registry $registry,
  28. array $data = [])
  29. {
  30. $this->_catalogData = $catalogData;
  31. $this->registry = $registry;
  32. parent::__construct($context, $data);
  33. }
  34.  
  35. public function getCrumbs()
  36. {
  37. $evercrumbs = array();
  38.  
  39. $evercrumbs[] = array(
  40. 'label' => 'Home',
  41. 'title' => 'Go to Home Page',
  42. 'link' => $this->_storeManager->getStore()->getBaseUrl()
  43. );
  44.  
  45. $path = $this->_catalogData->getBreadcrumbPath();
  46. $product = $this->registry->registry('current_product');
  47. $categoryCollection = clone $product->getCategoryCollection();
  48. $categoryCollection->clear();
  49. $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
  50. $categoryCollection->setPageSize(1);
  51. $breadcrumbCategories = $categoryCollection->getFirstItem()->getParentCategories();
  52. foreach ($breadcrumbCategories as $category) {
  53. $evercrumbs[] = array(
  54. 'label' => $category->getName(),
  55. 'title' => $category->getName(),
  56. 'link' => $category->getUrl()
  57. );
  58. }
  59.  
  60.  
  61. $evercrumbs[] = array(
  62. 'label' => $product->getName(),
  63. 'title' => $product->getName(),
  64. 'link' => ''
  65. );
  66.  
  67. return $evercrumbs;
  68. }
  69. }
  70.  
  71. <?xml version="1.0"?>
  72. <page>
  73. <body>
  74. <referenceBlock name="breadcrumbs" remove="true" />
  75. <referenceContainer name="page.top">
  76. <block class="VendorModuleBlockCrumbblock" name="crumbs" as="crumbs" template="Vendor_Module::crumbs.phtml" />
  77. </referenceContainer>
  78. </body>
  79. </page>
  80.  
  81. <?php $crumbs = $block->getCrumbs(); ?>
  82. <?php if ($crumbs && is_array($crumbs)) : ?>
  83. <div class="container">
  84. <div class="breadcrumbs">
  85. <ul class="items">
  86. <?php
  87. foreach ($crumbs as $crumbName => $crumbInfo) : ?>
  88. <li class="item <?php echo $crumbName == 0 ? "home" : ""; ?>">
  89. <?php if ($crumbInfo['link']) : ?>
  90. <a href="<?= /* @escapeNotVerified */
  91. $crumbInfo['link'] ?>" title="<?= $block->escapeHtml($crumbInfo['title']) ?>">
  92. <?= $block->escapeHtml($crumbInfo['label']) ?>
  93. </a>
  94. <?php else: ?>
  95. <strong><?= $block->escapeHtml($crumbInfo['label']) ?></strong>
  96. <?php endif; ?>
  97. </li>
  98. <?php
  99. endforeach; ?>
  100. </ul>
  101. </div>
  102. </div>
  103. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement