我们如何检索产品的属性集名称。我想在产品详细信息和列表页面上使用它。
我们如何检索产品的属性集名称。我想在产品详细信息和列表页面上使用它。
Answers:
我们可以使用\Magento\Eav\Api\AttributeSetRepositoryInterface获取属性集名称。
我们需要覆盖该\Magento\Catalog\Block\Product\View块。在构造器上注入此类
/** @var \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet **/
protected $attributeSet;
public function __construct(
    ......
    \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet
    ......
) {
   ......
   $this->attributeSet = $attributeSet;
}
//Build method to get attribute set
public function getAttributeSetName() {
    $product = $this->getProduct();
    $attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
    return $attributeSetRepository->getAttributeSetName();
}现在,我们可以调用产品详细信息页面: $block->getAttributeSetName();
我们需要覆盖\Magento\Catalog\Block\Product\ListProduct块
/** @var \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet **/
protected $attributeSet;
public function __construct(
    ......
    \Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet
    ......
) {
   ......
   $this->attributeSet = $attributeSet;
}
public function getAttributeSetName($product) {
    $attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
    return $attributeSetRepository->getAttributeSetName();
}我们可以打电话给$block->getAttributeSetName($_product)。