0

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

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 } ?>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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