2013-12-10 32 views
0

我想在Magento的管理區域的「目錄產品」網格中添加一個新列。Magento - 包含自定義輸出/邏輯的AdminHtml產品網格中的自定義列?

新列將被稱爲目前優惠?銷售項目?,當產品的price大於special_price(即產品在銷售中)時,將顯示「是」。

到目前爲止,我已經能夠在special_price列添加到網格,通過下面的代碼:

$collection->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left', $store->getId());

$this->addColumn('special_price', 
     array(
      'header'=> Mage::helper('catalog')->__('Special Price'), 
      'type' => 'price', 
      'currency_code' => $store->getBaseCurrency()->getCode(), 
      'index' => 'special_price', 
    )); 

但我真正需要的是實際執行某種另一列的邏輯來比較special_price,如果打折則打印「是」,否則打印「否」。

我已經添加了列,但就目前而言,很明顯,這是因爲它的index不對應任何數據真正來源是空的:

$this->addColumn('is_sale_item', 
    array(
     'header'=> Mage::helper('catalog')->__('Sale Item?'), 
     'type' => 'options', 
     'index' => 'is_sale_item', // <--- HOW DO I POPULATE THIS INDEX? 
     'options' => array(0 => 'No', 1 => 'Yes') 
)); 

我將如何實現這一目標?我會在哪裏放置「is_sale_item」計算的邏輯並使其成爲「真實」索引?

回答

2

我設法實現這一點,但以一種稍微黑客的方式。如果有人知道更好的方式,請評論。

1.自定義列添加到列列表,使用自定義渲染器和過濾器回調

$this->addColumn('is_sale_item', 
      array(
       'header'=> Mage::helper('catalog')->__('Sale Item?'), 
       'type' => 'options', 
       'index' => 'is_sale_item', 
       'options' => array("no" => 'No', "yes" => 'Yes'), 
       'renderer' => 'Mage_Adminhtml_Catalog_Product_Renderer_Sale', 
       'filter_condition_callback' => array($this, '_filterIsSaleItem'), 
     )); 

2.新的自定義屬性

我用創建自定義渲染器自定義渲染器來執行邏輯來測試項目是否已被縮小。再一次,可能不是這樣做的正確方法。在與Grid.php文件夾,創建Renderer/Sale.php

<?php 
class Mage_Adminhtml_Catalog_Product_Renderer_Sale extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract 
{ 
    public function render(Varien_Object $row) 
    { 
     $_price  = $row->getData("price"); 
     $_specialPrice = $row->getData("special_price"); 
     if ($_specialPrice && $_price > $_specialPrice) { 
      return "Yes"; 
     } else { 
      return "No"; 
     } 
    } 
} 
?> 

3.添加過濾器回調將應用邏輯,當過濾器使用

回到Grid.php

protected function _filterIsSaleItem($collection, $column) 
    { 
     $value = $column->getFilter()->getValue(); 

     if (!$value) { 
      return $this; 
     } 

     $store = $this->_getStore(); 
     $this->getCollection()->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId()); 
     $this->getCollection()->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left', $store->getId()); 

     if ($value == "yes") { 
      $this->getCollection()->getSelect()->where("_table_price.value > _table_special_price.value"); 
     } else { 
      $this->getCollection()->getSelect()->where("_table_price.value <= _table_special_price.value OR _table_special_price.value IS NULL"); 
     } 

     return $this; 

    } 
2

這應該有幫助:

$this->addColumn('special_price', array(
    'header' => Mage::helper('catalog')->__('Special Price'), 
    'type'  => 'options', 
    'index'  => 'special_price', 
    'options' => Mage::getSingleton('eav/entity_attribute_source_boolean')->getOptionArray(), 
)); 

@WackGet我希望你找到這個有用的:)。

相關問題