magento 2 - Comment obtenir le nom de l'ensemble d'attributs dans la liste des produits et la page de détails du produit


Réponses:


15

Nous pouvons utiliser \Magento\Eav\Api\AttributeSetRepositoryInterfacepour obtenir le nom de l'ensemble d'attributs.

Page de détail

Nous devons remplacer le \Magento\Catalog\Block\Product\Viewbloc. Injectez cette classe sur le constructeur

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

Maintenant, nous pouvons appeler la page de détails du produit: $block->getAttributeSetName();

Page de cotation

Nous devons remplacer le \Magento\Catalog\Block\Product\ListProductbloc

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

On peut appeler $block->getAttributeSetName($_product).


$ attributeSet et $ product sont des variables non définies, je suis très nouveau sur magento2 et je ne suis pas en mesure de comprendre exactement ce que je dois écrire
— Abhishek Dhanraj Shahdeo

Vous pouvez voir ma réponse mise à jour. Assez pour toi?
— Khoa TruongDinh

J'essaie de l'implémenter dans le bloc de liste de produits, mais cela ne fonctionne pas aussi exactement, faisant quelques modifications
— Abhishek Dhanraj Shahdeo

Je reçois un objet d'erreur dom doit être créé
— Abhishek Dhanraj Shahdeo

Vous pouvez mettre à jour votre réponse avec le problème actuel en suivant ma réponse.
— Khoa TruongDinh
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.