[英]How to displa a custom attribute value in catalog product grid in magento?
我為產品“ sellable_in_market”創建了一個自定義屬性。 並嘗試將其顯示在產品網格中。 但是該列為空。 但是,如果我使用“是/否”進行過濾,則會顯示出來。 如何在網格中沒有過濾器的情況下顯示屬性值(“ sellable_in_market”)? 不知道該怎么辦。 下面是我的代碼。
提前感謝。
protected function _prepareCollection()
{
parent::_prepareCollection();
$collection = $this->getCollection();
$store = $this->_getStore();
if ($store->getId()) {
$collection = $collection->joinAttribute(
'sellable_in_market',
'catalog_product/sellable_in_market',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection = $collection->joinAttribute('sellable_in_market', 'catalog_product/sellable_in_market', 'entity_id', null, 'left');
}
$this->setCollection($collection);
return $this;
}
protected function _prepareColumns()
{
$this->addColumnAfter('sellable_in_market',
array(
'header'=> Mage::helper('catalog')->__('Resellable'),
'width' => '60px',
'index' => 'sellable_in_market',
'sortable' => true,
'type' => 'options',
'options' => array("1" => 'Yes', "0" => 'No'),
),
'type'
);
parent::_prepareColumns();
}
在網格中“可轉售”列為空。 但是,如果我們選擇“是/否”進行過濾,則會顯示出來。 默認情況下如何在網格中顯示自定義值?
在$ this-> setCollection($ collection)行之前,在產品網格Mage_Adminhtml_Block_Catalog_Product_Grid的_prepareCollection函數中添加以下代碼以選擇屬性。
$attributeCode = 'qc_status';//here your attribute code
$collection->joinAttribute($attributeCode, 'catalog_product/'.$attributeCode, 'entity_id', null, 'left');
$collection->addAttributeToSelect($attributeCode);
然后下面的代碼在網格的_prepareColumns函數中。
$attributeCodeConfig ='qc_status';//Your attribute code...
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $attributeCodeConfig);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
$attributeOptions = $attribute->getSource()->getAllOptions();
$b = new Mage_Catalog_Model_Resource_Eav_Attribute();
$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
if(!empty($value['value'])) {
$attributeOptions2[$value['value']] = $value['label'];
}
}
if(count($attributeOptions2) > 0) {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
'type' => 'options',
'options' => $attributeOptions2,
));
} else {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.