Value not getting bind in the Magento controller

2019-09-19 02:52发布

问题:

This is the predefined post function inside Magento controller.

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('*/*/');
    }
}

When I log the producturl attribute, it prints the correct view.But I do not get that value in my email. How to get that value binded so it gets send to the email?

My form looks like this:

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

回答1:

So, the emails templates are html files that you can actually find in the folder app/locale/[some_locale]/template/ if your locale is english of the US then some_locale will be en_US, if that is french from Belgium, then it will be fr_BE, so that is the language code on 2 letter as defined by ISO 639-1 then an underscore _ and country code as defined by ISO 3166-1 alpha-2.

And the name of the file will be something you could find if you do a global search on the handle of the template you actually stated in you comment.

So here contacts_email_email_template is actually defined in app/code/core/Mage/Contacts/etc/config.xml as being the file 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>

So if you go and edit app/locale/en_US/template/email/contact_form.html that I reproduced here below and in which I added your product url value, that should work :

<!--@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, actually being the key of the array you are passing as the fifth argument of the function sendTransactional



回答2:

Well if you wish add the product URL to the email you first need to get the product key and wrap it around the static getUrl function.

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

Next just assign the above code to a variable and use some jQuery / Javascript to append the url to the end of the content of the message body. Honestly I would not recommend you to do this.



标签: magento