我的属性代码数组,我需要得到的值:是否有更简单的方法来获取属性的前端值?
$attributeId = Mage::getResourceModel('eav/entity_attribute')
->getIdByCode('catalog_product', $attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->load($attributeId);
$value = $attribute->getFrontend()->getValue($product);
简单:
$attributes = array(
'Category' => 'type',
'Manufacturer' => 'brand',
'Title' => 'meta_title',
'Description' => 'description',
'Product Link' => 'url_path',
'Price' => 'price',
'Product-image link' => 'image',
'SKU' => 'sku',
'Stock' => 'qty',
'Condition' => 'condition',
'Shipping cost' => 'delivery_cost');
通过产品收集我得到的属性的前端值,像这样的迭代之后使用$product->getDate($attribute)
将无法使用下拉菜单和多选,它只返回它们的id而不是它们的前端值。
尽管上面的代码有效,但获取值似乎还有很长的路要走,但更重要的是它运行速度很慢。有没有更快捷/更明智的方式来获得产品属性的前端值?
编辑
我现在有以下(处理特殊情况下,像image
和qty
后),这是对眼睛更容易一点,似乎可以跑得快一点(虽然我不知道为什么):
$inputType = $product->getResource()
->getAttribute($attribute_code)
->getFrontend()
->getInputType();
switch ($inputType) {
case 'multiselect':
case 'select':
case 'dropdown':
$value = $product->getAttributeText($attribute_code);
if (is_array($value)) {
$value = implode(', ', $value);
}
break;
default:
$value = $product->getData($attribute_code);
break;
}
$attributesRow[] = $value;
如果有人能改善这个(使其更简单/更有效),请张贴一个答案。
看看这篇文章http://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-价值/ –
谢谢,有用的文章。 – Jamie