We want to implement the attribute group name on the compare product page and separate all attributes based on the attribute group.
We use the following code to show the attribute group name on the specifications section on the product page.
How can we implement this on the product compare page?
CODE;
<?php
namespace AttributeGroup\Module\Block\Product;
class Specification extends \Magento\Catalog\Block\Product\View
{
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
*/
protected $groupCollectionFactory;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
\Magento\Framework\Locale\FormatInterface $localeFormat,
\Magento\Customer\Model\Session $customerSession,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
array $data = []
) {
parent::__construct($context, $urlEncoder, $jsonEncoder, $string,
$productHelper, $productTypeConfig, $localeFormat, $customerSession,
$productRepository, $priceCurrency, $data);
$this->groupCollectionFactory = $groupCollectionFactory;
}
public function getAttributeLabels()
{
$product = $this->getProduct();
$attributeSetId = $product->getAttributeSetId();
$groupCollection = $this->groupCollectionFactory->create()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();
$html = '';
foreach ($groupCollection as $group) {
$attributes = $product->getAttributes($group->getId(), true);
$new_html = "";
foreach ($attributes as $key => $attribute) {
if($attribute->getIsVisibleOnFront() && $attribute->getFrontend()->getValue($product) !="" && $attribute->getFrontend()->getValue($product) !="Non"){
$new_html .= '<div class="product-specs-list-item">
<dt class="product-specs-item-title">' . $attribute->getStoreLabel(). '</dt>
<dl class="product-specs-item-spec">' . $attribute->getFrontend()->getValue($product) . '</dl>
</div>';
}
}
if($new_html!=''){
$html .= "<div class='grid-section-xs-specification active'>";
$html .= '<h3 class="noselect toggle-specification">' . $group->getData('attribute_group_name').'</h3>';
$html .= "<dl class='product-specs'>";
$html .= $new_html;
$html .= "</dl>
</div>";
}
}
return $html;
}
}?>