将自定义字段添加到商店地址部分的 Woocommerce 常规设置

Chr*_*ris 3 php wordpress settings custom-fields woocommerce

我希望将电话号码字段添加到 Woocommerce 设置页面。我可以将该字段添加到设置列表中,但它会附加到整个选项卡的底部。理想情况下,我想在常规选项卡的商店地址部分添加该字段。

这是我现在正在使用的(精简):

add_filter('woocommerce_general_settings', 'woocommerce_add_phone');

function woocommerce_add_phone($settings) {

    $settings[] = array(
        'title'    => 'Phone Number',
        'type'     => 'text',
    );

    $sections[] = array( 'type' => 'sectionend', 'id' => 'store_address' );

    return $settings;

}
Run Code Online (Sandbox Code Playgroud)

我尝试使用array_splice但无法使用它。

任何建议将不胜感激。

Loi*_*tec 7

这可以通过一种简单的方式来完成,以woocommerce_store_postcode字段 ID为目标,在此常规设置中的正确位置插入商店电话自定义字段:

add_filter('woocommerce_general_settings', 'general_settings_shop_phone');
function general_settings_shop_phone($settings) {
    $key = 0;

    foreach( $settings as $values ){
        $new_settings[$key] = $values;
        $key++;

        // Inserting array just after the post code in "Store Address" section
        if($values['id'] == 'woocommerce_store_postcode'){
            $new_settings[$key] = array(
                'title'    => __('Phone Number'),
                'desc'     => __('Optional phone number of your business office'),
                'id'       => 'woocommerce_store_phone', // <= The field ID (important)
                'default'  => '',
                'type'     => 'text',
                'desc_tip' => true, // or false
            );
            $key++;
        }
    }
    return $new_settings;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

测试和工作......你会得到类似的东西:

在此处输入图片说明