簡體   English   中英

自定義Woocommerce模板my-account / form-edit-addresses

[英]Customizing Woocommerce template my-account/form-edit-addresses

/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>

請注意,上面這些只是從inspect中獲取的HTML標記,並不是使表單有效的正確字段。 我可以處理 - 它只是找到或替換字段。

這是當前的布局,不能很好地適應我的需求

這就是我要求表單看起來像使用上面的標簽

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

  2. 第三種是在提交重定向到/my-account/而不是/my-account/edit-addresses/填寫表格,並包含任何Woocommerce通知

  3. 最后一點是刪除First Name Last Name Email Phone字段。 我假設將通過完成步驟1並刪除不需要的表單字段來輕松修復。

非常感謝任何時候尋找解決方案。

干杯

您不能將<p>標記(刪除所有現有屬性+值)更改為<div>因為這肯定會破壞某些進程,因為在此處啟用了特定的woocommerce javascript ...

現在你可以使用woocommerce_form_field_args過濾器鈎子在所有<p>標簽上全局更改 ,你也可以在所有字段上刪除所需的行為,所以<abbr class="required" title="required">*</abbr>將被刪除。

這是代碼:

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 ...

我已經找到了我的問題的第2,第3和第4部分 - 以防其他人正在尋找......

第2部分是將form-edit-address.phpedit-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;
}

任何有關這個問題的其他部分的幫助將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM