Magento 2 Compare product is one of the native features available with Out of the box. You can compare multiple products from listing and product pages with a click on compare link.
If you want to know about get all the is_comparable attribute lists using programmatically you need to use Magento\Eav\Api\AttributeRepositoryInterface Interface.
Comparable on Storefront Set to Yes used for compare products in a storefront.
I have given a demo using Block file,
<?php namespace Rbj\Compare\Block; class Demo extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository, array $data = [] ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->attributeRepository = $attributeRepository; parent::__construct($context,$data); } public function getIsComparableAttribute() { try { $attributeSearchCriteria = $this->searchCriteriaBuilder->create(); $attributeRepository = $this->attributeRepository->getList( 'catalog_product', $attributeSearchCriteria ); $additionalAttributeCollection = []; foreach ($attributeRepository->getItems() as $item) { if ($item->getIsComparable()) { $additionalAttributeCollection[] = $item->getAttributeCode(); } } return $additionalAttributeCollection; } catch (\Exception $exception) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Such Attribute doesn\'t exist')); } return ''; }
From template file,
$productComparable = $block->getIsComparableAttribute(); foreach ($productComparable as $value) { echo $value;echo "<br>"; }
The result for Magento 2.3 With sample data installed, All is_comparable attribute code,
sku
description
short_description
manufacturer
activity