getSku works but getAttributeText doesn't? I have tried setting sku as the attribute in AttributeText as I know it returns a result. Also tried getManufacturer.
$xml .= $this->createNode("g:brand", $this->getAttributeText($product, 'manufacturer'));
$xml .= $this->createNode("g:mpn", $product->getSku());
Add a comment
|
1 Answer
Please try this code may be help you :---
First you have to load the products
$_productCollection = $block->getLoadedProductCollection();
In the product listing page ,there will be a foreach loop for listing the products like this
foreach ($_productCollection as $_product)
Your code will be inside this loop.Place the below code at a place where ever you want to display the attribute label.
$_product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($_product);
And more refer this very helpful :--
how can i get the the attribute label from the attribute value in magento 2?
Thanks.
-
Hi below is how i have loaded the products. Your code works for attribute text but only for sku nothing else? Attributes visible on storefront set to yes.
public function getProductsXml() { $productCollection = $this->_productFeedHelper->getFilteredProducts(); $xml = ""; foreach ($productCollection as $product) { $xml .= "<item>" . $this->buildProductXml($product) . "</item>"; } return $xml; } public function buildProductXml($product) {
– ShaunCommented Apr 19, 2020 at 10:26 -
i don't understand your above comment so Please explain briefly... Commented Apr 19, 2020 at 11:16
-
$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product);
returns no results.$product->getResource()->getAttribute('sku')->getFrontend()->getValue($product);
returns result as expected.– ShaunCommented Apr 19, 2020 at 11:40 -
-
No because I need the attribute 'manufacturer' to return a result. I was thinking maybe it was the way I loaded the products.– ShaunCommented Apr 19, 2020 at 11:48