我一直在做frontend magento一段时间,但刚刚开始构建模块。这是我知道如何做前端但我在我的模块中苦苦挣扎的事情。我现在想要实现的目标是在管理员中使用所有可用的产品属性填充多选。在所有产品属性集中包含自定义产品属性。我不完全确定这将需要什么表,因为我不想假设启用了Flat Category Data。
我在系统配置的新选项卡中创建了我的管理区域,我创建了一个多选字段,当前正在填充三个静态选项。这很有用。任何人都可以通过指向正确的方向来帮助我......目前这是我迄今为止所拥有的(为了它的价值)。
<?php
class test_test_Model_Source
{
public function toOptionArray()
{
return array(
array('value' => 0, 'label' =>'First item'),
array('value' => 1, 'label' => 'Second item'),
array('value' => 2, 'label' =>'third item'),
);
}
}
/////////////////////////////编辑///////////////// ////////////////////
我觉得我可能会在这里找到一些东西,但它只返回每个属性的第一个字母(所以我不确定它的属性是否会返回)
public function toOptionArray()
{
$attributes = Mage::getModel('catalog/product')->getAttributes();
$attributeArray = array();
foreach($attributes as $a){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
return $attributeArray;
}
/////////////////////////////////编辑///////////// /////////////////////////
我不是非常接近,因为我现在知道数组正在返回我想要的东西,所有的attribute_codes。然而它仍然只输出每个的第一个字母......任何人都知道为什么?
public function toOptionArray()
{
$attributes = Mage::getModel('catalog/product')->getAttributes();
$attributeArray = array();
foreach($attributes as $a){
foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) {
$attributeArray[$attributeName] = $attributeName;
}
break;
}
return $attributeArray;
}
答案 0 :(得分:6)
我已经回答了我自己的问题。我找到了一种方法,但是我不知道为什么,所以如果有人可以评论和解释那将是有用的。所以虽然有$ attributeArray [$ attributeName] = $ attributeName;当你返回一个print_r时,当你返回数组时,它只提供了第一个字母。但是,如果你执行以下操作,我认为这似乎完全相同,它的工作原理。我只能想象,当渲染时,它并不期待字符串,而是其他东西。无论如何,这是代码:
public function toOptionArray()
{
$attributes = Mage::getModel('catalog/product')->getAttributes();
$attributeArray = array();
foreach($attributes as $a){
foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) {
//$attributeArray[$attributeName] = $attributeName;
$attributeArray[] = array(
'label' => $attributeName,
'value' => $attributeName
);
}
break;
}
return $attributeArray;
}
答案 1 :(得分:4)
无需像Frank Clark建议的那样进行额外的循环。只需使用:
public function toOptionArray()
{
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')->addVisibleFilter();
$attributeArray = array();
foreach($attributes as $attribute){
$attributeArray[] = array(
'label' => $attribute->getData('frontend_label'),
'value' => $attribute->getData('attribute_code')
);
}
return $attributeArray;
}
答案 2 :(得分:1)
您可以尝试以其他方式获取属性,例如
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
拥有属性后,您可以通过这种方式获取选项(从magento代码复制)
$result = array();
foreach($attributes as $attribute){
foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) {
if($option['value']!='') {
$result[$option['value']] = $option['label'];
}
}
}