0

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());

1 Answer 1

0

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.

8
  • 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) {
    – Shaun
    Commented 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.
    – Shaun
    Commented Apr 19, 2020 at 11:40
  • you solve the issue ?? Commented Apr 19, 2020 at 11:42
  • No because I need the attribute 'manufacturer' to return a result. I was thinking maybe it was the way I loaded the products.
    – Shaun
    Commented Apr 19, 2020 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.