我的屬性代碼數組,我需要得到的值:是否有更簡單的方法來獲取屬性的前端值?
$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