问题描述
我试图从结帐页面中删除一些字段(特别是:邮政编码、城市、地址 2)
首先是删除城市字段的问题,我去了:国际/地点他们我选择了国家,当我试图删除字段“城市”时我不能,PrestaShop说以下消息:
"The city field (in tab Address) is required"
所以经过一些谷歌,我发现我需要修改:classes/Address.PHP
我评论了整个第 134 行说:
'city' => ['type' => self::TYPE_STRING,'validate' => 'isCityName','required' => true,'size' => 64],
问题仍然存在,我仍然收到相同的消息,所以我编辑了文件 classes/AddressFormat.PHP
我评论了第 63 行“城市”:
/** @var array Default required form fields list */
public static $requireFormFieldsList = [
'firstname','lastname','address1',// 'city','Country:name',];
现在,我终于可以删除该字段了。但问题仍然存在。让我解释一下我自己:
如果您转到结帐页面,您可以看到邮政编码和城市字段。
Screenshot - Address field default
但是,当我选择国家/地区时,一切看起来都是应该的。
Screenshot - Address After selecting country
有什么想法吗?我该怎么做才能删除邮政编码和城市字段?
顺便说一句,我的缓存已关闭,但是,我已经清除了缓存(在性能页面中),并且还清除了浏览器上的缓存。另外,我删除了这个文件夹:/var/cache/prod/
谢谢各位
我使用 Prestashop:1.7.7.2
解决方法
要实现这一点,您只需要更改一个文件。我们更改的文件是核心文件,因此请确保在开始编辑之前进行备份。我们将创建一个过滤器,用于检查 postcode
等值是否在过滤器中。当值在过滤器中时,它将不再创建表单字段。
- 打开文件
Yourstore/classes/form/CustomerAddressFormatter.php
。 - 搜索
public function getFormat()
并导航到部分foreach ($fields as $field) {
我们在 foreach ($fields as $field) {
下面添加
// In this case the fields postcode,adress1 and address2 will be removed from the Addresses form
$ignorefields = ['postcode','address1','address2'];
if (!in_array($field,$ignorefields)) {
然后我们继续向下滚动,直到找到 $format[$formField->getName()] = $formField;
。在这一行下,我们添加一个括号 }
来关闭函数。
因此,您现在将拥有一个像这样的新功能:
foreach ($fields as $field) {
// Add whatever value you which to hide to the array $ignorefields to remove it from the Addresses form-field.
// Available values: address1,address2,postcode,city,State,phone,phone_mobile,company,vat_number
$ignorefields = ['postcode',$ignorefields)) {
$formField = new FormField();
$formField->setName($field);
$fieldParts = explode(':',$field,2);
if (count($fieldParts) === 1) {
if ($field === 'postcode') {
if ($this->country->need_zip_code) {
$formField->setRequired(true);
}
} elseif ($field === 'phone') {
$formField->setType('tel');
} elseif ($field === 'dni' && null !== $this->country) {
if ($this->country->need_identification_number) {
$formField->setRequired(true);
}
}
} elseif (count($fieldParts) === 2) {
list($entity,$entityField) = $fieldParts;
$formField->setType('select');
$formField->setName('id_' . strtolower($entity));
if ($entity === 'Country') {
$formField->setType('countrySelect');
$formField->setValue($this->country->id);
foreach ($this->availableCountries as $country) {
$formField->addAvailableValue(
$country['id_country'],$country[$entityField]
);
}
} elseif ($entity === 'State') {
if ($this->country->contains_states) {
$states = State::getStatesByIdCountry($this->country->id,true);
foreach ($states as $state) {
$formField->addAvailableValue(
$state['id_state'],$state[$entityField]
);
}
$formField->setRequired(true);
}
}
}
$formField->setLabel($this->getFieldLabel($field));
if (!$formField->isRequired()) {
$formField->setRequired(
array_key_exists($field,$required)
);
}
$format[$formField->getName()] = $formField;
} // Add bracket to close our new function - Crezzur
}