【问题标题】:Add another tab with Product Grid to Category edit page in Mangento在 Magento 的类别编辑页面中添加另一个带有 Product Grid 的选项卡
【发布时间】:2015-04-10 08:48:46
【问题描述】:

我正在尝试将另一个选项卡添加到包含另一个产品网格的类别编辑页面。我需要这样做,因为我想向每个类别页面添加一个区域,该区域包含在此网格中选择的产品。

谁能帮我完成这个模块?还是有更简单或现有的模块可以做到这一点?

PS:非常感谢完整的代码:)

这是我到目前为止的地方: etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Forideas_Promotion>
      <version>0.1.0</version>
    </Forideas_Promotion>
  </modules>
  <global>
    <resources>
        <forideas_promotion_setup>
            <setup>
                <module>Forideas_Promotion</module>
                <class>Forideas_Promotion_Model_Resource_Setup</class>
            </setup>
        </forideas_promotion_setup>
    </resources>
    <helpers>
      <promotion>
        <class>Forideas_Promotion_Helper</class>
      </promotion>
    </helpers>
    <models>
        <forideas_promotion>
            <class>Forideas_Promotion_Model</class>
        </forideas_promotion>
        <promotion_resource>
            <entities>
              <category_product>
                  <table>category_product</table>
              </category_product>
            </entities>
        </promotion_resource>
    </models>
    <events>
        <adminhtml_catalog_category_tabs>
            <observers>
                <forideas_promotion_observer>
                    <class>forideas_promotion/observer</class>
                    <method>addCategoryTab</method>
                </forideas_promotion_observer>
            </observers>
        </adminhtml_catalog_category_tabs>
    </events>
  </global>
  <adminhtml>
    <layout>
        <updates>
            <forideas_category_promotion>
                <file>forideas_promotion.xml</file>
            </forideas_category_promotion>
        </updates>
    </layout>
  </adminhtml>
  <admin>
      <routers>
          <adminhtml>
              <args>
                  <modules>
                      <Forideas_Promotion before="Mage_Adminhtml">Forideas_Promotion_Adminhtml</Forideas_Promotion>
                  </modules>
              </args>
          </adminhtml>
      </routers>
  </admin>
</config> 

块/Adminhtml/Category/Edit/Tab/Product.php:

<?php
class Forideas_Promotion_Block_Adminhtml_Category_Edit_Tab_Product
    extends Mage_Adminhtml_Block_Widget_Grid {
    public function __construct(){
        parent::__construct();
        $this->setId('product_grid');
        $this->setDefaultSort('position');
        $this->setDefaultDir('ASC');
        $this->setUseAjax(true);
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_products'=>1));
        }
    }
    protected function _prepareCollection() {
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->addAttributeToSelect('price');
        $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
        $collection->joinAttribute('product_name', 'catalog_product/name', 'entity_id', null, 'left', $adminStore);
        if ($this->getCategory()->getId()){
            $constraint = '{{table}}.category_id='.$this->getCategory()->getId();
        }
        else{
            $constraint = '{{table}}.category_id=0';
        }
        $collection->joinField('position',
            'promotion/category_product',
            'position',
            'product_id=entity_id',
            $constraint,
            'left');
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
     protected function _prepareMassaction(){
        return $this;
    }
    protected function _prepareColumns(){
        $this->addColumn('in_products', array(
            'header_css_class'  => 'a-center',
            'type'  => 'checkbox',
            'name'  => 'in_products',
            'values'=> $this->_getSelectedProducts(),
            'align' => 'center',
            'index' => 'entity_id'
        ));
        $this->addColumn('product_name', array(
            'header'=> Mage::helper('catalog')->__('Name'),
            'align' => 'left',
            'index' => 'product_name',
        ));
        $this->addColumn('sku', array(
            'header'=> Mage::helper('catalog')->__('SKU'),
            'align' => 'left',
            'index' => 'sku',
        ));
        $this->addColumn('price', array(
            'header'=> Mage::helper('catalog')->__('Price'),
            'type'  => 'currency',
            'width' => '1',
            'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
            'index' => 'price'
        ));
        $this->addColumn('position', array(
            'header'=> Mage::helper('catalog')->__('Position'),
            'name'  => 'position',
            'width' => 60,
            'type'  => 'number',
            'validate_class'=> 'validate-number',
            'index' => 'position',
            'editable'  => true,
        ));
    }
    protected function _getSelectedProducts(){
        $products = $this->getCategoryProducts();
        if (!is_array($products)) {
            $products = array_keys($this->getSelectedProducts());
        }
        return $products;
    }
    public function getSelectedProducts() {
        $products = array();
        $selected = Mage::registry('current_category')->getSelectedProducts();
        if (!is_array($selected)){
            $selected = array();
        }
        foreach ($selected as $product) {
            $products[$product->getId()] = array('position' => $product->getPosition());
        }
        return $products;
    }
    public function getRowUrl($item){
        return '#';
    }
    public function getGridUrl(){
        return $this->getUrl('*/*/productsGrid', array(
            'id'=>$this->getCategory()->getId()
        ));
    }
    public function getCategory(){
        return Mage::registry('current_category');
    }
    protected function _addColumnFilterToCollection($column){
        // Set custom filter for in product flag
        if ($column->getId() == 'in_products') {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
            }
            else {
                if($productIds) {
                    $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
                }
            }
        }
        else {
            parent::_addColumnFilterToCollection($column);
        }
        return $this;
    }
}

controllers/Adminhtml/Promotion/Category/ProductController.php:

<?php

require_once ("Mage/Adminhtml/controllers/Catalog/ProductController.php");
class Forideas_Promotion_Adminhtml_Promotion_Category_ProductController extends Mage_Adminhtml_Catalog_ProductController
{

    public function productsAction(){
        //$this->_initEntity(); //if you don't have such a method then replace it with something that will get you the entity you are editing.
        $this->loadLayout();
        $this->getLayout()->getBlock('category.edit.tab.product')
            ->setCategoryProducts($this->getRequest()->getPost('category_products', null));
        $this->renderLayout();
    }
    public function productsgridAction(){
        //$this->_initCategory();
        $this->loadLayout();
        $this->getLayout()->getBlock('category.edit.tab.product')
            ->setCategoryProducts($this->getRequest()->getPost('category_products', null));
        $this->renderLayout();
    }

}

模型/观察者.php

<?php

class Forideas_Promotion_Model_Observer{


    public function addCategoryTab($observer)
    {
        $block = $observer->getEvent()->getTabs();

            $block->addTab('features', array(
                'label' => Mage::helper('catalog')->__('Some Label here'),

                'url'   => Mage::helper('adminhtml')->getUrl('adminhtml/promotion_category_product/productsgrid', array('_current' => true)),
                'class' => 'ajax', 
            ));


    }

}

sql/setup/install-0.1.0.php

$this->startSetup();

$table = $this->getConnection()
    ->newTable($this->getTable('promotion/category_product'))
    ->addColumn('rel_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'unsigned'  => true,
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
        ), 'Relation ID')
    ->addColumn('category_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
    ), 'Category ID')
    ->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
    ), 'Product ID')
    ->addColumn('position', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'nullable'  => false,
        'default'   => '0',
    ), 'Position')
    ->addIndex($this->getIdxName('promotion/category_product', array('product_id')), array('product_id'))
    ->addForeignKey($this->getFkName('promotion/category_product', 'category_id', 'promotion/category', 'entity_id'), 'category_id', $this->getTable('promotion/category'), 'entity_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->addForeignKey($this->getFkName('promotion/category_product', 'product_id', 'catalog/product', 'entity_id'),    'product_id', $this->getTable('catalog/product'), 'entity_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
    ->setComment('Category to Product Linkage Table');
$this->getConnection()->createTable($table);

$this->endSetup();

【问题讨论】:

    标签: magento admin categories adminhtml


    【解决方案1】:

    我找到了您的“ajax 加载程序不会消失”问题的答案。这是因为从 AJAX 调用返回的 HTML 实际上是指原始表 catalog_category_products_table。但是 Javascript 正在寻找您自己的表名但找不到它,因此无限请等待加载器。

    解决方案是创建您自己的扩展 Mage_Adminhtml_Catalog_CategoryController 的控制器,并在那里使用您的网格名称加载您自己的块。然后,在您扩展 Mage_Adminhtml_Block_Catalog_Category_Tab_Product 的块中,您将定义 getGridUrl() 并将路径返回到新创建的控制器。现在一切都会好起来的。

    【讨论】:

      【解决方案2】:

      不知道您上传所有这些代码的目的是什么。据我所知,它没有提到你的问题。如果要添加新标签,请执行以下操作:

      config.xml

      <adminhtml>
          <events>
              <adminhtml_catalog_category_tabs>
                  <observers>
                      <add_new_tab>
                          <type>singleton</type>
                          <class>company_module/observer</class>
                          <method>addNewCategoryTab</method>
                      </add_new_tab>
                  </observers>
              </adminhtml_catalog_category_tabs>
          </events>
      </adminhtml>
      

      模型/观察者.php

      class Company_Module_Model_Observer
      {
          public function addNewCategoryTab($observer)
          {
              $tabs = $observer->getTabs();
              $tabs->addTab('prod', array(
                  'label'     => Mage::helper('catalog')->__('Other Products'),
                  'content'   => $tabs->getLayout()->createBlock(
                      'adminhtml/catalog_category_tab_product',
                      'category.product.grid'
                  )->toHtml(),
              ));
          }
      

      比使用从adminhtml/catalog_category_tab_product 扩展的块与你重写的_prepareCollection 方法。 (别忘了把你的块放在 createBlock 方法中)

      【讨论】:

      • 好的,我扩展了块并按照您的建议使用它,它的工作原理,但是我在哪里/如何访问/保存在此网格中选择的产品?
      • 而且我在更改页面和应用过滤器方面还有一个问题,这意味着当我使用上述其中一种产品时,产品正在发生变化,但 ajax 加载器不会消失。
      • 问题是关于如何创建附加选项卡。我向你展示了如何做到这一点。如果您想编辑选项卡的内容 - 查看示例 adminhtml/catalog_category_tab_product,使用自己的逻辑创建自己的块。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多