1

I have created a small module to add new tab on order view page. right now tab is showing a static phtml file with a form in it:

<?php
class MageSf_OrderCustom_Block_Adminhtml_Order_View_Tab_OrderCustom
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{    

public function _construct()
{
    parent::_construct();
    $this->setTemplate('magsf/ordercustom/order/view/tab/ordercustom.phtml');
}

public function getTabLabel() {
    return $this->__('Order Attachment');
}

public function getTabTitle() {
    return $this->__('Order Attachment');
}

public function canShowTab() {
    return true;
}

public function isHidden() {
    return false;
}

public function getOrder(){
    return Mage::registry('current_order');
  }
 } 

ordercustom.phtml includes the following:

if(isset($_POST['docname']))
   {
     try
   {      
    $path = Mage::getBaseDir().DS.'customer_documents'.DS;  //desitnation directory    
    $fname = $_FILES['docname']['name']; //file name                       
    $uploader = new Varien_File_Uploader('docname'); //load class
    $uploader->setAllowedExtensions(array('doc','pdf','txt','docx')); //Allowed extension for file
    $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
    $uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
    $uploader->setFilesDispersion(false);
    $uploader->save($path,$fname); //save the file on the specified path

}
catch (Exception $e)
{
    echo 'Error Message: '.$e->getMessage();
}
}

 <form id="doc-form" name="doc-form" method="post" action="" enctype="multipart/form-data">
 <label>Upload Document</label>
 <input type="file" title="File" name="docname">
 <button type="submit" title="Save"><span>Upload</span></button>                    
 </form>

Here is the config.xml

<?xml version="1.0"?>
<config>
<modules>
    <MageSf_OrderCustom>
        <version>1.0.1</version>
    </MageSf_OrderCustom>
</modules>
<global>
    <blocks>
        <ordercustom>
            <class>MageSf_OrderCustom_Block</class>
        </ordercustom>
    </blocks>
</global>       
<adminhtml>
   <layout>
        <updates>
            <ordercustom>
                <file>magsf_ordercustom.xml</file>
            </ordercustom>
        </updates>
    </layout>
</adminhtml>
<admin> 
<routers>
     <adminhtml> 
         <args> 
             <modules> 
            <MageSf_OrderCustom after="Mage_Adminhtml">MageSf_OrderCustom_Adminhtml</MageSf_OrderCustom> 
            </modules> 
        </args>
   </adminhtml>

And my module configuration file is as:

<?xml version="1.0"?>
<layout>
<adminhtml_sales_order_view>
    <reference name="sales_order_tabs">
        <action method="addTab">
            <name>order_view_tab_ordercustom</name>
               <block>ordercustom/adminhtml_order_view_tab_ordercustom</block>
        </action>
    </reference>
   </adminhtml_sales_order_view>
  </layout>

But issue is this when i submit the form nothing happens and page redirected to the dashboard page. what wrong i am doing and how i can fix this.

Want something like this: enter image description here

7
  • You need to add action URL for your form and create action in your controller. – Jaimin Sutariya Mar 16 '17 at 12:26
  • So it will not work in this way? any example? – Xabby Mar 16 '17 at 12:29
  • You can refer this example webkul.com/blog/… – Jaimin Sutariya Mar 16 '17 at 12:40
  • Actually I have to create the form into a custom tab on admin order view page. So this example is for default Form. do have any example for order view page? @Jaimin Sutariya – Xabby Mar 16 '17 at 12:53
  • Please add your config.xml I'll show what you can do – Jaimin Sutariya Mar 16 '17 at 13:09
1

Create a controller file SfuploadController.php under MageSf/OrderCustom/controllers/Adminhtml

<?php
class MageSf_OrderCustom_Adminhtml_SfuploadController extends Mage_Adminhtml_Controller_Action {  
    public function uploadAction() {
        if (isset($_FILES['docname']['name']) && $_FILES['docname']['name'] != '') {
            try {
                $uploader = new Varien_File_Uploader('docname');
                $uploader->setAllowedExtensions(array('doc','pdf','txt','docx'));
                $uploader->setAllowCreateFolders(true);
                $uploader->setAllowRenameFiles(false);
                $uploader->setFilesDispersion(false);
                $newDir = "customer_documents";

                $newdirPath = Mage::getBaseDir('media') . DS . "customer_documents";

                if (!file_exists($newdirPath)) {
                    mkdir($newdirPath, 0777);
                }

                $path = Mage::getBaseDir('media') . DS . $newDir . DS;
                $resizedPath = Mage::getBaseDir('media') . DS . $newDir;
                $uploader->save($path, $_FILES['docname']['name']);
                $filename = $uploader->getUploadedFileName(); // Uploaded File name
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('core')->__('File uploaded successfully.'));
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('core')->__('Unable to find item to save'));
            }
        }

        $this->_redirectReferer();
    }
}

Update your config.xml and add below code

<admin> 
    <routers>
         <adminhtml> 
             <args> 
                 <modules> 
                <MageSf_OrderCustom after="Mage_Adminhtml">MageSf_OrderCustom_Adminhtml</MageSf_OrderCustom> 
                </modules> 
            </args>
       </adminhtml>
   </routers> 
</admin>

Update your ordercustom.phtml You need to remove all other code from this file.

<form id="doc-form" name="doc-form" method="post" action="<?php echo Mage::helper('adminhtml')->getUrl('adminhtml/sfupload/upload',array('form_key' => Mage::getSingleton('core/session')->getFormKey())); ?>" enctype="multipart/form-data">
 <label>Upload Document</label>
 <input type="file" title="File" name="docname">
 <button type="submit" title="Save"><span>Upload</span></button>                    
 </form>
6
  • Thanks Alot Let me Give it a try hope so it will work fine – Xabby Mar 16 '17 at 13:47
  • Let me know if you face any issue – Jaimin Sutariya Mar 16 '17 at 15:20
  • Bro Its going to 404 page on fronted. 127.0.0.1:8080/magento-1-testing/index.php/admin/… @Jaimin Sutariya – Xabby Mar 17 '17 at 5:12
  • Please update your config.xml file code in the question so we can debug the issue – Jaimin Sutariya Mar 17 '17 at 5:31
  • Please check the question now with updated config.xml – Xabby Mar 17 '17 at 6:11

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.