在Magento控制器中没有绑定的值

时间:2016-02-08 16:06:11

标签: magento

这是Magento控制器内的预定义后期功能。

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; //orignal code
                  $error = false;

            }

            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }
            Mage::log($post['producturl']);
            if (!Zend_Validate::is(trim($post['producturl']) , 'NotEmpty')) {
                $error = true;
            }

            //if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
              //  $error = true;
            //}
            Mage::log($error);

            Mage::log($postObject);
            if ($error) {
                throw new Exception();
            }
            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $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('*/*/');
    }
}

当我记录producturl属性时,它会打印正确的视图。但是我的电子邮件中没有得到该值。如何将该值绑定以便发送到电子邮件?

我的表单如下:

<form action="<?php echo $this->getUrl('contacts/index/post'); ?>" id="contactForm" method="post">
<div class="row">
        <div class="col-sm-12">
            <!--<label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>-->
            <div class="input-box">
                <input name="name" id="name" placeholder="Name*" title="<em>*</em><?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
            </div>
        </div>
        <div class="col-sm-12">
            <!--<label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>-->
            <div class="input-box">
                <input name="email" id="email"  placeholder="Email*" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
            </div>
        </div>
        <div class="col-sm-12">
            <!--<label for="telephone" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Mobile') ?></label>-->
            <div class="input-box">
                <input name="telephone" id="telephone" placeholder="Mobile*" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text required-entry" type="text" />
            </div>
        </div>

        <div class="col-sm-12">

            <div class="input-box">
                <input name="producturl" id="producturl" placeholder="Product URL*" title="<?php echo Mage::helper('contacts')->__('ProductURL') ?>" value="url hai bhai" class="input-text required-entry" type="text" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <!--<label for="comment" class=""><?php echo Mage::helper('contacts')->__('Comment') ?></label>-->
            <div class="input-box input-textarea">
                <textarea name="comment" id="comment" rows="3" placeholder="Message" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class=" input-text" placeholder="<?php echo Mage::helper('contacts')->__('Comment') ?>" style="width: 100%; height: 15%;resize:none;"></textarea>
            </div>
        </div>
    </div>

    <div class="row" style="text-align: center">
        <div class="col-sm-12" style="padding-top:2%">
            <input type="text" name="hideit" id="hideit" value="url hai bhai" style="display:none !important;" />
            <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('BOOK A DESIGNER') ?></span></span></button>
        </div>
</div>
</div>

    </div>

2 个答案:

答案 0 :(得分:0)

如果您希望将产品URL添加到电子邮件中,首先需要获取产品密钥并将其包装在静态getUrl函数周围。

Mage::getUrl($_product->getUrlKey())

接下来只需将上面的代码分配给变量,并使用一些jQuery / Javascript将url追加到邮件正文内容的末尾。老实说,我不建议你这样做。

答案 1 :(得分:0)

因此,电子邮件模板是您可以在文件夹app/locale/[some_locale]/template/中实际找到的html文件,如果您的区域设置是美国的英语,那么some_locale将是en_US,如果这是法语来自比利时,它将是fr_BE,因此这是由ISO 639-1定义的2个字母的语言代码,然后是由ISO 3166-1 alpha-2定义的下划线_和国家/地区代码。

如果您对评论中实际陈述的模板句柄进行全局搜索,则可以找到该文件的名称。

因此contacts_email_email_template实际上app/code/core/Mage/Contacts/etc/config.xml定义为文件contact_form.html

<template>
    <email>
        <contacts_email_email_template translate="label" module="contacts">
            <label>Contact Form</label>
            <file>contact_form.html</file>
            <type>text</type>
        </contacts_email_email_template>
    </email>
</template>

因此,如果你去编辑我在下面复制的app/locale/en_US/template/email/contact_form.html并在其中添加了你的产品网址值,那么这应该有效:

<!--@subject Contact Form@-->
<!--@vars
{"var data.name":"Sender Name",
"var data.email":"Sender Email",
"var data.telephone":"Sender Telephone",
"var data.comment":"Comment"}
@-->
Name: {{var data.name}}
Email: {{var data.email}}
Telephone: {{var data.telephone}}

Comment: {{var data.comment}}
<!-- this below line is actually what you are after -->
Product URL : {{var data.producturl}}

data,实际上是您传递的数组的键,作为函数sendTransactional的第五个参数