Skip to content

Prevent not category links in breadcrumbs at product page #14994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

vovayatsyuk
Copy link
Member

@vovayatsyuk vovayatsyuk commented May 4, 2018

Description

Magento 2.2.4 only.
When a product is opened from a homepage and menu has a link to the homepage,
two home links will be inserted into breadcrumbs.

Update: Added the case with "Contacts" link in the navigation.

Manual testing scenarios

  1. Open app/code/Magento/Theme/view/frontend/templates/html/topmenu.phtml
  2. Insert the following code:
<li class="level0 level-top" role="presentation">
    <a href="<?php echo $block->getUrl() ?>" class="level-top" tabindex="-1" role="menuitem"><span>Home</span></a>
</li>
<li class="level0 level-top" role="presentation">
    <a href="<?php echo $block->getUrl('contact') ?>" class="level-top" tabindex="-1" role="menuitem"><span>Contacts</span></a>
</li>

Right before

<?= /* @escapeNotVerified */ $_menu ?>
  1. Open homepage or contacts page
  2. Click on any product to navigate to its page (It may be a product in cart)
  3. Breadcrumbs will have two "Home" links, or Contacts page will be added into breadcrumbs.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

This may happen if menu has a link to the homepage, and
product is opened from the homepage
@VladimirZaets VladimirZaets self-assigned this May 4, 2018
@VladimirZaets
Copy link
Contributor

Hi, @vovayatsyuk , I took your PR into processing, thank you for collaboration.

@@ -193,6 +193,11 @@ define([
if (categoryUrl.indexOf('?') > 0) {
categoryUrl = categoryUrl.substr(0, categoryUrl.indexOf('?'));
}

// prevent double home link, when menu has link to the home
if (categoryUrl === BASE_URL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, you should change BASE_URL to window.BASE_URL according to Magento JS code style.
Also, we don't want use global variables, will be great if you pass base URL to the widget configuration.

@VladimirZaets
Copy link
Contributor

@vovayatsyuk thanks for fast update

@vovayatsyuk
Copy link
Member Author

vovayatsyuk commented May 4, 2018

Updated PR, because the same issue happens with other non-category links that may be added to navigation (Contacts, for example).

@vovayatsyuk
Copy link
Member Author

It still doesn’t work well, if non-category links where added programmatically into the tree.

They will have ‘nav-‘ css class just like category items and latest logic will detect them as category links.

:(

It looks like there is no way to detect if item from navigation is a category item or not.

Any thoughts?

@vovayatsyuk
Copy link
Member Author

Propose

Revert the changes and do the following instead:

  1. Add is_category_item flag into the Node
  2. Add category-item CSS class when rendering menu if Node has is_category_item flag
  3. Use category-item CSS class to detect if link found in navigation by breadcrumbs.js script is a category link.

This logic will cover both cases: links added manually into template and links added programmatically by third-party modules.

We can skip step 1 and use Node ID(category-node-123) in step 2 to check if this a category item. If it starts with category-node-, then it's a category, otherwise - custom link.

What do you think?

@vovayatsyuk
Copy link
Member Author

I've rewritten this PR completely because the previous solution does not work
with programmatically added items.

This change prevents adding custom links, like 'contacts', 'home', etc.into breadcrumbs
@vovayatsyuk vovayatsyuk force-pushed the fix-double-home-breadcrumbs branch from 0984c97 to 1df8452 Compare May 7, 2018 07:46
@vovayatsyuk vovayatsyuk changed the title Prevent double home links in breadcrumbs at product page Prevent not category links in breadcrumbs at product page May 7, 2018
@VladimirZaets
Copy link
Contributor

Hi @vovayatsyuk.

What do you think?

  • Yes, I looked the code, I think the solution is good, thanks.

@magento-engcom-team
Copy link
Contributor

Hi @vovayatsyuk. Thank you for your contribution.
Changes from your Pull Request will be available with the upcoming 2.2.5 release.

Please, consider porting this solution across release lines.
You may use Porting tool to port commits automatically.

vovayatsyuk pushed a commit to vovayatsyuk/magento2 that referenced this pull request May 24, 2018
magento-engcom-team added a commit that referenced this pull request May 25, 2018
…at product page #14994 #15480

 - Merge Pull Request #15480 from vovayatsyuk/magento2:2.3-fix-non-category-links-in-breadcrumbs
 - Merged commits:
   1. 22746f0
magento-engcom-team added a commit that referenced this pull request May 25, 2018
Accepted Public Pull Requests:
 - #15480: [Forwardport] Prevent not category links in breadcrumbs at product page #14994 (by @vovayatsyuk)
 - #14700: [2.3] Optimize ID to SKU lookup of tier prices (by @toddbc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants