PrestaShop: add custom field into registration and profile pages

Trabla: PrestaShop: add custom  field into registeration and profile pages


PrestaShop eCommerce shop - howto add custom  field into registration and profile pages - codingtrabla tutorial 1


PrestaShop eCommerce shop - howto add custom  field into registration and profile pages - codingtrabla tutorial 2

PrestaShop eCommerce shop - howto add custom  field into registration and profile pages - codingtrabla tutorial 3


Solving:


1. Add new field 'favorite_hero' into 'ps_customer' table

ALTER TABLE `ps_customer` ADD COLUMN `favorite_hero` varchar(255) DEFAULT NULL;

2. Create file Customer.php in  /override/classes/ folder and copy following code (extending CustomerCore class):

<?php

class Customer extends CustomerCore {

    public $favorite_hero = null;

    /**
     * this is default definition from CustomerCore class + new custom field 
     * "favorite_hero"
     */
    public static $definition = array(
        'table' => 'customer',
        'primary' => 'id_customer',
        'fields' => array(
            'secure_key' => array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),
            'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
            'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
            'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
            'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 32),
            'last_passwd_gen' => array('type' => self::TYPE_STRING, 'copy_post' => false),
            'id_gender' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
            'newsletter' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'newsletter_date_add' => array('type' => self::TYPE_DATE, 'copy_post' => false),
            'ip_registration_newsletter' => array('type' => self::TYPE_STRING, 'copy_post' => false),
            'optin' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'website' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl'),
            'company' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
            'siret' => array('type' => self::TYPE_STRING, 'validate' => 'isSiret'),
            'ape' => array('type' => self::TYPE_STRING, 'validate' => 'isApe'),
            'outstanding_allow_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'copy_post' => false),
            'show_public_prices' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
            'id_risk' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
            'max_payment_days' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
            'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
            'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
            'note' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'size' => 65000, 'copy_post' => false),
            'is_guest' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
            'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
            'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
            'id_default_group' => array('type' => self::TYPE_INT, 'copy_post' => false),
            'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
            'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
            'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
            
            ///custom field 
            'favorite_hero' => array('type' => self::TYPE_STRING ),
        ),
    );

}


3. Modify authentication.tpl ( is used to render registation form).
Path to file ../prestashop/themes/default-bootstrap/authentication.tpl

<form action="{$link->getPageLink('authentication', true)|escape:'html':'UTF-8'}" method="post" id="account-creation_form" class="std box">
{$HOOK_CREATE_ACCOUNT_TOP}
<div class="account_creation">
<h3 class="page-subheading">{l s='Your personal information'}</h3>
<div class="clearfix">
<label>{l s='Title'}</label>
<br />
{foreach from=$genders key=k item=gender}
<div class="radio-inline">
<label for="id_gender{$gender->id}" class="top">
<input type="radio" name="id_gender" id="id_gender{$gender->id}" value="{$gender->id}" {if isset($smarty.post.id_gender) && $smarty.post.id_gender == $gender->id}checked="checked"{/if} />
{$gender->name}
</label>
</div>
{/foreach}
</div>
<div class="required form-group">
<label for="customer_firstname">{l s='First name'} <sup>*</sup></label>
<input onkeyup="$('#firstname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_firstname" name="customer_firstname" value="{if isset($smarty.post.customer_firstname)}{$smarty.post.customer_firstname}{/if}" />
</div>
<div class="required form-group">
<label for="customer_lastname">{l s='Last name'} <sup>*</sup></label>
<input onkeyup="$('#lastname').val(this.value);" type="text" class="is_required validate form-control" data-validate="isName" id="customer_lastname" name="customer_lastname" value="{if isset($smarty.post.customer_lastname)}{$smarty.post.customer_lastname}{/if}" />
</div>
<div class="required form-group">
<label for="email">{l s='Email'} <sup>*</sup></label>
<input type="text" class="is_required validate form-control" data-validate="isEmail" id="email" name="email" value="{if isset($smarty.post.email)}{$smarty.post.email}{/if}" />
</div>
<div class="required password form-group">
<label for="passwd">{l s='Password'} <sup>*</sup></label>
<input type="password" class="is_required validate form-control" data-validate="isPasswd" name="passwd" id="passwd" />
<span class="form_info">{l s='(Five characters minimum)'}</span>
</div> 
                        
                        <!-- Start Custom Field -->
                        <div class="form-group">
                             <label for="favorite_hero">{l s='Favorite Hero'} </label>
                             <input class="form-control" type="text"  id="favorite_hero" name="favorite_hero" value="{$smarty.post.favorite_hero}" />
                        </div>
                                        
                        <!-- End Custom Field -->
                                       
                        
<div class="form-group">
<label>{l s='Date of Birth'}</label>
<div class="row">

4. Modify  identity.tpl (is used to render profile page )
Path to file ../prestashop/themes/default-bootstrap/identity.tpl

     <input class="is_required validate form-control" data-validate="isName" type="text" name="lastname" id="lastname" value="{$smarty.post.lastname}" />
                </div>
                <div class="required form-group">
                    <label for="email" class="required">
                        {l s='E-mail address'} 
                    </label>
                    <input class="is_required validate form-control" data-validate="isEmail" type="email" name="email" id="email" value="{$smarty.post.email}" />
                </div>
                
                
                
                <!-- Start Custom Field -->
                        <div class="form-group">
                             <label for="favorite_hero">{l s='Favorite Hero'} </label>
                             <input class="form-control" type="text"  id="favorite_hero" name="favorite_hero" value="{$smarty.post.favorite_hero}" />
                        </div>
                                        
                <!-- End Custom Field -->
                
                
                <div class="form-group">
                    <label>
                        {l s='Date of Birth'}
                    </label>

No comments:

Post a Comment