我有一个选项类型的属性“account_status”。它有两个值“有效”和“无效”。我试图使用下面的代码来获取它的价值:
$customer->getAccountStatus(); //o/p is 10 i.e. option_id in eav_attribute_option table
但我想从eav_attribute_option_value表中获取值。
如何获得此值?
答案 0 :(得分:4)
获取$customer
模型
Mage::getResourceSingleton('customer/customer')
->getAttribute('account_status')
->getSource()->getOptionText($customer->getAccountStatus());
在您自己的回答中,您已经提供了获取属性的所有选项的方法。你也可以这样做
Mage::getResourceModel('customer/customer')
->getAttribute('account_status')
->getSource()
->getAllOptions();
获取选项值的选项ID
Mage::getResourceModel('customer/customer')
->getAttribute('account_status')
->getSource()
->getOptionId('my_value');
答案 1 :(得分:0)
我尝试了以下代码并且它正在运行..
$attribute = Mage::getModel('eav/config')->getAttribute('customer', 'account_status'); // "gender" is attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$genders[$instance['value']] = $instance['label'];
}
这是访问选项属性值的标准方法吗?
答案 2 :(得分:0)
这里很容易获得所有属性选项
$product = Mage::getModel('catalog/product');
$collection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code','attribute name');
$_attribute = $collection->getFirstItem()->setEntity($product->getResource());
$attribute_options = $_attribute->getSource()->getAllOptions(false);
print_r($attribute_options);
答案 3 :(得分:0)
请尝试使用以下代码获取客户地址属性下拉值。 (我的属性名称:address_name)
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn())
{
$addressesCollection = Mage::getSingleton('customer/session')->getCustomer();
foreach ($addressesCollection->getAddresses() as $addresses)
{
$address = Mage::getModel('customer/address')->load($addresses->getId());
$attr = Mage::getModel('customer/address')->getResource()->getAttribute('address_name');
if ($attr->usesSource())
{
$address_label = $attr->getSource()->getOptionText($address->getAddressName());
}
}
}
?>