0

I wish to add a contact us page customized for the logged in user in magento2 website. It should be like it's activated when a person is logged in and it should not ask for name or email , it should be populated with the name from account only. Only text field to ask a question should be active. I tried putting a block with a call to the contact us page, but it won't work as I need it to work. How should I approach this? thanks

1 Answer 1

0

In your theme directory, create directory Magento_Contact with subdirectory templates. In templates create the file form.phtml with this contents:

<form class="form contact"
      action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>"
      id="contact-form"
      method="post"
      data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
      data-mage-init='{"validation":{}}'>
    <fieldset class="fieldset">
        <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Write Us') ?></span></legend><br />
        <div class="field note no-label"><?php /* @escapeNotVerified */ echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
        <div class="field name required">
            <label class="label" for="name"><span><?php /* @escapeNotVerified */ echo __('Name') ?></span></label>
            <div class="control">
                <input <?php echo empty($this->helper('Magento\Contact\Helper\Data')->getUserName()) ? '' : 'disabled' ?> name="name" id="name" title="<?php /* @escapeNotVerified */ echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
            </div>
        </div>
        <div class="field email required">
            <label class="label" for="email"><span><?php /* @escapeNotVerified */ echo __('Email') ?></span></label>
            <div class="control">
                <input <?php echo empty($this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ? '' : 'disabled' ?> name="email" id="email" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
            </div>
        </div>
        <div class="field telephone">
            <label class="label" for="telephone"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label>
            <div class="control">
                <input name="telephone" id="telephone" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone')) ?>" class="input-text" type="text" />
            </div>
        </div>
        <div class="field comment required">
            <label class="label" for="comment"><span><?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?></span></label>
            <div class="control">
                <textarea name="comment" id="comment" title="<?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('comment')) ?></textarea>
            </div>
        </div>
        <?php echo $block->getChildHtml('form.additional.info'); ?>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary">
            <input type="hidden" name="hideit" id="hideit" value="" />
            <button type="submit" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>" class="action submit primary">
                <span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
            </button>
        </div>
    </div>
</form>

This will add some custom logic to the form that disables the email and name inputs if the customer is logged in.

6
  • Hi, I did what you described here. But I am just getting a line and nothing else . The line says, "Write Us Jot us a note and we’ll get back to you as quickly as possible. Name".
    – Farid Haq
    Commented Aug 20, 2016 at 5:11
  • 1drv.ms/i/s!AmO6Xk8N1CMfgaRTBaB6E9ON1sCnkA
    – Farid Haq
    Commented Aug 20, 2016 at 5:18
  • Try flushing the cache. Commented Aug 20, 2016 at 5:52
  • I did that. But its showing same
    – Farid Haq
    Commented Aug 20, 2016 at 15:25
  • You could try deleting contents of pub/static and var/view_preprocessed folders. What version are you using? The above code works in 2.1 but I'm not sure about older versions. Commented Aug 20, 2016 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.