I am using Magento2.3.1 and I need to list and detail page current product if add compare product than getting list and count on list and detail page. So I try to detail and list page checkbox with compare link click than product add and remove on one link.

If anyone this type of functionality working code share with and appreciated for help.

i share with u screen this type showing enter image description here


1 Answer 1


In your block class

public function __construct(
    \Magento\Catalog\CustomerData\CompareProducts $compareProducts
) {
    $this->compareProducts = $compareProducts;

public function getComparedProductsList(){
    return $this->compareProducts->getSectionData();

Add below code in your phtml file

    $compareCollection = $block->getComparedProductsList();
<div class="compared-producr-items">
<?php foreach($compareCollection['items'] as $comparelist){                        
    <div class="compared-list">
        <a class="remove-item" data-href="<?php echo $block->getBaseUrl() ?>ajaxcompare/compare/remove/" data-compare="<?php echo $comparelist['id']; ?>">x</a>
        <span class="compare-item-name"><?php echo $comparelist['name']; ?></span>
<?php } ?>
  • not working on it this code.my requirement check it please
    – Rv Singh
    Sep 2, 2019 at 11:19
  • What error you are getting? Sep 2, 2019 at 11:22
  • my requiremnt is on list and detail page checbox icon with comapre link show and if unchecked compare link to remove it if laready compare on list.
    – Rv Singh
    Sep 2, 2019 at 11:24
  • if u have to customize and any other plugin please share me.
    – Rv Singh
    Sep 2, 2019 at 11:24
  • @RvSingh : Have you found any solution? I am also facing the same. Aug 4, 2020 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.