2016-07-25 276 views
0

我正在使用Magento 1.9.2,並且我正在重寫產品網格表。Magento - Adminhtml試圖通過自定義列在產品網格中搜索

我已經從原來的Grid.php完成副本,並創造了這個:

/app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php這裏是它所包含的內容:

<?php 
class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid 
{ 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->setId('productGrid'); 
     $this->setDefaultSort('entity_id'); 
     $this->setDefaultDir('DESC'); 
     $this->setSaveParametersInSession(true); 
     $this->setUseAjax(true); 
     $this->setVarNameFilter('product_filter'); 

    } 

    protected function _getStore() 
    { 
     $storeId = (int) $this->getRequest()->getParam('store', 0); 
     return Mage::app()->getStore($storeId); 
    } 

    protected function _prepareCollection() 
    { 
     $store = $this->_getStore(); 
     $collection = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect('sku') 
      ->addAttributeToSelect('name') 
      ->addAttributeToSelect('attribute_set_id') 
      ->addAttributeToSelect('type_id'); 

     if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) { 
      $collection->joinField('qty', 
       'cataloginventory/stock_item', 
       'qty', 
       'product_id=entity_id', 
       '{{table}}.stock_id=1', 
       'left'); 
     } 
     if ($store->getId()) { 
      //$collection->setStoreId($store->getId()); 
      $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID; 
      $collection->addStoreFilter($store); 
      $collection->joinAttribute(
       'name', 
       'catalog_product/name', 
       'entity_id', 
       null, 
       'inner', 
       $adminStore 
      ); 
      $collection->joinAttribute(
       'custom_name', 
       'catalog_product/name', 
       'entity_id', 
       null, 
       'inner', 
       $store->getId() 
      ); 
      $collection->joinAttribute(
       'status', 
       'catalog_product/status', 
       'entity_id', 
       null, 
       'inner', 
       $store->getId() 
      ); 
      $collection->joinAttribute(
       'visibility', 
       'catalog_product/visibility', 
       'entity_id', 
       null, 
       'inner', 
       $store->getId() 
      ); 
      $collection->joinAttribute(
       'price', 
       'catalog_product/price', 
       'entity_id', 
       null, 
       'left', 
       $store->getId() 
      ); 
     } 
     else { 
      $collection->addAttributeToSelect('price'); 
      $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); 
      $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); 
     } 

     $this->setCollection($collection); 

     parent::_prepareCollection(); 
     $this->getCollection()->addWebsiteNamesToResult(); 
     return $this; 
    } 

    protected function _addColumnFilterToCollection($column) 
    { 
     if ($this->getCollection()) { 
      if ($column->getId() == 'websites') { 
       $this->getCollection()->joinField('websites', 
        'catalog/product_website', 
        'website_id', 
        'product_id=entity_id', 
        null, 
        'left'); 
      } 
     } 
     return parent::_addColumnFilterToCollection($column); 
    } 

    protected function _prepareColumns() 
    { 
     $this->addColumn('entity_id', 
      array(
       'header'=> Mage::helper('catalog')->__('ID'), 
       'width' => '50px', 
       'type' => 'number', 
       'index' => 'entity_id', 
     )); 
     $this->addColumn('name', 
      array(
       'header'=> Mage::helper('catalog')->__('Name'), 
       'index' => 'name', 
     )); 

     $store = $this->_getStore(); 
     if ($store->getId()) { 
      $this->addColumn('custom_name', 
       array(
        'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()), 
        'index' => 'custom_name', 
      )); 
     } 

     $this->addColumn('type', 
      array(
       'header'=> Mage::helper('catalog')->__('Type'), 
       'width' => '60px', 
       'index' => 'type_id', 
       'type' => 'options', 
       'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(), 
     )); 

     $sets = Mage::getResourceModel('eav/entity_attribute_set_collection') 
      ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId()) 
      ->load() 
      ->toOptionHash(); 

     $this->addColumn('set_name', 
      array(
       'header'=> Mage::helper('catalog')->__('Attrib. Set Name'), 
       'width' => '100px', 
       'index' => 'attribute_set_id', 
       'type' => 'options', 
       'options' => $sets, 
     )); 

     $this->addColumn('sku', 
      array(
       'header'=> Mage::helper('catalog')->__('SKU'), 
       'width' => '80px', 
       'index' => 'sku', 
     )); 

     $this->addColumn('number', 
      array(
       'header'=> Mage::helper('catalog')->__('Поръчка №'), 
       'width' => '50px', 
       'index' => 'entity_id', 
       'renderer' => 'Mage_Adminhtml_Block_Catalog_Product_Renderer', 
     )); 

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

     if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) { 
      $this->addColumn('qty', 
       array(
        'header'=> Mage::helper('catalog')->__('Qty'), 
        'width' => '100px', 
        'type' => 'number', 
        'index' => 'qty', 
      )); 
     } 

     $this->addColumn('visibility', 
      array(
       'header'=> Mage::helper('catalog')->__('Visibility'), 
       'width' => '70px', 
       'index' => 'visibility', 
       'type' => 'options', 
       'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(), 
     )); 

     $this->addColumn('status', 
      array(
       'header'=> Mage::helper('catalog')->__('Status'), 
       'width' => '70px', 
       'index' => 'status', 
       'type' => 'options', 
       'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(), 
     )); 

     if (!Mage::app()->isSingleStoreMode()) { 
      $this->addColumn('websites', 
       array(
        'header'=> Mage::helper('catalog')->__('Websites'), 
        'width' => '100px', 
        'sortable' => false, 
        'index'  => 'websites', 
        'type'  => 'options', 
        'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(), 
      )); 
     } 

     $this->addColumn('action', 
      array(
       'header' => Mage::helper('catalog')->__('Action'), 
       'width'  => '50px', 
       'type'  => 'action', 
       'getter'  => 'getId', 
       'actions' => array(
        array(
         'caption' => Mage::helper('catalog')->__('Edit'), 
         'url'  => array(
          'base'=>'*/*/edit', 
          'params'=>array('store'=>$this->getRequest()->getParam('store')) 
         ), 
         'field' => 'id' 
        ) 
       ), 
       'filter' => false, 
       'sortable' => false, 
       'index'  => 'stores', 
     )); 

     if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) { 
      $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS')); 
     } 

     return parent::_prepareColumns(); 
    } 

    protected function _prepareMassaction() 
    { 
     $this->setMassactionIdField('entity_id'); 
     $this->getMassactionBlock()->setFormFieldName('product'); 

     $this->getMassactionBlock()->addItem('delete', array(
      'label'=> Mage::helper('catalog')->__('Delete'), 
      'url' => $this->getUrl('*/*/massDelete'), 
      'confirm' => Mage::helper('catalog')->__('Are you sure?') 
     )); 

     $statuses = Mage::getSingleton('catalog/product_status')->getOptionArray(); 

     array_unshift($statuses, array('label'=>'', 'value'=>'')); 
     $this->getMassactionBlock()->addItem('status', array(
      'label'=> Mage::helper('catalog')->__('Change status'), 
      'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)), 
      'additional' => array(
        'visibility' => array(
         'name' => 'status', 
         'type' => 'select', 
         'class' => 'required-entry', 
         'label' => Mage::helper('catalog')->__('Status'), 
         'values' => $statuses 
        ) 
      ) 
     )); 

     if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){ 
      $this->getMassactionBlock()->addItem('attributes', array(
       'label' => Mage::helper('catalog')->__('Update Attributes'), 
       'url' => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true)) 
      )); 
     } 

     Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this)); 
     return $this; 
    } 

    public function getGridUrl() 
    { 
     return $this->getUrl('*/*/grid', array('_current'=>true)); 
    } 

    public function getRowUrl($row) 
    { 
     return $this->getUrl('*/*/edit', array(
      'store'=>$this->getRequest()->getParam('store'), 
      'id'=>$row->getId()) 
     ); 
    } 
} 

自定義代碼我在Grid.php已經添加是這樣的:

$this->addColumn('number', 
     array(
      'header'=> Mage::helper('catalog')->__('Поръчка №'), 
      'width' => '50px', 
      'index' => 'entity_id', 
      'renderer' => 'Mage_Adminhtml_Block_Catalog_Product_Renderer', 
    )); 

我已經創建和渲染也,我看到這個在另一個答案:

<?PHP 
class Mage_Adminhtml_Block_Catalog_Product_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract 
{ 
     public function render(Varien_Object $row) 
     { 

     $productId = $row->getData($this->getColumn()->getIndex()); 
      $orders = array(); 
      $collection = Mage::getResourceModel('sales/order_item_collection') 
       ->addAttributeToFilter('product_id', array('eq' => $productId)) 
       ->load(); 
      foreach($collection as $orderItem) { 
       $orders[$orderItem->getOrder()->getIncrementId()] = $orderItem->getOrder(); 
      } 

      $first_key = key($orders); 
      return $first_key; 


     } 
} 

我已經添加了附加列,這樣我就可以在購買產品的訂單ID的每一行中顯示。我沒有問題。一切都是正確的,但是當我嘗試使用此自定義列進行搜索時,問題就出現了。

訂單ID在列中正確顯示,但無法通過訂單ID進行搜索。

我的錯在哪裏,爲什麼它不工作,我該如何解決它?

在此先感謝!

回答

1

您無法在網格中搜索您的custom_column,因爲您剛剛使用了在運行時返回order_id的渲染器並準備好您的列。而已。 magento網格搜索是做什麼的,它用「搜索文本」過濾加載的集合。

例如: - 您搜索order_id = 10000901,您的搜索結果返回null,因爲order_id不在集合中。

所以你應該加入sales_flat_order & sales_flat_order_item與你的產品表,以獲得集合中的increment_id。然後,排序&搜索將完美。

+0

非常感謝你的答案添加以下代碼

$collection->getSelect()->joinLeft( array('order_item'=>'sales_flat_order_item'), 'e.entity_id = order_item.product_id', array('order_item.product_id','order_item.order_id') ); $collection->getSelect()->joinLeft( array('order'=>'sales_flat_order'), 'order_item.order_id = 'order'.entity_id', array('order.increment_id') ); $collection->getSelect()->group('e.entity_id'); 

。我知道我必須這樣做,但我不知道如何。你能幫我麼? –

+0

任何人都可以請幫忙。任何指南? –

+0

要添加哪些代碼?你可以請一個完美的答案。我已經搜索了這麼多放置,無法解決它。提前致謝! –

1

$this->setCollection($collection);前法_prepareCollection()/app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php

更新列代碼如下

$this->addColumn('increment_id', 
    array(
     'header'=> Mage::helper('catalog')->__('Order Id'), 
     'width' => '100px', 
     'type' => 'number', 
     'index' => 'increment_id', 
)); 
+0

感謝您的回答。現在搜索工作,但重定向到訂單頁面。爲什麼?是否有可能停留在產品網格上並在那裏顯示結果? –

+0

即使搜索不起作用。它只是重定向到銷售網格頁面。你能幫我解決嗎?謝謝。 –

+0

你可以讓這個列可以過濾嗎?它甚至有可能嗎? –

相關問題