【问题标题】:Magento: Display attribute name and value on category pageMagento:在类别页面上显示属性名称和值
【发布时间】:2015-04-11 06:52:49
【问题描述】:

我正在尝试在特定属性集 (id=9) 的类别页面上同时显示产品属性值和属性标签 (attribute label="Available Colours" 属性代码='available_colours')。

我目前正在使用以下代码来显示属性值,但似乎也无法显示属性标签。

<?php 
    if (9 == $_product->getAttributeSetId()) {
        echo $_product->getAvailableColours() 
    } 
?>

有什么建议吗?

【问题讨论】:

    标签: php magento entity-attribute-value


    【解决方案1】:

    以下应该会有所帮助:

        <?php 
            $attributeSetId = 9;
            if ($attributeSetId == $_product->getAttributeSetId()) {
                echo $this->__('Available Colours') . "=" . $_product->getAvailableColours(); 
            } 
        ?>
    

    如果你想打印多个属性,你可以试试下面的代码:

        <?php 
    $attributeSetId = 9;
        if ($attributeSetId == $_product->getAttributeSetId()) {
              $productData = $_product->getData();
    
              foreach($productData as $attributeCode=>$attributeValue) {
                   echo $attributeCode . "=" . $attributeValue; //You can do needed customization to check if attribute value is array then there will be a second loop here.
                }
        } 
        ?>  
    

    另外,最好不要使用静态 id,因为它可能会随服务器而变化。

    使用以下代码,您将能够从属性集名称动态加载属性集 id

    <?php
    
    $attributeSetName = "default"; // put your own attribute set name $attribute_set = 
    
    Mage::getModel("eav/entity_attribute_set")->getCollection(); 
    
    $attribute_set->addFieldToFilter("attribute_set_name",$attributeSetName)->getFirstItem(); 
    
    $attributeSetId = $attribute_set->getAttributeSetId(); //In your case you will get 9
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      相关资源
      最近更新 更多