Post by syncspin » Mon Apr 23, 2012 5:27 am

Opencart version: 1.5.1.3
New Install


How can I go about adding a custom field that will appear on the register page, and then in admin as data attached to the customers profile?

Essentially, I want to add a "How did you find us?" drop down.

Thanks for any guidance.

Newbie

Posts

Joined
Mon Jun 27, 2011 5:44 am

Post by Avvici » Mon Apr 23, 2012 5:59 am

Let's place it below the newsletter sign up. You can place it really wherever you want.
Open: catalog/view/theme/your_theme/template/account/register.tpl and find this code:

Code: Select all

<tr>
          <td><?php echo $entry_newsletter; ?></td>
          <td><?php if ($newsletter == 1) { ?>
            <input type="radio" name="newsletter" value="1" checked="checked" />
            <?php echo $text_yes; ?>
            <input type="radio" name="newsletter" value="0" />
            <?php echo $text_no; ?>
            <?php } else { ?>
            <input type="radio" name="newsletter" value="1" />
            <?php echo $text_yes; ?>
            <input type="radio" name="newsletter" value="0" checked="checked" />
            <?php echo $text_no; ?>
            <?php } ?></td>
        </tr>
Right below that code put this:

Code: Select all

  <tr><td>
       <select name="hear">
              <?php if( $hear != ""){?>
              <option value="<?php echo $hear;?>" selected="selected"><?php echo $hear;?></option>
<?php }else{?>
 <option value="<?php echo $hear;?>" selected="selected">Choose One</option>
<?php } ?>
              <option value="Google" selected="selected">Google</option>
               <option value="Yahoo" selected="selected">Yahoo</option>
                <option value="Bing" selected="selected">Bing</option>
                <option value="Friendorrelative" selected="selected">Friend or relative</option>
                 <option value="Advertisement" selected="selected">Advertisement</option>
            </select>
            </td></tr>
Now open catalog/controller/account/register.php and find this line of code:

Code: Select all

if (isset($this->request->post['newsletter'])) {
            $this->data['newsletter'] = $this->request->post['newsletter'];
        } else {
            $this->data['newsletter'] = '';
        } 
Right below it add this:

Code: Select all

if (isset($this->request->post['hear'])) {
            $this->data['hear'] = $this->request->post['hear'];
        } else {
            $this->data['hear'] = '';
        }     
Now open catalog/model/account/customer.php and find this function:

Code: Select all

$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()"); 
Make it look like this:

Code: Select all

$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', hear = '" . (string)$data['hear'] . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()"); 
Now all we have to do is add the field. Open up your database in PHP MY ADMIN and click on the table "customer." Now insert a new field called "hear" (check attached image to see how. The image will show you the proper set up for the field properties)

Save, and you are done. Keep in mind it's not validated so they don't have to choose How They Found You. If you want validation I can add that for you and amend the post. If want to see this data in the admin then you need only to call that field and echo it to the back end on the appropriate page.

Attachments

insertfield.jpg

insertfield.jpg (196.76 KiB) Viewed 12624 times


User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by syncspin » Mon Apr 23, 2012 6:52 am

Thanks so much!

Newbie

Posts

Joined
Mon Jun 27, 2011 5:44 am

Post by Avvici » Mon Apr 23, 2012 6:57 am

If you have trouble getting it to come through to the admin we can help with that. Just let me know.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by hellodesignoxon » Mon Jun 11, 2012 4:40 pm

Any chance that I could ask for a little help on what would need doing to get some custom fields showing up on the order_invoice.tpl specifically, but elsewhere in admin would be good.

I've setup a store where users must register to purchase, and have added 3 new fields to the registration page as per this thread (all works with the info getting added to the database)....what I need now is to get at that info to make it visible on the invoice template.

Any help gratefully received! Thanks in advance...


Posts

Joined
Thu May 17, 2012 10:11 pm

Post by Avvici » Tue Jun 12, 2012 1:08 pm

Contact me on skype Involution Media

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Avvici » Wed Sep 12, 2012 9:04 am

I got your message. Thanks.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC
Who is online

Users browsing this forum: Amazon [Bot] and 76 guests