如何从magento 2中的属性值中获取属性标签?

时间:2016-10-18 11:56:16

标签: attributes product magento2

我从下面的代码

获得了属性值232
$_product->getData('farben');

我如何从这个值中获得标签?

4 个答案:

答案 0 :(得分:4)

尝试

$_product->getAttributeText('farben');

答案 1 :(得分:1)

请尝试此代码

步骤1)首先,您必须加载产品

$_productCollection = $block->getLoadedProductCollection();

步骤2)在产品列表页面中,将有一个foreach循环用于列出此类产品

foreach ($_productCollection as $_product)

Step3)你的代码将在这个循环中。将下面的代码放在你想要显示属性标签的地方。

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

只需将 your_attribute_code 替换为您的任何属性。

答案 2 :(得分:0)

$passing_marks = $_product->getResource()->getAttribute('attribute_code');

$passing_marks->getFrontend()->getLabel($_product);

答案 3 :(得分:0)

尝试一下

<?php $_product = $block->getProduct(); ?>
<?php if (null !== $_product->getCustomAttribute('attribute_code')): ?>
    <?= $_product->getAttributeText('attribute_code'); ?>
<?php endif; ?>
相关问题