3

I am trying to get compare url of a product.

For this i am using:

<?php echo $_compareUrl=$this->helper('Magento\Catalog\Helper\Product\Compare')->getAddUrl($_product); ?>

Its output is http://localhost/my_magento_folder/catalog/product_compare/add/.

I am not getting the compare url with product id. It should be like http://localhost/my_magento_folder/catalog/product_compare/add/product/3723/uenc/aHR0cDovL2xvY2FsaG9zdC90d3Avd2lyZS1tZXNoLW1hdGVyaWFsL2JyYXNz/.

Please help me. Thanks

0

1 Answer 1

2

When you look at compare.phtml from below file path

magento\vendor\magento\module-catalog\view\frontend\templates\product\list\addto\compare.phtml

<a href="#"
   class="action tocompare"
   title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
   aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
   data-post='<?php /* @escapeNotVerified */ echo $block->getCompareHelper()->getPostDataParams($block->getProduct()); ?>'
   role="button">
    <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
</a>

It means they are using getPostDataParams() method from compare helper, so you can also use same method to get the compare URL. Like below

<?php echo $_compareUrl=$this->helper('Magento\Catalog\Helper\Product\Compare')->getPostDataParams($_product); ?>

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.