简体   繁体   English

Magento getAttribute 在 list.phtml 中不起作用?

[英]Magento getAttribute is not working in list.phtml?

This is magento code that i have used to get attribute.这是我用来获取属性的 magento 代码。

  echo $_product->getResource()->getAttribute('attrcode')->getFrontend()->getValue($_product);

This code is working fine in view.phtml it return to attribute code value.此代码在view.phtml工作正常,它返回属性代码值。

when i write same code in list.phtml file this code is return blank .当我在list.phtml文件中编写相同的代码时,此代码返回blank

where i do mistake.我做错的地方。 please help.请帮忙。

<?php echo $_product->getAttributeText('attrcode');?>

请在列表页面上使用该代码并检查属性设置('用于产品列表':“是”)

Here is the code get attribute name and value that that doesn't belongs to any product这是获取不属于任何产品的属性名称和值的代码

$attributeCode = 'YOUR_ATTRIBUTE_CODE';

$product = Mage::getModel('catalog/product');

$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeCode);

$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes

$options = $attribute->getSource()->getAllOptions(false);
print_r($options);

If you want to display all the values of a specific attribute in Magento 2 phtml file, then use following code如果要在 Magento 2 phtml 文件中显示特定属性的所有值,请使用以下代码

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product','attribute_code');
$values = $attribute->getSource()->getAllOptions();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM