亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

magento 添加上傳功能 Adding upload field in

系統 2189 0

Introduction

Nowadays most of the clients wants extra fields in contacts form of magento to fulfill their requirements. And adding custom fields other than upload field (for example: text, textarea, select etc) in contact form are easy in the sense you can easily include the field values in contact email template.
But adding upload field can be trickier as you have to process the file upload operation and attach it with your email contents.


Steps

1. Modify the contact form template
You need to modify the contact form template file: app/design/frontend/[your-interface]/[your-theme]/template/contacts/form.phtml:


a> Add enctype=”multipart/form-data” in <form> element as:

    <form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data">
  

?

b> Add file upload field below ‘Comment’ field (or anywhere you want) as:

    <li>
    <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
    <div class="input-box">
        <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
        <input name="attachment" id="attachment" class="input-text" type="file" />
    </div>
</li>
  
?

2. Create controller class for processing file upload

Next step is to override the Mage_Contacts_IndexController class.
For this you need to create a custom module. For tutorial purpose I am assuming Namespace to be ‘MagePsycho’ and Module to be ‘Customcontact’.

?

a> Add xml override code in your module’s config.xml as:

    <frontend>
    <routers>
        <contacts>
            <args>
                <modules>
                    <magepsycho_customcontact before="Mage_Contacts">MagePsycho_Customcontact</magepsycho_customcontact>
                </modules>
            </args>
        </contacts>
    </routers>
</frontend>
  

?

b> Create your custom controller
Create controller file in your module dir as: app/code/local/MagePsycho/Customcontact/controllers/IndexController.php
and copy the following code:

    <?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class MagePsycho_Contactspro_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);
 
                $error = false;
 
                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }
 
                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }
 
                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }
 
                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }
 
                /**************************************************************/
                $fileName = '';
                if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') {
                    try {
                        $fileName       = $_FILES['attachment']['name'];
                        $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                        $fileNamewoe    = rtrim($fileName, $fileExt);
                        $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;
 
                        $uploader       = new Varien_File_Uploader('attachment');
                        $uploader->setAllowedExtensions(array('doc', 'docx','pdf'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media') . DS . 'contacts';
                        if(!is_dir($path)){
                            mkdir($path, 0777, true);
                        }
                        $uploader->save($path . DS, $fileName );
 
                    } catch (Exception $e) {
                        $error = true;
                    }
                }
                /**************************************************************/
 
                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
 
                /**************************************************************/
                //sending file as attachment
                $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
                if(file_exists($attachmentFilePath)){
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
                    $attachment->filename = $fileName;
                }
                /**************************************************************/
 
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );
 
                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }
 
                $translate->setTranslateInline(true);
 
                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
                $this->_redirect('*/*/');
 
                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);
 
                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }
 
        } else {
            $this->_redirect('*/*/');
        }
    }
}
  
?

3> There you go.
Try to attach file and submit the contact form, you will get that file as attachment in contact email.

?

[Snapshots]

magento 添加上傳功能 Adding upload field in contact form and send as attachment

Default Contact Form

?

?

magento 添加上傳功能 Adding upload field in contact form and send as attachment

Contact Form With Upload Field

?

?

Hope this helps you.
Happy E-Commerce!

?

來源: http://www.blog.magepsycho.com/adding-upload-field-in-contact-form-and-send-as-attachment/

?

?

?

magento 添加上傳功能 Adding upload field in contact form and send as attachment


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 四虎视频国产精品免费 | 成人性色大片 | 97综合视频 | 亚洲成人一区 | 美女网站视频免费 | 中文字幕久久亚洲一区 | 特级毛片网站 | 日韩亚洲欧美在线观看 | 国产不卡在线看 | 亚洲精品久久激情影院 | 五月天婷婷网址 | 91午夜精品亚洲一区二区三区 | 中文国产成人精品久久一区 | 九九伦理影院手机观看 | 91亚洲精品福利在线播放 | 亚洲国产欧美久久香综合 | 天天操夜夜操免费视频 | 久久久国产亚洲精品 | 国产精品日日做人人爱 | 性欧美video另类3d | 亚洲综合狠狠 | 欧美精品成人一区二区在线观看 | 成人毛片一区二区三区 | 深夜网站在线观看 | 欧美特黄a级高清免费大片 欧美特黄a级猛片a级 | 久久免费观看爱情动作片 | 国产精品福利久久香蕉中文 | 国产欧美另类第一页 | 日韩精品特黄毛片免费看 | 国产精品视频免费一区二区三区 | 亚洲黄a | 欧美成人免费全网站大片 | 午夜私人影院在线观看 | 国产一级淫片a视频免费观看 | 国产99久久精品 | 97影院不用| 偷亚洲偷国产欧美高清 | 国产女人体一区二区三区 | 日本一区高清视频 | 国内一区二区 | 精品视频久久 |