1

我正在学习 Shopware,但遇到了一些我无法解决的问题。

我正在编写一个为客户添加属性的测试插件。我已将对应字段添加到注册表单,它会自动将其值保存到数据库中,就像我在文档中的某处读到的一样。

现在我想让该属性在帐户配置文件页面中的密码字段之后可编辑。我设法将输入放在那里,甚至显示数据库中的值。但是当我更改值并保存时,该值没有更新。我不知道这是否只是正确获取字段名称的问题,还是我需要覆盖其他内容。还是只是不可能?任何有关如何实现这一目标的帮助将不胜感激。

相关代码如下:

插件引导

public function install(InstallContext $context)
{
    $service = $this->container->get('shopware_attribute.crud_service');
    $service->update('s_user_attributes', 'test_field', 'string');

    $metaDataCache = Shopware()->Models()->getConfiguration()->getMetadataCacheImpl();
    $metaDataCache->deleteAll();
    Shopware()->Models()->generateAttributeModels(['s_user_attributes']);

    return true;
}

注册/personal_fieldset.tpl

{extends file="parent:frontend/register/personal_fieldset.tpl"}

{block name='frontend_register_personal_fieldset_password_description'}
{$smarty.block.parent}

<div class="register--test-field">
    <input autocomplete="section-personal test-field"
           name="register[personal][attribute][testField]"
           type="text"
           placeholder="Test Field"
           id="testfield"
           value="{$form_data.attribute.testField|escape}"
           class="register--field{if $errorFlags.testField} has--error{/if}"
            />
</div>
{/block}

帐户/profile.tpl

{extends file="parent:frontend/account/profile.tpl"}

{block name='frontend_account_profile_profile_required_info'}
<div class="profile--test-field">
    <input autocomplete="section-personal test-field"
           name="profile[attribute][testfield]"
           type="text"
           placeholder="Test Field"
           id="testfield"
           value="{$sUserData.additional.user.test_field|escape}"
           class="profile--field{if $errorFlags.testField} has--error{/if}"
    />
</div>

{$smarty.block.parent}
{/block}
4

1 回答 1

2

它在注册时使用的表单类型与您在个人资料中使用的表单类型不同。如果你检查 \Shopware\Bundle\AccountBundle\Form\Account\PersonalFormType::buildForm,你可以看到

$builder->add('attribute', AttributeFormType::class, [
            'data_class' => CustomerAttribute::class
        ]);

这意味着属性包含在表单中并且它们将被持久化。这就是为什么您可以将值保存在注册表单上的原因。

在配置文件中,您有 \Shopware\Bundle\AccountBundle\Form\Account\ProfileUpdateFormType。并且这里的属性没有添加到表单构建器中。

如何扩展 ProfileUpdateFormType?

  1. 在 Bootstrap(或特定订阅者类)上订阅 Shopware_Form_Builder

    $this->subscribeEvent('Shopware_Form_Builder', 'onFormBuild');

  2. 创建 onFormBuild 方法以添加您的逻辑

    公共函数 onFormBuild(\Enlight_Event_EventArgs $event) { if ($event->getReference() !== \Shopware\Bundle\AccountBundle\Form\Account\ProfileUpdateFormType::class) { return; } $builder = $event->getBuilder();

        $builder->add('attribute', AttributeFormType::class, [
            'data_class' => CustomerAttribute::class
        ]);
    }
    

使用这种方法,您的个人资料表单上的所有属性都可用。

您的其他可能性是使用“附加”属性而不是“属性”,然后订阅控制器事件或挂钩控制器操作来处理您的自定义数据。

于 2017-02-24T10:27:51.717 回答