添加自定义字段prestashop客户

时间:2018-11-01 10:29:23

标签: php prestashop custom-fields prestashop-1.7

嗨,我对prestashop来说还比较陌生,想知道是否有人可以将我引向正确的地方。

基本上,我是在create_account页面上添加了新的自定义字段之后。

我最初是在解决此问题的,但是我知道一旦prestashop更新,对核心文件的所有更改都将被覆盖。

我已经开始对AdminCustomerController.php进行更改 如下。(我尝试添加的字段是业务性质)

class AdminCustomersController extends AdminCustomersControllerCore {

    public function renderForm($id = null)
    {

        $this->fields_form = array(
            'legend' => array(
                'title' => $this->trans('Customer', array(), 'Admin.Global'),
                'icon' => 'icon-user'
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'prefix' => '',
                    'label' => $this->trans('Practice', array(), 'Admin.Global'),
                    'name' => 'practice',
                    'col' => '4',
                    'autocomplete' => false
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('Nature of Business', array(), 'Admin.Global'),
                    'name' => 'nature_enquiry',
                    'col' => '4',
                ),



            )
        );

        return parent::renderForm();
    }
}

还在覆盖文件夹中编辑了Customer.php。如下:

class Customer extends CustomerCore {

    public $practice_from;

    /** nature of enquiry */
    public $nature_enquiry;

    protected  $definition = array(
        'table' => 'customer',
        'primary' => 'id_customer',
        'fields' => array(
         'nature_enquiry' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'required'=>false,'size' => 65000),

        ),
    );

    public function __construct($id = null)
    {
        parent::__construct($id);
        parent::$definition['practice_from'] = ['type' => parent::TYPE_STRING];
    }
}

最后,将我的新字段添加到定义数组底部的classes / Customer.php中,如下所示:

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' => 255),
        'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 255),
        'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
        'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 60),
        '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),
        'reset_password_token' => array('type' => self::TYPE_STRING, 'validate' => 'isSha1', 'size' => 40, 'copy_post' => false),
        'reset_password_validity' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull', 'copy_post' => false),
        'nature_enquiry' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'required'=>false,'size' => 65000),

    ),
);

classes / form / CustomerFormatter.php

$format['nature_enquiry'] = (new FormField)
    ->setName('nature_enquiry')
    ->setLabel(
        $this->translator->trans(
            'Nature of Business', [], 'Shop.Forms.Labels'
        )
    );

理想情况下,我想知道我错过了任何东西,以便在实时站点上正常工作。

或任何将自定义字段添加到注册表单的文档。

PrestaShop-1.7.0.6

谢谢

2 个答案:

答案 0 :(得分:1)

您必须避免编辑核心代码。也不建议覆盖。您应该尝试创建一个模块。

您可以在模块中使用以下挂钩:

public function hookAdditionalCustomerFormFields($params)
{
    //return html of your field
}

public function hookActionSubmitAccountBefore($params)
{
    //process your field posted data
}


public function hookActionAdminCustomersListingFieldsModifier($params)
{
    //show your filed in admin customers list
    $params['fields']['your_new_field'] = array(
        'title' => $this->l('label'),
        'align' => 'center',
    );
}

public function hookActionAdminCustomersFormModifier($params)
{
    dd($params);
    //add your fields
}

答案 1 :(得分:0)

没有详细查看您的更改,但是乍一看它们看上去很可靠。为了在更新时保留这些更改,您将需要“覆盖”这些文件。参见the official docs on overrides。它基本上包括在select (((t2.BIDPX + t2.OFFERPX) / 2) / ((t1.BID + t1.ASK) / 2)) - 1, t2.TRANSACTTIME from NBBO_20181001 t1, FILLORDERACK t2 where t2.symbol = 'F' and t1.symbol = t2.symbol and t2.TRADEDATE = '20181001' and t1.TS between t2.TRANSACTTIME and TIMESTAMPADD(SQL_TSI_ms, 1000, (t2.TRANSACTTIME)) order by t1.TS desc limit 1 目录中创建适当的文件,然后重写要更改的方法。

您还需要删除[prestashop_root_dir]/overrides,以便加载覆盖的文件。