Jump to content

How Can I Change Field Label in Address Form


Osama Nashaat

Recommended Posts

  • 2 weeks later...
  • 5 months later...

I found a solution, it's not a good one but ...

i went on my custom.js in themes/my_theme/js/

then i added 

$('#identity .field5 .form-control-label, #authentication .field5 .form-control-label').html('my_new_text');

yes, yes i changed my text with .html in jQuery be careful too select the good classes to avoid mistakes 

 

the hard part was to have a different class for my different field so i went to themes/my_theme/templates/customer/_partials/customer-form.tpl 

and created a variable that increase at every line. 

<form action="{$action}" id="customer-form" class="js-customer-form" method="post">
  <section>
    {block "form_fields"}
    {assign var="number" value="0"}
      {foreach from=$formFields item="field"}
      {assign var=number value=$number+1}
        {block "form_field"}
          <div class="field{$number}">
             {form_field field=$field}
          </div>
        {/block}
      {/foreach}
    {/block}
  </section>

  <footer class="form-footer clearfix">
    <input type="hidden" name="submitCreate" value="1">
    {block "form_buttons"}
      <button class="btn btn-primary form-control-submit pull-xs-right send_to_adresse" data-link-action="save-customer" type="submit">
        {l s='Save' d='Shop.Theme.Actions'}
      </button>
    {/block}
  </footer>

</form>

with that i have differents variables for every lines and it's easier to target with css/js 

2018-03-13_18h13_46.png.c04794a80a56d4a1725187ddb8f2df89.png

 

Good luck with that 
 

Link to comment
Share on other sites

  • 1 year later...
  • 7 months later...
  • 8 months later...
  • 2 years later...
On 12/7/2020 at 9:22 PM, doncamillo said:

same problem...

I need to rename one of the label but there is not in TRANSLATION tool, any lang file or in databeses.

 

any solution? 

Go to translations or edit directly the table ps_translation.
Edit or add a row (one for each language) where:
 - id_lang id of the langauge
 - key: Address
 - translation: "whatever you want the label to be in the desired language"
 - domain: ShopFormsLabels
 - theme: your theme

Here 1 is EN and 7 is NL for me, yours might differ

image.png.166c72a7c6c29ff5b9ac9c0f05aa6c14.png

Link to comment
Share on other sites

  • 4 months later...
On 3/24/2024 at 11:47 AM, Hoda245 said:

same problem here... 

Any proper solution for it???

Depends on how you define "proper" :D

Here's an example module that can rename the registration field labels and make them translatable at the same time.

<root>/modules/formlabelmangler.php


 

<?php
/** 
 * Module Name: formlabelmangler
 * Description: Example code to show how to modify field labels
 * Version: 8.0.0
 * @author: Paul Campbell [https://www.prestashop.com/forums/profile/11264-paul-c/] 
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
declare(strict_types=1);

if (!defined('_PS_VERSION_')) {
    exit;
}

class formlabelmangler extends Module
{   
    /** @var array */
    private $hooks;

    /**
     * @param string|null $name (Deprecated parameter)
     * @param Context|null $context
     * @return void
     */
    public function __construct()
    {
        $this->name = 'formlabelmangler';
        $this->tab = 'front_office_features';
        $this->author = 'PaulC';
        $this->version = '8.0.0';

        parent::__construct();

        $this->displayName = $this->trans('Fun with forms and hooks.', [], 'Modules:formlabelmangler.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', [], 'Modules.formlabelmangler.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);

        // Hooks to install to
        $this->hooks =  [
            'additionalCustomerFormFields'
        ];
    }

    /**
     * Install and hook module.
     * @return bool True on success, False if an error is encountered
     */
    public function install()
    {
        return 
            parent::install() &&
            $this->registerHook($this->hooks);
    }

    /**
     * Respond whether the module uses the new translation system.
     * 
     * @return bool
     */
    public function isUsingNewTranslationSystem(): bool
    {
        return true;
    }

    /**
     * Inject additional customer registration fields and/or manipulate existing
     * Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true)
     * @see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/FormField.php
     * 
     * @param array FormField &$arguments['fields'] any additional data
     * @return array The field definitions to be rendered
     * 
     */
    public function hookAdditionalCustomerFormFields(array $arguments) : array
    {
        // The current fields which are set can be retrieved with the following
        // since the data is passed by reference.
        $fields = $arguments['fields'];

        // Uncomment the following if you want to record the current field details in the
        // PHP error log for reference.
        
        //error_log('We got ourselves some fields to play in: '. print_r($fields, true));

        // In this example we want to spruce up the 'email' field and call it "Email Address"
        // instead of just plain old "Email".
        foreach ($fields as $field) {
            if ($field->getName() == 'email') {
                $field->setLabel(
                    $this->trans(
                        'Email Address', [], 'Shop.Forms.Labels'
                    ));
            }
        }

        // This is where you would return any additional fields you want to create
        return [];
    }
}

This has been tested with 8.1.4 but should be fine for 1.7.0+ php7+

Edited by Paul C
fixed typos (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...