Magento获取产品自定义属性及对应的值

自定义产品属性后,通常可以设置其是否在产品查看页中显示,可显属性将会出现在additional information 中。但是这边我需要在分类列表页中调用产品特定的属性。

 

在列表页循环获取对应产品的id之后,直接输出产品的所有属性内容会太多,getData等取出的信息又过于简洁。总之用起来不是很爽,于是根据 app/code/core/Mage/Catalog/Block/Product/View/Attributes.php中的 getAdditionalData 方法进行了一些简单的修改, 新方法还是放在getAdditionalData 同一文件中,因为有些继承的方法要用。 代码如下

public function getMeSetData($t_product)
{
    $data = array();
    $product = $t_product;
    $attributes = $product->getAttributes();
    foreach ($attributes as $attribute) {
        $value = $attribute->getFrontend()->getValue($product);
 
        if (!$product->hasData($attribute->getAttributeCode())) {
            $value = Mage::helper('catalog')->__('N/A');
        } elseif ((string)$value == '') {
            $value = Mage::helper('catalog')->__('No');
        } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
            $value = Mage::app()->getStore()->convertPrice($value, true);
        }
 
        if (is_string($value) && strlen($value)) {
            $data[$attribute->getAttributeCode()] = array(
                'label' => $attribute->getStoreLabel(),
                'value' => $value,
                'code'  => $attribute->getAttributeCode()
            );
        }
    }
    return $data;
}
 

可以看到这边的参数是一个产品对象,但是测试的时候发现,列表页直接使用的产品对象信息并不够全,也无法使用这个方法,所以在获取产品id后重新加载了一下对应的产品,调用方法如下

$p_id = $_product->getId();
$_product = Mage::getModel('catalog/product')->load($p_id);
$p_attrs = Mage::getBlockSingleton('catalog/product_view_attributes')->getMeSetData($_product);
print_r($p_attrs);
 

这样取出的产品属性信息就比较全了,也不会过于臃肿。具体用途  各取所需吧

 

 

来源:http://vsfor.com/archives/323

 

 

最全面的:

$attributes = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();

// Localize attribute label (if you need it)
$attributes->addStoreLabel(Mage::app()->getStore()->getId());

// Loop over all attributes
foreach ($attributes as $attr) {
    /* @var $attr Mage_Eav_Model_Entity_Attribute */
    // get the store label value
    $label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
    echo "Attribute: {$label}\n<br>";

    // If it is an attribute with predefined values
    if ($attr->usesSource()) {

        // Get all option values ans labels
        $options = $attr->getSource()->getAllOptions();

        // Output all option labels and values
        foreach ($options as $option)
        {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;{$option['label']} =========> (Value {$option['value']})\n<br>";
        }
    }
    else
    {
        // Just for clarification of the debug code
        echo "    No select or multiselect attribute\n<br>";
    }
}
 

来源: http://stackoverflow.com/questions/9275826/how-to-get-all-active-attributes-of-products-in-magento

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值