php Magento如何通过选项ID获取属性值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22987787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:41  来源:igfitidea点击:

Magento how to get attribute value by option id?

phpmagento

提问by Damian

So I have

所以我有

$attribute_option_id = Mage::getResourceModel('catalog/product')
->getAttributeRawValue($productId, 'my_attribute', $storeId);

This gives me something like $attribute_option_id = 12345

这给了我类似的东西 $attribute_option_id = 12345

Now how do I get the (text) value for this id?

现在如何获取此 ID 的(文本)值?

Thanks

谢谢

回答by Marius

This should work.

这应该有效。

$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
$text = $product->getAttributeText('my_attribute');

[EDIT]
If you don't want to load the full product you can do a sneaky think. Impersonate a product.

[编辑]
如果你不想加载完整的产品,你可以偷偷摸摸地思考。冒充产品。

So you get the option id like you already do

所以你会像你已经做的那样得到选项 id

$attribute_option_id = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'my_attribute', $storeId);

Then create just an empty instance of the product and set some attributes to it.

然后只创建产品的一个空实例并为其设置一些属性。

$product = Mage::getModel('catalog/product')
    ->setStoreId($storeId)
    ->setData('my_attribute', $attribute_option_id);//the result from above

$text = $product->getAttributeText('my_attribute');

There is already a confirmation that this works. See it here.

已经确认这是有效的。在这里看到它

回答by Joel Mellon

Here is a method that is a different approach that works for me.

这是一种对我有用的不同方法。

Assume Known: $_current_productid

假设已知:$_current_productid

$_Current_OptionId =  Mage::getResourceModel('catalog/product')->getAttributeRawValue($_current_productid, $attribute_id,$store_id);

$_write = Mage::getSingleton('core/resource')->getConnection('core_write');

// now $_write is an instance of Zend_Db_Adapter_Abstract

$_readresult=$_write->query("SELECT value FROM eav_attribute_option_value WHERE option_id ='".$_Current_OptionId."'"); 

while ($row = $_readresult->fetch() ) {$_Current_OptionValue=$row['value'];}