3

I am using the getCategories() function below:

use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Context;
class Categories extends AbstractProduct
{
 private $logger;
 public function __construct(Context $context,
 \Psr\Log\LoggerInterface $logger,
 array $data = [])
 {
 $this->logger = $logger;
 parent::__construct($context, $data);
 }
 public function getCategories()
 {
 $product = $this->getProduct();
 $cats = $product->getCategoryCollection()
 ->addAttributeToSelect("name")
 ->addIsActiveFilter();
 return $cats;
 }
}

The layout file catalog_product_view.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceContainer name="product.info.main">
 <block class="Company\ProductCategories\Block\Product\View\Categories"
 name="product.view.categories"
 template="Company_ProductCategories::product/view/categories.phtml"
 before="product.social.links">
 </block>
 </referenceContainer>
 </body>
</page>

And the template file categories.phtml:

<?php
 /** @var \Magento\Catalog\Model\Category $categories */
 $categories = $this->getCategories();
?>
<?php if ($categories->getSize() > 0): ?>
<div id="product.view.categories">
 <p>Categories:
 <?php foreach($categories as $category): ?>
 <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a>
 <?php endforeach; ?>
 </p>
</div>
<?php endif; ?>

So this returns the category names when going to a product from a category page.

However when I go to a product view from the front page through a new or latest products block, it does not add the category names to the results. It also doesn't add the proper path of breadcrumbs.

Just a bit more info the url when navigating from the catalog page:

http://shop.dev/ammunition/airgun/exact-airgun-pellets-44gr-500.html

Where ammunition and airgun are the categories.

when going from the home page the url is just:

http://shop.dev/exact-airgun-pellets-44gr-500.html

I tested this on a standard magento install and whether going to the page from a catalog page or the home page the url is the same: http://magento2.dev/index.php/hero-hoodie.html and the categories show.

Why is that on this porto theme, there is an issue?

Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Jan 18, 2017 at 12:00
8
  • Which block class you use? Commented Jan 18, 2017 at 15:37
  • I am using a custom module block that extends abstractProduct Commented Jan 18, 2017 at 17:32
  • Can you give more detail with code? Commented Jan 18, 2017 at 17:35
  • @SohelRana I have updated the code. Please take a look. Commented Jan 19, 2017 at 7:02
  • Can you show your layout xml file? Commented Jan 19, 2017 at 7:19

1 Answer 1

1

Just to confirm, I've tested your code on a fresh Magento 2.1.2 and it works absolutely fine in both cases (when accessing the product via its direct URL or when accessing the product via the category).

Regarding your second question: yes I'm 90% sure the Porto theme is the problem. All those themes are shipped with a huge number of widgets, modules, custom modifications which are very often the root cause of many issues.

I'm not saying those themes are bad, they're great when you're dealing with a small store which do not need a lot of custom development and modifications. However, when you're dealing with a bigger structure, problems arise.

answered Jan 24, 2017 at 9:54
1
  • What is strange is that I can get the category url with ->getUrl()... Commented Jan 27, 2017 at 7:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.