1

I want to check if products is already in compare list on product listing page and show remove compare button there

below is come of my list.phtml

Add to compare

<a  id="compareclick-<?php echo $product->getId();?>" href="#"
                               class="action compare"
                               title="<?php echo $block->escapeHtml(__('+ Compare')); ?>"
                               aria-label="<?php echo $block->escapeHtml(__('+ Compare')); ?>"
                               data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
                               role="button">
                                <span><?php /* @escapeNotVerified */ echo __('+ Compare') ?></span>
                            </a>

Remove from compare

                        <a id="rxcompareclick-<?php echo $product->getId();?>" href="#"
                           class="action compare"
                           title="<?php echo $block->escapeHtml(__('+ Compare')); ?>"
                           aria-label="<?php echo $block->escapeHtml(__('+ Compare')); ?>"
                           data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataRemove($_product); ?>'
                           role="button">
                            <span><?php /* @escapeNotVerified */ echo __('+ Compare') ?></span>
                        </a>

I want to add condition so remove code should show If product in compare list

1
  • Have you found a solution for this?
    – JGeer
    Commented Jun 25, 2021 at 8:01

2 Answers 2

0

{theme_dir}/Magento_Catalog/templates/product/compare/link.phtml.

do this by calling the Magento\Catalog\Helper\Product\Compare helper's method hasItems()

<?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
if ($compareHelper->hasItems()) { ?>
    //default Magento code
    <li class="item link compare" data-bind="scope: 'compareProducts'" data-role="compare-products-link">
        <a class="action compare no-display" title="<?php echo __('Compare Products') ?>"
           data-bind="attr: {'href': compareProducts().listUrl}, css: {'no-display': !compareProducts().count}"
        >
            <?php echo __('Compare Products') ?>
            (<span class="counter qty" data-bind="text: compareProducts().countCaption"></span>)
        </a>
    </li>
    <script type="text/x-magento-init">
        {"[data-role=compare-products-link]": {"Magento_Ui/js/core/app": <?php echo $block->getJsLayout();?>}}
    </script>
    //end of default Magento code
<?php } ?>
1
  • This doesn't work, I want the same thing as asked. On the product list page (category page) standard it shows an "add to compare" button but when added to the compare list I want the "add to compare" button to be checked or replace it with an "remove from compare" button. In the module-catalog/helper/product/compare.php I also don't see the option of checking if product is added to the compare ..
    – Riccardo
    Commented Nov 16, 2020 at 21:20
0

You may do it using Ko conditions

<li class="item link compare" data-bind="scope: 'compareProducts'" data-role="compare-products-link">
<!-- ko if: compareProducts().count -->
    <a class="action compare no-display" title="<?php echo __('Compare Products') ?>"
       data-bind="attr: {'href': compareProducts().listUrl}, css: {'no-display': !compareProducts().count}"
    >
        <?php echo __('Compare Products') ?>
        (<span class="counter qty" data-bind="text: compareProducts().countCaption"></span>)
    </a>
<!-- /ko -->
</li>
<script type="text/x-magento-init">
    {"[data-role=compare-products-link]": {"Magento_Ui/js/core/app": <?php echo $block->getJsLayout();?>}}
</script>

Your Answer

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

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