我有一组属性代码,我需要获取以下值:
$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');
遍历产品集合后,我得到属性的前端值,如下所示:
$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);
简单地使用$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;
如果有人可以改进这一点(使其更简单/更高效),请发布答案。