在Drupal Commerce 2中为配送方式创建自定义字段遇阻求助
解决Commerce配送方式字段不显示问题
问题排查与修复步骤
1. 修正配置文件命名与存放路径
Drupal的配置文件必须遵循固定命名规则,且需放在模块的config/install目录下,安装模块时才会自动导入:
- 字段存储文件命名应为:
field.storage.commerce_shipping_method.field_opening_hours_x.yml - 字段实例文件命名应为:
field.commerce_shipping_method.commerce_shipping_method.field_opening_hours_x.yml
你之前使用的自定义命名不符合Drupal配置文件识别规则,系统无法自动导入配置。
2. 验证配置内容正确性
字段存储配置(field.storage.commerce_shipping_method.field_opening_hours_x.yml)
langcode: en status: true dependencies: module: - commerce_shipping - number id: commerce_shipping_method.field_opening_hours_x field_name: field_opening_hours_x entity_type: commerce_shipping_method type: decimal module: number settings: precision: 2 scale: 2 locked: false cardinality: 1 translatable: true indexes: { } persist_with_no_fields: true custom_storage: false
补充
scale参数:decimal类型字段需同时设置精度(precision)和小数位数(scale),否则配置可能无法正常生效。
字段实例配置(field.commerce_shipping_method.commerce_shipping_method.field_opening_hours_x.yml)
langcode: en status: true dependencies: config: - field.storage.commerce_shipping_method.field_opening_hours_x module: - number id: commerce_shipping_method.commerce_shipping_method.field_opening_hours_x field_name: field_opening_hours_x entity_type: commerce_shipping_method bundle: commerce_shipping_method label: 'Test Numeric Field' description: 'A test field' required: false default_value: { } default_value_callback: '' settings: { } third_party_settings: { } deleted: false
3. 重新部署配置
- 卸载当前模块:
drush pm-uninstall custom_module -y - 确认配置文件已放入
custom_module/config/install/目录 - 重新安装模块:
drush pm-enable custom_module -y - 若配置未自动导入,手动执行:
drush config-import -y
4. 检查字段状态
进入/admin/commerce/config/shipping-methods/fields查看字段是否存在,若字段已导入,编辑配送方式页面会自动加载该字段。
关于营业时间字段的优化建议
如果最终要实现营业时间功能,推荐:
- 使用
daterange字段类型(需启用datetime模块),通过时间范围记录单时段营业区间 - 若需支持多时段配置,将字段基数设置为
Unlimited,允许添加多条营业时间记录
内容的提问来源于stack exchange,提问作者Zathrus Writer
相关产品推荐
相关产品推荐

