自定义 Woocommerce 结帐字段在页面加载后恢复为默认值

Customized Woocommerce checkout fields reverting to default after page load

我已经使用以下代码成功重命名了我的 woocommerce 结帐页面的“shipping_country”标签:

add_filter( 'woocommerce_checkout_fields', 'rename_woo_checkout_fields' );
function rename_woo_checkout_fields( $fields ) {
    $fields['shipping']['shipping_country']['label'] = 'Country'; 
    return $fields;
}

但是当我尝试添加我想更改的其他标签和占位符时,它不起作用。好吧,实际上发生了一些奇怪的事情。当我刷新页面以应用更改时,它似乎有效,但页面仍在加载,一秒钟后又恢复到原来的状态。 (shipping_country 字段仍然有效,但我添加的所有其他字段都会发生上述情况。

我试过改变顺序,但没关系。

我尝试更改但不起作用的字段是:

    $fields['billing']['billing_address_1']['label'] = 'Address';
    $fields['billing']['billing_address_1']['placeholder'] = 'Street and house number';
    $fields['billing']['billing_city']['label'] = 'City';
    $fields['billing']['billing_postcode']['label'] = 'Postcode';
    $fields['shipping']['shipping_postcode']['label'] = 'Postcode';
    $fields['shipping']['shipping_city']['label'] = 'City';
    $fields['shipping']['shipping_city']['placeholder'] = 'City';
    $fields['shipping']['shipping_address_1']['label'] = 'Address';
    $fields['shipping']['shipping_address_1']['placeholder'] = 'Street and house number';
    $fields['order']['order_comments']['placeholder'] = 'Special notes';

可能是什么导致页面在完成加载页面之前还原更改?

请尝试以下操作:

// For billing and shipping fields
add_filter( 'woocommerce_default_address_fields', 'custom_default_address_fields' );
function custom_default_address_fields( $address_fields ) {
    if ( is_checkout() ) {
        $address_fields['address_1']['label'] = __('Address', 'woocommerce');
        $address_fields['address_1']['placeholder'] = __('Street and house number', 'woocommerce');
        $address_fields['country']['label'] = __('Country', 'woocommerce');
        $address_fields['postcode']['label'] = __('Postcode', 'woocommerce');
        $address_fields['city']['label'] = __('City', 'woocommerce');
        $address_fields['city']['placeholder'] = __('City', 'woocommerce');
    }
    return $address_fields;
}

add_filter( 'woocommerce_checkout_fields', 'change_checkout_fields' );
function change_checkout_fields( $fields ) {
    $fields['order']['order_comments']['placeholder'] = __('Special notes');
    
    return $fields;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。测试并适用于最后一个 WooCommerce 版本 (5.0.0).