2

I am trying to show some products based on a attribute value in an admin grid.

This admin grid should be accessible from the admin menu.

Should be in the following format:

                                                                   SAVE

show in Homepage   Product_id   Product_name  type    SKU    searchable     
- yes
- no
- any

Some points about the requirement are:

  • No edit/add button to this grid.
  • The product collection can be saved by SAVE button after checking/unchecking the column "show in Home page". (I think this requires database table)
  • The grid must be accessible from admin menu.

What files should I change to get my requirement? if not, atleast tell me which code to refer in Mage/core files.

3
  • 1
    With due respect, you're asking for custom development work which is outside the scope of SO site. Take a look at Mage_Adminhtml_Block_Catalog_Product_Grid to get started on how Magento builds admin grids.
    – Roscius
    May 2, 2014 at 14:24
  • @Roscius FYI, I am not sitting idle here. I am actually completed it 90% and I am going to post the answer soon. I am not waiting for other dev answers here.
    – Mr_Green
    May 2, 2014 at 15:18
  • Please have a look at magento.stackexchange.com/questions/7370/… may be helpful for you.
    – Anu
    May 3, 2014 at 5:04

3 Answers 3

8

Somebody had already posted the answer with a link to inchoo blog. I don't know why the user removed the answer later.

I followed the inchoo blog and made some changes as per my needs.

Make the following directory structure:

app/code/local
     --> {namespace}
         --> {module}
             --> Block
             --> controllers
             --> etc
             --> Helper

Just for example,

 {namespace} => Namespace
 {module}    => Module

Add the following codes as per the paths.

app/etc/modules/{namespace}_{module}

    <Namespace_Module>
        <active>true</active>
        <codePool>local</codePool>
    </Namespace_Module>

app/code/local/{namespace}/{module}/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.0.0.1</version>
        </Namespace_Module>
    </modules>
    <global>
        <blocks>
            <namespace_module>
                <class>Namespace_Module_Block</class>
            </namespace_module>
        </blocks>
        <helpers>
            <namespace_module>
                <class>Namespace_Module_Helper</class>
            </namespace_module>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <namespace_module before="Mage_Adminhtml">Namespace_Module_Adminhtml</namespace_module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

app/code/local/{namespace}/{module}/etc/adminhtml.xml

<?xml version="1.0"?>
<config>
    <menu>
        <sales> <!-- In menu, change as needed -->
            <children>
                <namespace_module translate="title" module="namespace_module">
                    <sort_order>10</sort_order> <!-- sort order in menu -->
                    <title>module title here</title>
                    <action>adminhtml/order/</action> <!-- controller action to call -->
                </namespace_module>
            </children>
        </sales>
    </menu>
</config>

app/code/local/{namespace}/{module}/Helper/Data.php

<?php
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app/code/local/{namespace}/{module}/Block/Adminhtml/Item/Grid.php

<?php
class Namespace_Module_Block_Adminhtml_Item_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('namespace_module_grid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
    protected function _prepareCollection()
    {
        $collection = null; //Your collection code here instead of null
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
    protected function _prepareColumns()
    {
        $helper = Mage::helper('namespace_module');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);

        // To add a column. (column will be added if the field is available)
        $this->addColumn('entity_id', array(
            'header' => $helper->__('Product id'),
            'index'  => 'entity_id',
            'type'   => 'number'
        ));

        return parent::_prepareColumns();
    }

    //Include this if checkboxes are needed for the listed items
    protected function _prepareMassaction(){
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('namespace_module');
        $this->getMassactionBlock()->addItem('status', array(
            'label'=> Mage::helper('namespace_module')->__('Save'),
            'url'  => $this->getUrl('*/*/massSave', array('_current'=>true))  // calling "massSaveAction" method in controller.
        ));
        return $this;
    }
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

app/code/local/{namespace}/{module}/Block/Adminhtml/Item.php

<?php
class Namespace_Module_Block_Adminhtml_Item extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'namespace_module';
        $this->_controller = 'adminhtml_sales_order';
        $this->_headerText = Mage::helper('namespace_module')->__('Module items');
        parent::__construct();
        $this->_removeButton('add');
    }
}

app/code/local/{namespace}/{module}/controllers/Adminhtml/ItemController.php

<?php
class Namespace_Module_Adminhtml_ItemController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('page title here'));
        $this->loadLayout();
        $this->_setActiveMenu('active menu name'); //looks like optional to me
        $this->_addContent($this->getLayout()->createBlock('namespace_module/adminhtml_item'));
        $this->renderLayout();
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('namespace_module/adminhtml_item_grid')->toHtml()
        );
    }
    public function massSaveAction(){
        $urlParam = $this->getRequest()->getParam('namespace_module');
        if(!is_array($urlParam)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('namespace_module')->__('Please select Items.'));
        } 
        else {
            try {
                // Add action code here
                // Like, sql queries to update database table

                $this->_getSession()->addSuccess($this->__('Total of %d Items were successfully updated.', count($urlParam)));
            }
            catch (Mage_Core_Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('namespace_module')->__('There was an error updating Items.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }
}
2
  • too bad you didn't find that sooner since you basically lost 100 points for nothing. +1's to boost ya back up!
    – SR_Magento
    May 5, 2014 at 14:53
  • @sr_magento thanks, would be helpful for other bounties :)
    – Mr_Green
    May 5, 2014 at 17:01
3

You can try Magicento But only if you know how to do it but you want to do it faster, if not you will never learn the basics.

1

For what you are asking, I would suggest taking a look at Marius's Ultimate Module Creator.

It will give you the most insight to achieve something very similar results to the example grid you've given, with the ability to associate products and categories. Multi-select status update disable/enable by default.

If you already have most of it built out, it may not hurt to mock it out with UMC, and look at how it is doing such for the "Change status" action on the grid it creates.

1
  • Thanks.. but I already confirmed from Marius that it can't be done using this module creator. I solved it. so, I will post the answer today/tomorrow.
    – Mr_Green
    May 4, 2014 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.