【问题标题】:Customizing Woocommerce template my-account/form-edit-addresses自定义 Woocommerce 模板 my-account/form-edit-addresses
【发布时间】:2017-08-13 00:18:37
【问题描述】:

/my-account/edit-addresses/的地址区有些问题

  1. 我想自定义模板form-edit-addresses.php 中的表单域。例如,我想更改所有字段,并单独将一些字段放在单独的类中:
<p class="form-row form-row-first validate-required" id="billing_first_name_field" data-priority="10">
<label for="billing_first_name" class="">First name <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value="Name"></p>

到

<div class="form-group form-group-default input-group">
<div class="form-input-group">
<label for="billing_first_name" class="">Company</label>
<input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value="Name">
</div>

请注意,以上这些只是从检查中获取的 HTML 标记,并不是使表单工作的正确字段。我可以处理 - 它只是查找或替换字段。

  1. 我想要完成的第二件事是将此表单添加到 /my-account/edit-addresses/ URL/Slug 中,而不是 /my-account/edit-addresses/billing

  2. 第三个是在提交重定向到/my-account/而不是/my-account/edit-addresses/时制作表单,并包含任何Woocommerce通知

  3. 最后一点是删除First NameLast NameEmailPhone字段。我假设通过完成第 1 步并删除不需要的表单字段可以轻松修复。

非常感谢您花时间寻找这些问题的解决方案。

干杯

【问题讨论】:

    标签: php wordpress forms woocommerce hook-woocommerce


    【解决方案1】:

    我已经弄清楚了我的问题的第 2、3 和 4 部分 - 以防万一其他人正在寻找......

    第 2 部分是将 form-edit-address.php 添加到 edit-address

    我将此添加到my-address.php 模板中

    <?php
    // get the user meta
    $userMeta = get_user_meta(get_current_user_id());
    
    // get the form fields
    $countries = new WC_Countries();
    $billing_fields = $countries->get_address_fields( '', 'billing_' );
    $shipping_fields = $countries->get_address_fields( '', 'shipping_' );
    ?>
    
    <!-- billing form -->
    <?php
    $load_address = 'billing';
    $page_title   = __( 'Billing Address', 'woocommerce' );
    ?>
    <form action="/my-account/edit-address/billing/" class="edit-account" method="post">
    
        <h2><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title ); ?></h2>
    
        <?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?>
    
        <?php foreach ( $billing_fields as $key => $field ) : ?>
    
            <?php woocommerce_form_field( $key, $field, $userMeta[$key][0] ); ?>
    
        <?php endforeach; ?>
    
        <?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?>
    
        <p>
            <input type="submit" class="button" name="save_address" value="<?php esc_attr_e( 'Save Address', 'woocommerce' ); ?>" />
            <?php wp_nonce_field( 'woocommerce-edit_address' ); ?>
            <input type="hidden" name="action" value="edit_address" />
        </p>
    
    </form>
    

    要做第 3 部分并从 form-edit-address.php 重定向提交,我将以下内容添加到我的主题 functions.php

    function action_woocommerce_customer_save_address( $user_id, $load_address ) { 
       wp_safe_redirect(wc_get_page_permalink('myaccount')); 
       exit;
    }; 
    add_action( 'woocommerce_customer_save_address', 'action_woocommerce_customer_save_address', 99, 2 );
    

    要执行第 4 部分并从 form-edit-address.php 中删除字段,我将以下内容添加到我的主题 functions.php

    add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
    
    function custom_override_billing_fields( $fields ) {
      unset($fields['billing_first_name']);
      unset($fields['billing_last_name']);
      unset($fields['billing_phone']);
      unset($fields['billing_email']);
      return $fields;
    }
    

    对于这个问题的其他部分的任何帮助将不胜感激。

    【讨论】:

    • 没问题。我也认为这个问题太宽泛了,但我不敢删除它,因为我发现你的答案和我的答案很有帮助。我可能会删除这些 cmets,尽管现在我们已经完成了另一件事。祝你有美好的一天。
    • 这很有帮助。已经 4 年了,还有其他选项可以将帐单和运输表格添加到帐户页面的底部吗?
    【解决方案2】:

    您不能将&lt;p&gt; 标记(删除所有现有属性+值)更改为&lt;div&gt;,因为这肯定会破坏某些进程,因为在此启用了特定的 woocommerce javascript……

    现在您可以使用 woocommerce_form_field_args 过滤钩子来全局更改所有 &lt;p&gt; 标记上的 class 并且您可以删除 必需的 > 所有字段的行为,因此 &lt;abbr class="required" title="required"&gt;*&lt;/abbr&gt; 将被删除。

    代码如下:

    add_filter( 'woocommerce_form_field_args', 'custom_wc_form_field_args', 10, 3 );
    function custom_wc_form_field_args( $args, $key, $value ){
        // Only on My account > Edit Adresses
        if( is_wc_endpoint_url( 'edit-account' ) || is_checkout() ) return $args;
    
        $args['class'] = array('form-input-group');
        $args['required'] = false;
    
        return $args;
    }
    

    因此,您必须管理 CSS 才能进行设计更改。

    这个数组中所有可用的参数 $args 是:

    'type'              # @ string
    'label'             # @ string
    'description'       # @ string
    'placeholder'       # @ string
    'maxlength'         # @ boolean
    'required'          # @ boolean
    'autocomplete'      # @ boolean
    'id'                # => $key (argument)
    'class'             # @ array
    'label_class'       # @ array
    'input_class'       # @ array
    'return'            # @ boolean
    'options'           # @ array
    'custom_attributes' # @ array
    'validate'          # @ array
    'default'           # @ string
    'autofocus'         # @ string
    'priority'          # @ string
    

    使用woocommerce_form_field_{field_type} 过滤器挂钩,您可以根据字段类型单独访问字段设置。这个钩子有 4 个可用参数:$field、$key、$args 和 $value...

    【讨论】:

    • 非常感谢。但是,请忽略删除

      ,因为您正确地表明该类是可编辑的。我需要一个

      在开头

      和

    • 我现在已经完成了我想要的,但牺牲了一些布局。总而言之,感谢您的帮助。虽然这次我没有应用它,但我确实将它用作修复的基础。我通常懒得看functions.php,浪费很多时间寻找替代解决方案。我什么时候学=)
    • @LoicTheAztec 嗨;我试图将 billing_address_1 的优先级更改为 state 和 city fields 之后。我设置了switch ($key) { case 'billing_state' : $args['priority'] = '45'; break; case 'billing_city' : break; case 'billing_address_1' : $args['priority'] = '190'; break; default: break;,但它不影响行优先级。我没有任何想法。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签