magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento

Today let’s talk about Magento and custom attributes. With Magento you can create as many custom attributes for your products as you like, and there are different attribute types such as:

  • Text Field
  • Text Area
  • Date
  • Yes/No
  • Multiple Select
  • Dropdown
  • Price
  • Gallery
  • Media Image
  • Fixed Product Tax

Because there are different types of attributes there are also different ways of obtaining an attributes value. If we were to create a new custom attribute called “theme_color ” and the attribute was of a “Text Field ” type, we could do something like the following at a product level (meaning a product is loaded) to obtain its value.

<?php echo $_product->getThemeColor(); ?>
 

$_product is the product of question we have loaded up and ready to work with, and we use “getThemeColor(); ” because our attribute was named “theme_color ”. It’s important to realize that when calling out the value of an attribute with this method, we start with “get” and then capitalize the first letter of each word that is divided by a “_”.

 

Now this method would give us the value of a simple “Text Field ” attribute but what if the attribute was a “Dropdown or a Multiple Select ” type? Well if you try to use this “getYourAttributesName() ” method on a none “Text Field ” or “Textarea ” attribute type you will most likely get nothing.

 

So how do we obtain the value of a “Dropdown or a Multiple Select ” attribute type you ask? Well some would argue that you would do so via this method:

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

However, what this would do is return an array of the “Dropdown or Multiple Select ” options instead of the value of the selected option you would have set. There is another way to obtain these types of attributes selected value and it looks like this:

<?php
$attributes = $_product->getAttributes();
$themeColor = $attributes['theme_color']->getFrontend()->getValue($_product);
echo $themeColor;
?>
 

With this method we are simply creating a new variable called “$attributes ” and pointing to our “$_product ” to then using the “getAttributes() ” function to gather all the attributes of this product.

Once we have all the product’s attributes inside of the “$attributes ” variable, we make another new variable called “$themeColor ”. “$themeColor ” then assigns its self to our “$attributes ” array that we in turn call out our custom attribute [‘theme_color’] .

 

Once we have pointed to the custom attribute in the “$attributes ” array, we simply run through the front end via the function “getFrontend() ” and then pull the value down via “getValue($_product) ”.

If we then echo our “$themeColor ” you should get the value of your custom Drop Down or Multiple Select attribute to do what you will with it.

 

So to recap:

<?php echo $_product->getThemeColor() ?>
 

Would get the value of an attribute wholes type is of Text Field or Text Area’s

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

That will not give a value of a Text Field or Text Area attribute type but rather gather an array of all options in a Drop Down or Multiple Select attribute type.

<?php
$attributes = $_product->getAttributes();
$themeColor = $attributes['theme_color']->getFrontend()->getValue($_product);
?>
 

That will give you the value of any type of attribute, even the value from “Drop Down or Multiple Select ” attribute types.

 

I hope this clears some things up and helps some developers out there .

 

Thanks!

 

 

来源:http://www.devinrolsen.com/magento-custom-attributes-value/

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值