magento 2: cómo obtener el nombre del conjunto de atributos en la lista de productos y la página de detalles


Respuestas:


15

Podemos usar \Magento\Eav\Api\AttributeSetRepositoryInterfacepara obtener el nombre del conjunto de atributos.

Página de detalles

Necesitamos anular el \Magento\Catalog\Block\Product\Viewbloque. Inyecte esta clase en el constructor.

/** @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();
}

Ahora, podemos llamar a la página de detalles del producto: $block->getAttributeSetName();

Página de listado

Necesitamos anular el \Magento\Catalog\Block\Product\ListProductbloque

/** @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();
}

Podemos llamar $block->getAttributeSetName($_product).


$ attributeSet y $ product son variables indefinidas, soy muy nuevo en magento2 y no puedo entender exactamente qué necesito escribir
— Abhishek Dhanraj Shahdeo

Puedes ver mi respuesta actualizada. ¿Suficiente para ti?
— Khoa TruongDinh

Estoy tratando de implementarlo en el bloque de la lista de productos, pero no funciona como exacto, haciendo algunas modificaciones
— Abhishek Dhanraj Shahdeo

Estoy obteniendo un objeto de error dom debería ser creado
— Abhishek Dhanraj Shahdeo

Puede actualizar su respuesta con el problema actual al seguir mi respuesta.
— Khoa TruongDinh
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.