覆盖生日表单的CustomerFormatter.php

时间:2018-06-10 11:46:28

标签: prestashop-1.7

我想强制生日字段,所以我覆盖CustomerFormatter但它根本不起作用。这就是我的表现:

<?php

class CustomerFormatter extends CustomerFormatterCore
{
    public function getFormat()
    {
        $customerForm = parent::getFormat();

        if ($customerForm->ask_for_birthdate) {
            $format['birthday'] = (new FormField)
                ->setName('birthday')
                ->setType('text')
                ->setLabel(
                    $customerForm->translator->trans(
                        'Birthdate', [], 'Shop.Forms.Labels'
                    )
                )
                ->addAvailableValue('placeholder', Tools::getDateFormat())
                ->addAvailableValue(
                    'comment',
                    $customerForm->translator->trans('(E.g.: %date_format%)', array('%date_format%' => Tools::formatDateStr('31 May 1970')), 'Shop.Forms.Help')
                )
                ->setRequired(true)
            ;
        }
    }
}

我在Prestashop 1.7.3.3

之下

你对这个问题有所了解吗?谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

可能对您来说太迟了,但可能会对其他人有所帮助,不幸的是,使用1.7不可能实现。

“是的,在没有名称空间的所有类上都可以照常工作(因此您仍然可以覆盖产品,地址等)。”

http://build.prestashop.com/news/prestashop-1-7-faq/#can-developers-still-use-overrides-in-17

答案 1 :(得分:0)

您需要转到public_html / override / classes / form /并创建文件CustomerFormatter.php,然后键入以下代码:

<?php
/**
 * @Override CustomerFormatter
 */

use Symfony\Component\Translation\TranslatorInterface;

class CustomerFormatter extends CustomerFormatterCore
{

    private $translator;
    private $language;

    private $ask_for_birthdate = true;
    private $ask_for_password = true;
    private $password_is_required = true;
    private $ask_for_new_password = false;

    public function __construct(
        TranslatorInterface $translator,
        Language $language
    ) {
        parent::__construct($translator, $language);
        $this->translator = $translator;
        $this->language = $language;
    }

    public function getFormat()
    {
        $format = parent::getFormat();

        $format = [];

        if ($this->ask_for_birthdate) {
            $format['birthday'] = (new FormField())
                ->setName('birthday')
                ->setType('birthday')
                ->setLabel(
                    $this->translator->trans(
                        'Birthdate',
                        [],
                        'Shop.Forms.Labels'
                    )
                )
                ->setRequired($this->password_is_required);
        }
    }

您需要使用:

->setRequired($this->password_is_required)

因为客人购买的结帐信息有误。因此,这将是一个可选字段。