0

How can I export and display products which are going to expire in the Magento admin panel reports tab?

Below is a screen shot with my requirement.

Desired menu item

0

I have created a module for you. Try this

app/etc/modules/Dinesh_Expiredproducts.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Dinesh_Expiredproducts>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Dinesh_Expiredproducts>
  </modules>
</config>

app/code/local/Dinesh/Expiredproducts/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Dinesh_Expiredproducts>
      <version>0.1.0</version>
    </Dinesh_Expiredproducts>
  </modules>
  <global>
    <helpers>
      <expiredproducts>
        <class>Dinesh_Expiredproducts_Helper</class>
      </expiredproducts>
    </helpers>
    <blocks>
      <expiredproducts>
        <class>Dinesh_Expiredproducts_Block</class>
      </expiredproducts>
    </blocks>
  </global>
  <admin>
    <routers>
      <expiredproducts>
        <use>admin</use>
        <args>
          <module>Dinesh_Expiredproducts</module>
          <frontName>admin_expiredproducts</frontName>
        </args>
      </expiredproducts>
    </routers>
  </admin>
  <adminhtml>
    <menu>
      <report module="reports">
        <children>
          <products module="reports">
            <children>
              <expiredproducts module="reports">
                <title>Expiredproducts</title>
                <sort_order>10</sort_order>
                <action>admin_expiredproducts/adminhtml_expiredproducts</action>
              </expiredproducts>
            </children>
          </products>
        </children>
      </report>
    </menu>
    <acl>
      <resources>
        <all>
          <title>Allow Everything</title>
        </all>
        <admin>
          <children>
            <expiredproducts translate="title" module="expiredproducts">
              <title>Expiredproducts</title>
              <sort_order>1000</sort_order>
              <children>
          <expiredproducts translate="title">
            <title>Manage Expiredproducts</title>
            <sort_order>0</sort_order>
          </expiredproducts>
              </children>
            </expiredproducts>
          </children>
        </admin>
      </resources>
    </acl>
    <layout>
      <updates>
        <expiredproducts>
          <file>expiredproducts.xml</file>
        </expiredproducts>
      </updates>
    </layout>
  </adminhtml>
</config>

app/code/local/Dinesh/Expiredproducts/Block/Adminhtml/Expiredproducts.php

<?php


class Dinesh_Expiredproducts_Block_Adminhtml_Expiredproducts extends Mage_Adminhtml_Block_Widget_Grid_Container{

    public function __construct()
    {

    $this->_controller = "adminhtml_expiredproducts";
    $this->_blockGroup = "expiredproducts";
    $this->_headerText = Mage::helper("expiredproducts")->__("Expiredproducts Manager");
    $this->_addButtonLabel = Mage::helper("expiredproducts")->__("Add New Item");
    parent::__construct();
    $this->_removeButton('add');
    }

}

app/code/local/Dinesh/Expiredproducts/Block/Adminhtml/Expiredproducts/Grid.php

<?php

class Dinesh_Expiredproducts_Block_Adminhtml_Expiredproducts_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

        public function __construct()
        {
                parent::__construct();
                $this->setId("expiredproductsGrid");
                $this->setDefaultSort("entity_id");
                $this->setDefaultDir("DESC");
                $this->setSaveParametersInSession(true);
        }

        protected function _prepareCollection()
        {
               $currentDate = Mage::getModel('core/date')->gmtDate('Y-m-d');
               $plusOneMonth = date('Y-m-d', strtotime($currentDate . "+1 months"));

               $collection = Mage::getModel('catalog/product')->getCollection();
               $collection->addAttributeToSelect("name")->addFieldToFilter('expiry_date', array('from' => $currentDate, 'to' => $plusOneMonth));

                $this->setCollection($collection);
                return parent::_prepareCollection();
        }
        protected function _prepareColumns()
        {
                $this->addColumn("entity_id", array(
                "header" => Mage::helper("expiredproducts")->__("ID"),
                "align" =>"right",
                "width" => "50px",
                "type" => "number",
                "index" => "entity_id",
                ));

                $this->addColumn("name", array(
                "header" => Mage::helper("expiredproducts")->__("Name"),
                "index" => "name",
                ));
                $this->addColumn("sku", array(
                "header" => Mage::helper("expiredproducts")->__("SKU"),
                "index" => "sku",
                ));
            $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV')); 
            $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel'));

                return parent::_prepareColumns();
        }

        public function getRowUrl($row)
        {
               return '#';
        }



        protected function _prepareMassaction()
        {
            $this->setMassactionIdField('entity_id');
            $this->getMassactionBlock()->setFormFieldName('entity_ids');
            $this->getMassactionBlock()->setUseSelectAll(true);
            $this->getMassactionBlock()->addItem('remove_expiredproducts', array(
                     'label'=> Mage::helper('expiredproducts')->__('Remove Expiredproducts'),
                     'url'  => $this->getUrl('*/adminhtml_expiredproducts/massRemove'),
                     'confirm' => Mage::helper('expiredproducts')->__('Are you sure?')
                ));
            return $this;
        }


}

app/code/local/Dinesh/Expiredproducts/Adminhtml/ExpiredproductsController.php

<?php

class Dinesh_Expiredproducts_Adminhtml_ExpiredproductsController extends Mage_Adminhtml_Controller_Action
{
        protected function _isAllowed()
        {
        //return Mage::getSingleton('admin/session')->isAllowed('expiredproducts/expiredproducts');
            return true;
        }

        protected function _initAction()
        {
                $this->loadLayout()->_setActiveMenu("expiredproducts/expiredproducts")->_addBreadcrumb(Mage::helper("adminhtml")->__("Expiredproducts  Manager"),Mage::helper("adminhtml")->__("Expiredproducts Manager"));
                return $this;
        }
        public function indexAction() 
        {
                $this->_title($this->__("Expiredproducts"));
                $this->_title($this->__("Manager Expiredproducts"));

                $this->_initAction();
                $this->renderLayout();
        }

        /**
         * Export order grid to CSV format
         */
        public function exportCsvAction()
        {
            $fileName   = 'expiredproducts.csv';
            $grid       = $this->getLayout()->createBlock('expiredproducts/adminhtml_expiredproducts_grid');
            $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
        } 
        /**
         *  Export order grid to Excel XML format
         */
        public function exportExcelAction()
        {
            $fileName   = 'expiredproducts.xml';
            $grid       = $this->getLayout()->createBlock('expiredproducts/adminhtml_expiredproducts_grid');
            $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
        }
}

app/code/local/Dinesh/Expiredproducts/Helper/Data.php

<?php
class Dinesh_Expiredproducts_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app/design/adminhtml/default/default/layout/expiredproducts.xml

<?xml version="1.0"?>
<layout version="0.1.0">
  <expiredproducts_adminhtml_expiredproducts_index>
    <reference name="content">
      <block type="expiredproducts/adminhtml_expiredproducts" name="expiredproducts" />
    </reference>
  </expiredproducts_adminhtml_expiredproducts_index>
</layout>
  • Hi @ Dinesh Yadav, I have followed your instructions but it shows error. I have to go to Admin->Reports->products->expired products click it shows 404 error.How to fix these error? Can you please help me? Please find my screenshots.prnt.sc/hirdqh this link is admin below link is an error.prnt.sc/hirbvd – Dileep Kumar Dec 4 '17 at 5:40
  • There might be an issue in your controller action. – Dinesh Yadav Dec 4 '17 at 6:01
  • Which file I am missed controller function.Can you please tell me? – Dileep Kumar Dec 4 '17 at 6:07
  • My site URL shows this link 127.0.0.1/magento/index.php/admin_expiredproducts/… – Dileep Kumar Dec 4 '17 at 6:09
  • 1
    I have changed to this path app/code/local/Dinesh/Expired products/Adminhtml/ExpiredproductsController.php to app/code localDinesh/Expired products/controllers/Adminhtml/ExpiredproductsController.php after I have click expired products options it shows below screenshot.prnt.sc/hirtcy.Can you please help what I am missing – Dileep Kumar Dec 4 '17 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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