magento 2-如何在产品列表和产品详细信息页面中获取属性集名称


Answers:


15

我们可以使用\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)。


$ attributeSet和$ product是未定义的变量,我是magento2的新手,我无法理解我到底需要写些什么
— Abhishek Dhanraj Shahdeo

您可以看到我的最新答案。够了吗?
— Khoa TruongDinh

我正在尝试在产品列表块中实现它,但它不能完全准确地进行一些修改
— Abhishek Dhanraj Shahdeo

我收到错误消息,应该创建对象dom
— Abhishek Dhanraj Shahdeo

您可以按照我的答案更新当前问题的答案。
— Khoa TruongDinh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.