magento 2 - Come ottenere il nome del set di attributi nell'elenco dei prodotti e nella pagina dei dettagli del prodotto


Risposte:


15

Possiamo usare \Magento\Eav\Api\AttributeSetRepositoryInterfaceper ottenere il nome del set di attributi.

Pagina dei dettagli

Dobbiamo scavalcare il \Magento\Catalog\Block\Product\Viewblocco. Iniettare questa classe sul costruttore

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

Ora possiamo chiamare nella pagina dei dettagli del prodotto: $block->getAttributeSetName();

Pagina di elenco

Dobbiamo scavalcare il \Magento\Catalog\Block\Product\ListProductblocco

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

Possiamo chiamare $block->getAttributeSetName($_product).


$ AttribSet e $ product sono variabili indefinite, sono molto nuovo in magento2 e non sono in grado di capire esattamente cosa devo scrivere
— Abhishek Dhanraj Shahdeo,

Puoi vedere la mia risposta aggiornata. Abbastanza per te?
— Khoa TruongDinh,

Sto cercando di implementarlo nel blocco dell'elenco prodotti, ma non funziona esattamente, facendo alcune modifiche
— Abhishek Dhanraj Shahdeo

Sto ottenendo l'errore oggetto dom dovrebbe essere creato
— Abhishek Dhanraj Shahdeo

Puoi aggiornare la tua risposta con il problema attuale quando segui la mia risposta.
— Khoa TruongDinh,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.