Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

Thursday 3 May 2018

How to get the final sql / mysql query from the grid includes filters and pagination in Magento


Open Following file
Open MagentoRoot/lib/Varien/Db/Adapter/Pdo/Mysql.php
Update false to true
protected $_debug = true
protected $_logAllQueries  = true

Saturday 10 March 2018

How to display product associated categories & filter product grid by categories in admin panel



Step 1.

Copy file from "app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php" to "app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php"
Step 2 : Add Column for category.

protected function _prepareColumns() {
    ......
    ......
    ......
    $this->addColumn(
        'category_id',
            array(
             'header'                    => Mage::helper('jaydip_kansagra')->__('Category'),
             'width'                     => '100px',
             'index'                     => 'category_ids',
             'type'                      => 'options',
             'options'                   => $this->getCategoriesOptions(),
             'filter_condition_callback' => array($this, '_callbackCategoryFilter'),
             'renderer'                  => 'Jaydip_Kansagra_Block_Adminhtml_Renderer_Categories'
           )
    );
    ......
    ......
    ......
Step 3 : Create new functions in this grid file.

protected function getCategoriesOptions() {
   $categoriesOptions = array();
   $this->prepareCategoriesOptions($this->_getCategories()->getNodes(), $categoriesOptions);
   return $categoriesOptions;
}

protected function _getCategories() {
   $storeId = (int) $this->getRequest()->getParam('store');
   if ($storeId) {
       $store = Mage::app()->getStore($storeId);
       $parent = $store->getRootCategoryId();
   } else {
       $parent = Mage_Catalog_Model_Category::TREE_ROOT_ID;
   }
   $tree = Mage::getResourceModel('catalog/category_tree');
   /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */
   $nodes = $tree->loadNode($parent)
        ->loadChildren(0)
        ->getChildren();
   $tree->addCollectionData(null, true, $parent, true, false);
   return $nodes;
}

protected function prepareCategoriesOptions($nodes, &$options) {
    /** @var $node Varien_Data_Tree_Node */
    foreach ($nodes as $node) {
        if ($node->getName()) {
            $options[$node->getId()] = str_repeat('    ', $node->getLevel() - 1) . $node->getName();
        }
        if ($node->hasChildren()) {
            $this->prepareCategoriesOptions($node->getAllChildNodes(), $options);
        }
    }
    return $options;
}

*********Or For without category tree view**********

protected function getCategoriesOptions() {
   $categoriesOptions = array();
   $this->prepareCategoriesOptions($this->_getCategories()->getNodes(), $categoriesOptions);
   return $categoriesOptions;
}
Step 4 : Create new function for filter in this file protected method _callbackCategoryFilter

protected function _callbackCategoryFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return null;
    }
    $collection->joinField(
        'category_id',
        'catalog/category_product',
        'category_id',
        'product_id = entity_id',
        '{{table}}.category_id=' . $column->getFilter()->getValue(),
        'inner'
    );
Step 5 : Create renderer file in any extension

class Jaydip_Kansagra_Block_Adminhtml_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
    public function render(Varien_Object $row) {
     
        $productCategories = array();
        $product = Mage::getModel('catalog/product')->load($row->getData('entity_id'));
        $categories = $product->getCategoryCollection()->addAttributeToSelect('name');
        foreach ($categories as $category) {
            array_push($productCategories, $category->getName());
        }
        return implode(',', $productCategories);
    } 

Friday 9 February 2018

How to add / create grid Tab in custom module in magento admin panel

Step1 :
open : /app/code/local/Namespace/Modulename/Block/Adminhtml/Employee/Edit/Tabs.php
Add following lines

protected function _beforeToHtml()
{
.........
.........
.........
$this->addTab(
    'form_advancepayment',
    array(
'label'   => Mage::helper('namespace_modulename')->__('Advance Payment'),
'title'   => Mage::helper('namespace_modulename')->__('Advance Payment'),
'content' => $this->getLayout()->createBlock(
    'namespace_modulename/adminhtml_employee_edit_tab_advancepayment'
)
->toHtml(),
    )
);
return parent::_beforeToHtml();
}
Step2 :
Create new file (Advancepayment.php) : /app/code/local/Namespace/Modulename/Block/Adminhtml/Employee/Edit/Tab/Advancepayment.php
<?php
class Namespace_Modulename_Block_Adminhtml_Employee_Edit_Tab_Advancepayment extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('advancepymt_grid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
        //$this->setFilterVisibility(false);
    }
   
    protected function _prepareCollection()
    {
        $id = $this->getRequest()->getParam('id');
        $collection = Mage::getModel('namespace_modulename/advancepymt')
            ->getCollection()
            ->addFieldToFilter('employee_id', $id);
       
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
   
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            array(
                'header' => Mage::helper('namespace_modulename')->__('Id'),
                'index'  => 'entity_id',
                'filter' => false,
                'type'   => 'number'
            )
        );
        $this->addColumn(
            'payment',
            array(
                'header'    => Mage::helper('namespace_modulename')->__('Payment'),
                'align'     => 'left',
                'type'   => 'number',
                'index'     => 'payment',
            )
        );
       
        $this->addColumn(
            'created_at',
            array(
                'header' => Mage::helper('namespace_modulename')->__('Created at'),
                'index'  => 'created_at',
                'width'  => '120px',
                'type'   => 'datetime',
            )
        );
        return parent::_prepareColumns();
    }
   
    public function getGridUrl()
    {
        return $this->getUrl('*/*/advancepymt', array('_current'=>true));
    }
   
    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
Step3 :
Create function in current controller : /app/code/local/Namespace/Modulename/controllers/Adminhtml/Payroll/EmployeeController.php
class Namespace_Modulename_Adminhtml_Payroll_EmployeeController extends Namespace_Modulename_Controller_Adminhtml_Payroll
{
.............
.............
.............
public function advancepymtAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock('edit.tab.advancepayment');
        $this->renderLayout(); 
    }
Step4 :
Add view in admin layout xml file : /app/design/adminhtml/default/default/layout/namespace_modulename.xml
Add following code into file

...........
...........
<adminhtml_modulename_employee_advancepymt>
<block type="core/text_list" name="root" output="toHtml">
    <block type="namespace_modulename/adminhtml_employee_edit_tab_advancepayment" name="edit.tab.advancepayment"/>
</block>
</adminhtml_modulename_employee_advancepymt>
...........
........... 

Thursday 31 March 2016

How to create import functionality on grid page in magneto


Open Grid.php
protected function _prepareMassaction()
    {
..................
..................
//Upload your csv file into var/import directory
$dir = new DirectoryIterator(Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS);
$extensions = "csv";
$csv = array();
foreach ($dir as $fileinfo) {
    if ($fileinfo->isFile() && stristr($extensions, $fileinfo->getExtension())) {
        $csv[$fileinfo->getFilename()] = $fileinfo->getFilename();
    }            
}
$csv = array_merge(array(''=>'Please select'),$csv);
$this->getMassactionBlock()->addItem('import', array(
    'label'        => Mage::helper('metizsoft_taxgenerate')->__('Import'),
    'url'          => $this->getUrl('*/*/massCsv', array('_current'=>true)),
    'confirm'  => Mage::helper('metizsoft_taxgenerate')->__('Are you sure?'),
    'additional'   => array(
        'subgroup'    => array(
            'name'     => 'csv',
            'type'     => 'select',
            'class'    => 'required-entry',
            'label'    => Mage::helper('customer')->__('Select Csv'),
            'values'   => $csv
        )
    )
));
return $this;
}

Open your controller file. and put this code.public function massCsvAction()
{
$csvfile = $this->getRequest()->getParam('csv');
if (!$csvfile) {
  Mage::getSingleton('adminhtml/session')->addError(
      Mage::helper('metizsoft_taxgenerate')->__('Please select statetaxs.')
  );
} else {
  try {
     $csv = new Varien_File_Csv;
     $csvpath = Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS . $csvfile;
             
     $datas = $csv->getData($csvpath);
     print_r($datas);
   } catch (Mage_Core_Exception $e) {
     Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
   } catch (Exception $e) {
     Mage::getSingleton('adminhtml/session')->addError(
        Mage::helper('metizsoft_taxgenerate')->__('There was an error updating statetaxs.')
     );
     Mage::logException($e);
   }
}
$this->_redirect('*/*/index');
}