Question

I'm trying to remove the compare products from appearing on the product page. We're a small publisher, and I'm not sure what compare would look like between two books in our catalog.

I'm currently using 2.3.6 and I've found in my searches a number of suggested modifications to the code that would supposedly remove this. However, many of them appear to be for earlier versions of Magento 2, and either the files are no longer named the same or the code changes do not work for the later version. Could anyone provide a code change for the compare items removal specifically for 2.3.6?

Thank you, Jeff

Was it helpful?

Solution

The easiest way to do this is to remove compare-related blocks from your layout.

If you share your earlier attempt we can see if something's wrong with it. It's hard to guess your skill level from your question.

In a nutshell:

  • Create a module YourName/YourModuleName/ , including registration.php and etc/module.xml. For now I'll assume you know how to do this.

  • Create layout update files. YourName/YourModuleName/view/frontend/layout/[somelayout].xml. The name of the file should be that of an existing layout you'd like to modify.

  • Add this in the contents to remove blocks. Magento will merge it with existing layout 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>
            <referenceBlock name="[name of block to remove]" remove="true" />
            <referenceContainer name="[or maybe name of container to remove]" remove="true" />
        </body>
    </page>
  • To find out which blocks to remove and which layout names to use this will help you greatly. Use this command to find the layout xml files where blocks with names containing "compare" are included:

    egrep -r -i --include \*.xml --exclude-dir=var --exclude-dir=dev "<block".*?"name=" * | grep -I 'compare'

    To find out which other modules mess with those blocks use:

    egrep -r -i --include \*.xml --exclude-dir=var --exclude-dir=dev "<referenceBlock".*?"name=" * | grep -I 'compare'

  • Make sure to enable template hints on your development environment. It enables you to see which template files and blocks are used to display that content. It will not tell you the block names or file names, but it will help you understand the contents of the layout XML files you found using the grep commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top