2016-05-17 61 views
0

我試圖實現一個類別屬性,旨在鏈接當前類別與其他類別的項目特定用法。Magento Adminhtml:類別屬性與類別選擇器

爲此,我創建了一個varchar屬性,用於存儲逗號分隔的類別ID列表,但我希望在該字段旁邊顯示類別選擇器的小選取器圖標,如條件中的選擇器晉升管理員屏幕的部分。

Category chooser from Promotion section

我真的不看到實物渲染我應該實現以實現的,我希望你們能給我一個提示。 感謝您的幫助。

+0

我試着爲我的屬性設置一個自定義的input_renderer。在這個渲染器中,我使用getAfterElementHtml()方法來注入HTML以顯示選擇器觸發按鈕鏈接,但是就我所知:'( – SMASHED

+0

Up! 任何人都設法使用Mage_Adminhtml_Block_Catalog_Category_Checkboxes_Tree塊? – SMASHED

回答

0

我做到了 - 終於 - 所以這裏有雲:

FYI:我的類別屬性被命名爲「category_top_searches」,是類別ID昏迷分隔列表。選擇器旨在幫助提供此列表。

1 - 使用觀察者添加選項卡

A。觀察員聲明

<adminhtml_catalog_category_tabs> 
    <observers> 
     <add_topsearches_tab> 
      <class>mymodule/observer</class> 
      <method>addTopSearchesTab</method> 
     </add_topsearches_tab> 
    </observers> 
</adminhtml_catalog_category_tabs> 

B。觀察實施

public function addTopSearchesTab(Varien_Event_Observer $observer) 
{ 
    $tabs = $observer->getTabs(); 
    $tabs->addTab(
     'top_searches_tab', 'mymodule/catalog_category_edit_tab_topsearches' 
    ); 
} 

2 - 定義塊的標籤

需要實現選項卡界面的方法和方法來獲取檢驗類初始化樹

class MyNamespace_Adminhtml_Block_Catalog_Category_Edit_Tab_Topsearches 
    extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories 
    implements Mage_Adminhtml_Block_Widget_Tab_Interface 
{ 

    /** 
    * Specify template to use 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->setTemplate('catalog/category/edit/categorypicker.phtml'); 
    } 

    /** 
    * Checks when this block is readonly 
    * 
    * @return bool 
    */ 
    public function isReadonly() 
    { 
     return false; 
    } 

    /** 
    * getCategoryIds method 

    * @return array 
    */ 
    protected function getCategoryIds() 
    { 
     $category = Mage::registry('current_category'); 
     $ids = explode(',', $category->getCategoryTopSearches()); 
     return $ids; 
    } 


    /** 
    * getIdsString 
    * 
    * @return string 
    */ 
    public function getIdsString() 
    { 
     $category = Mage::registry('current_category'); 
     return $category->getCategoryTopSearches(); 
    } 

    /** 
    * Return Tab label 
    * 
    * @return string 
    */ 
    public function getTabLabel() 
    { 
     return $this->__('Top Searches'); 
    } 

    /** 
    * Return Tab title 
    * 
    * @return string 
    */ 
    public function getTabTitle() 
    { 
     return $this->__('Top Searches'); 
    } 

    /** 
    * Can show tab in tabs 
    * 
    * @return boolean 
    */ 
    public function canShowTab() 
    { 
     return true; 
    } 

    /** 
    * Tab is hidden 
    * 
    * @return boolean 
    */ 
    public function isHidden() 
    { 
     return false; 
    } 
} 

3時 - 定義樹模板

我複製粘貼產品編輯頁面的類別樹的核心模板app/design/adminhtml/default/default/template/cata登錄/產品/編輯/ categories.phtml到應用程序/設計/ adminhtml /默認/ mytheme /模板/目錄/類別/編輯/ categorypicker.phtml並作出一些細微的變化:

添加一個文本字段來顯示我的ID:

<div class="entry-edit"> 
    <div class="entry-edit-head"> 
     <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('catalog')->__('Linked Categories') ?></h4> 
    </div> 
    <fieldset id="linked_cat"> 
     <input type="text" name="linked_categories_list" id="linked_categories_list" value="<?php echo $this->getIdsString() ?>"> 
    </fieldset> 
</div> 

並修改了樹頭更足與我的上下文

<div class="entry-edit"> 
    <div class="entry-edit-head"> 
     <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('catalog')->__('Category picker') ?></h4> 
    </div> 
    <fieldset id="grop_fields"> 
     <input type="hidden" name="linked_categories" id="linked_categories" value="<?php echo $this->getIdsString() ?>"> 
     <div id="product-categories" class="tree"></div> 
    </fieldset> 
</div> 

我只好稍微修改beforeLoad功能設置id PARAM:

categoryLoader.on("beforeload", function(treeLoader, node) { 
    treeLoader.baseParams.category = node.attributes.id; 
    treeLoader.baseParams.id = node.attributes.id; 
}); 

我增加了一個功能來實現array_unique功能

Array.prototype.unique = function(){ 
    'use strict'; 
    var im = {}, uniq = []; 
    for (var i=0;i<this.length;i++){ 
     var type = (this[i]).constructor.name, 
     //   ^note: for IE use this[i].constructor! 
      val = type + (!/num|str|regex|bool/i.test(type) 
        ? JSON.stringify(this[i]) 
        : this[i]); 
     if (!(val in im)){uniq.push(this[i]);} 
     im[val] = 1; 
    } 
    return uniq; 
} 

然後修改categoryAdd和categoryRemove功能與新選中/取消ID

function categoryAdd(id) { 
    var ids = $('linked_categories').value.split(','); 
    ids.push(id); 
    var idList = ids.unique().join(',').replace(/^,/, ''); 
    document.getElementById("linked_categories_list").value = idList; 
    $('linked_categories').value = ids.join(','); 
} 
function categoryRemove(id) { 
    var ids = $('linked_categories').value.split(','); 
    // bug #7654 fixed 
    while (-1 != ids.indexOf(id)) { 
     ids.splice(ids.indexOf(id), 1); 
    } 
    var idList = ids.unique().join(',').replace(/^,/, ''); 
    document.getElementById("linked_categories_list").value = idList; 
    $('linked_categories').value = ids.join(','); 
} 

4更新我的文本字段 - 實施類別保存事件的觀察者將新值注入到真實屬性中

A.宣言

<catalog_category_save_before> 
    <observers> 
     <set_top_searches> 
      <class>mymodule/observer</class> 
      <method>setTopSearches</method> 
     </set_top_searches> 
    </observers> 
</catalog_category_save_before> 

B.執行

/** 
* takes the fake attribute value and insert it into the real category_top_searches attribute 
* 
* @param Varien_Event_Observer $observer Observer 
* 
* @return void 
*/ 
public function setTopSearches(Varien_Event_Observer $observer) 
{ 
    /** @var $category Mage_Catalog_Model_Category */ 
    $category = $observer->getEvent()->getCategory(); 

    $params = Mage::app()->getRequest()->getParams(); 
    $ids = $params['linked_categories_list']; 
    $category->setData('category_top_searches', $ids); 
} 

5 - 最後,我通過在安裝腳本更新它躲在我的原始屬性: (我改變了它的組否則其專用的標籤將是空的,但仍然顯示)

$installer->updateAttribute("catalog_category", "category_top_searches", 'group', "General"); 
$installer->updateAttribute("catalog_category", "category_top_searches", 'is_visible', false); 

說不上來,如果有人會用它,因爲我敢獨自在這個線程,但我曾愛過找到試圖ACH當這樣一個職位即使這個功能......:D

如果有人有興趣,我也在配置字段中實現了這個類別選擇器作爲一個新的frontend_type,這非常棒!