magento 2 - Cara mendapatkan atribut set nama di cantuman produk dan halaman detail produk


Jawaban:


15

Kita bisa menggunakan \Magento\Eav\Api\AttributeSetRepositoryInterfaceuntuk mendapatkan nama atribut.

Halaman Detail

Kita perlu mengganti \Magento\Catalog\Block\Product\Viewblok. Suntikkan kelas ini pada konstruktor

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

Sekarang, kita dapat menghubungi halaman detail produk: $block->getAttributeSetName();

Halaman daftar

Kita perlu mengganti \Magento\Catalog\Block\Product\ListProductblok

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

Kita bisa menelepon $block->getAttributeSetName($_product).


$ attributeSet dan $ product adalah variabel yang tidak terdefinisi, saya sangat baru di Magento2 dan saya tidak dapat mengerti apa yang sebenarnya saya perlu tulis
— Abhishek Dhanraj Shahdeo

Anda dapat melihat jawaban saya yang diperbarui. Cukup untukmu?
— Khoa TruongDinh

Saya mencoba menerapkannya di blok daftar produk, tetapi tidak berfungsi dengan tepat, membuat beberapa modifikasi
— Abhishek Dhanraj Shahdeo

Saya mendapatkan kesalahan objek, dom harus dibuat
— Abhishek Dhanraj Shahdeo

Anda dapat memperbarui jawaban Anda dengan masalah saat ini ketika mengikuti jawaban saya.
— Khoa TruongDinh
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.