I like to retrieve compared product collections in homepage template. Is there any method to retrieve the data?


Please try with below code :

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

    * Get current compare product list
public function getCompareList(){
    return $this->compareProducts->getSectionData();

create one phtml file and then load in homepage container via xml.

    $compareObject = $block->getCompareList();
<div class="compare-items">
<?php foreach($compareObject['items'] as $comparelist){                        
    <div class="compProd">
        <a class="closeComp" data-href="<?php echo $block->getBaseUrl() ?>ajaxcompare/compare/remove/" data-compare="<?php echo $comparelist['id']; ?>">x</a>
        <span class="gBold cmPrd"><?php echo $comparelist['name']; ?></span>
<?php } ?>

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.