如何“以编程方式”在Magento 1.7.0.2中通过联系表单询问要约

时间:2013-09-19 08:15:58

标签: php jquery forms magento email

我正在寻找一个新的联系表格来询问Magento Eshop的报价。

此“询问优惠”表单将为访问者提供填写某些字段的选项,并且只是发送了与默认联系表单完全相同的电子邮件。< / p>

与默认联系表单的唯一区别在于访问者无需插入电子邮件即可。

所以到目前为止,我的方法是将文件 contacts / form.phtml 发布到 contacts / askforanoffer.phtml

当然我在文件夹 layout / askforanoffer.xml

中创建了新的 xml

问题是这个。  我应该在哪里更改代码,以便不必为了发送包含此要约表单的电子邮件而无需插入用户?

3 个答案:

答案 0 :(得分:2)

您必须为此创建单独的模块。我也在这里创建和共享代码。表单提交是由ajax。可能是我错过了删除或重命名的内容。试着纠正它。

应用\等\模块\ namespace_modulename.xml

<Namespace_Modulename>
        <active>true</active>
        <codePool>local</codePool>
    </Namespace_Modulename>

应用\代码\本地\命名空间\ MODULENAME \助手\ Data.php

<?php class Namespace_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{

}
?>

应用\代码\本地\命名空间\ MODULENAME \等\ config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.0.01</version>
        </Namespace_Modulename>
    </modules>
    <frontend>

        <routers>
            <contacts>
                <args>
                    <modules>
                        <Namespace_Modulename before="Mage_Contacts">Namespace_Modulename</Namespace_Modulename>
                    </modules>
                </args>
            </contacts>
        </routers>

    </frontend>

    <global>
        <helpers>
            <Modulename>
                <class>Namespace_Modulename_Helper</class>
            </Modulename>
        </helpers>
        <template>
            <email>
                <havequestion_email_email_template translate="label" module="contacts">
                    <label>Have a Question Form</label>
                    <file>havequestion_form.html</file>
                    <type>text</type>
                </havequestion_email_email_template>
            </email>
        </template>
    </global>

</config>

应用\代码\本地\命名空间\ MODULENAME \控制器\ IndexController.php

 public function havequestionpostAction()
    {
        $template_path = 'havequestion_email_email_template';
        //$post = $this->getRequest()->getPost();
        $post = array (
                'name' => $this->getRequest()->getParam('name'), 
                'email' => $this->getRequest()->getParam('email'),
                'comment' => $this->getRequest()->getParam('comment')

                );

        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;                  
                }

                if ($error) {
                    throw new Exception();
                }

                $mailTemplate = Mage::getModel('core/email_template');

                /* @var $mailTemplate Mage_Core_Model_Email_Template */


                $senderDetail = Mage::getStoreConfig('trans_email/ident_'.Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER));
                $senderDetail['name'] = $post['name'];

                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        $template_path,
                        $senderDetail,
                        //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->_redirectUrl($post['currentpage']);
                echo 'SUCCESS';

                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->_redirectUrl($post['currentpage']);
                echo '<div class="error-msg">Unable to submit your request. Please, try again later.</div>';                
                return;
            }

        } else {
            echo '<div class="error-msg">Unable to submit your request. Please, try again later.</div>';
            //$this->_redirectUrl($post['currentpage']);
            return;
        }
    }

}

应用\设计\前端\默认\ YOUR_TEMPLATE_PATH \模板\接触\ havequestionform.phtml

<div class="form-add">
<form action="<?php echo $this->getUrl('') ?>contacts/index/havequestionpost/" id="havequestionForm" method="post">
        <div class="question-ajax-msg"></div>
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
                    <div class="input-box">
                        <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                    <div class="input-box">
                        <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
                    </div>
                </div>
            </li>           
            <li class="wide">
                <label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
                <div class="input-box">
                    <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
                </div>
            </li>
        </ul>

    <div class="">
        <p class="required" style="text-align:left"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
                    <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
        <span class="question-ajax-loading">&nbsp;</span>
    </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var havequestionForm = new VarienForm('havequestionForm', true);

    // submit have a question by ajax
        jQuery('#havequestionForm').submit(function (e){
            e.preventDefault();
            jQuery(".question-ajax-loading").css('display','block');

            var name = jQuery("#name").val();
            var email = jQuery("#email").val();
            var comment = jQuery("#comment").val();
            var hideit = jQuery("#hideit").val();
            var prosku = jQuery("#prosku").val();
            var prostyle = jQuery("#prostyle").val();
            var procolor = jQuery("#procolor").val();
            var currentpage = jQuery("#currentpage").val();

            var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment + '&hideit=' + hideit + '&prosku=' + prosku + '&prostyle=' + prostyle + '&procolor=' + procolor + '&currentpage=' + currentpage;

            url = jQuery('#havequestionForm').attr('action');

            jQuery.ajax( {
                url : url,
                Type : "POST",
                data: dataString, 
                success: function(data) {
                    jQuery(".question-ajax-msg").css('display','block');

                    if(data == 'SUCCESS'){
                        var successMsg = '<div class="success-msg">Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.</div>';
                        jQuery(".question-ajax-msg").html(successMsg);                      
                        jQuery("#comment").val('');
                    }                   
                    else {                      
                        jQuery(".question-ajax-msg").html(data);
                        jQuery("#comment").val('');
                    }

                    jQuery(".question-ajax-loading").css('display','none');
                    jQuery('.question-ajax-msg').delay(5000).fadeOut('slow');


                }

            });

            return false;
        });
//]]>
</script>
</div>

答案 1 :(得分:1)

答案 2 :(得分:1)

您可以使用以下文档

具有自定义数据库表的自定义模块

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

Magento Commerce的前端编辑器扩展

http://www.youtube.com/watch?v=ALgdsMCVuH4

请参见上面的网址。

希望这对您有所帮助,