Magento 2:如何在注册表中显示地址字段?


14

我想在客户注册表格中显示地址字段(以保存为默认帐单地址)。我知道如何在Magento 1中进行操作。但是我对Magento 2感到震惊。在此之前有人做过吗?

像magento 1中一样的任何xml文件更新

<customer_account_create>
    <reference name="customer_form_register">
        <action method="setData"><key>show_address_fields</key><value>1</value></action>
    </reference>
</customer_account_create>

您将必须通过观察员领取地址数据,并自己创建账单地址记录。为什么不使用现成的扩展名?例如:itoris.com/…–
nico

@Elavarasan,您找到解决方案了吗?如果是,请在此处发布。它可能对其他人有用。
Bojjaiah'7

Answers:


21

使用customer_account_create.xml的示例显示地址字段:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <arguments>
                <argument name="show_address_fields" xsi:type="boolean">true</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

您知道如何在最新版本的Magento(v2.3.4)中实现这一目标吗?过去,我使用过与您的建议类似的方法,并且一直有效,直到2.3.3发行。
Karl Stephens

4

在您的自定义主题中创建此文件:

app/design/frontend/YOUR_PACKAGE/YOUR_THEME/Magento_Customer/layout/customer_account_create.xml

因此,您需要将名为setShowAddressFields的操作方法设置为true,例如该示例:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <action method="setShowAddressFields">
                <argument name="show.address.fields" xsi:type="boolean">true</argument>
            </action>
        </referenceBlock>
    </body>
</page>

1

嗨,首先找到magento根文件夹,然后找到vendor \ magento \ module-customer \ view \ frontend \ layout customer_account_create.xml,然后转到第13行,检查此代码,然后在第17行上放置巨浪部分

<referenceContainer name="content">
            <block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="form/register.phtml">
                <container name="form.additional.info" as="form_additional_info"/>
                <container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
                <action method="setShowAddressFields">
                    <argument name="show.address.fields" xsi:type="boolean">true</argument>
                </action>
            </block>
            <block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
                <arguments>
                    <argument name="triggers" xsi:type="array">
                        <item name="registerSubmitButton" xsi:type="string">.action.submit</item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>

仅使用此部分

<action method="setShowAddressFields">
                        <argument name="show.address.fields" xsi:type="boolean">true</argument>
                    </action>

1

永远不要编辑供应商文件!!

将其复制到您的主题中,然后继续进行,我在搜索设置(后端),因为为什么还要这样编码呢?

如果有人发现了这个让我知道

问候


0

只需打开您的应用程序/设计/前端/包/主题/ Magento_Customer /布局并打开customer_account_create.xml。

转到第16行或查找以下代码。

<container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>

在下面的代码之后,只需将下面的代码放进去。

<action method="setShowAddressFields">
                    <argument name="show.address.fields" xsi:type="boolean">true</argumen>
          </action>

之后,您将在注册页面中看到状态下拉列表。


0

有两种方法可以做到这一点,

  1. 来自模板文件

  2. 从XML

上面xml多次说明了方式,所以我将做模板文件方式。

在主题内打开模板文件[请勿编辑核心文件],app / design / frontend / [供应商名称] / [主题名称] /Magento_Customer/templates/form/register.phtml

找到包含此代码的行,

getShowAddressFields()

它可能在

<?php if($this->getShowAddressFields()): ?>

要么

<?php if ($block->getShowAddressFields()): ?>

取决于您的magento版本。

将此代码放在该行之前,

<?php $this->setShowAddressFields(true); ?>

因此,最终输出应如下所示:

<?php $this->setShowAddressFields(true); ?>
<?php if($this->getShowAddressFields()): ?>

要么

<?php $this->setShowAddressFields(true); ?>
<?php if ($block->getShowAddressFields()): ?>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.