在商店地址部分的 Woocommerce 常规设置中添加自定义字段
Add a custom field to Woocommerce general setting in store address section
我想在 Woocommerce 设置页面中添加一个 Phone 数字字段。我可以将字段添加到设置列表,但它会附加到整个选项卡的底部。理想情况下,我想在常规选项卡的 Store Address 部分中添加该字段。
这是我现在正在使用的(已删减):
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;
}
我尝试使用 array_splice
但无济于事。
如有任何建议,我们将不胜感激。
此代码将在商店地址部分的 post 代码元素之后添加您的 phone 号码。我借用了here.
的拼接函数
add_filter('woocommerce_general_settings', 'woocommerce_general_settings_add_phone_number');
function woocommerce_general_settings_add_phone_number($settings) {
$phone_number = array(
'title' => __( 'Phone Number', 'woocommerce' ),
'desc' => __( 'Add your phone number.', 'woocommerce' ),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => false,
);
$array_pos = 0;
foreach ($settings as $key => $value) {
if ( $value['id'] == 'woocommerce_store_postcode' ) {
$array_pos = $key;
break;
}
}
$settings = array_insert( $settings, $phone_number, $array_pos + 1 );
return $settings;
}
/*
Array insert
@array the array to add an element to
@element the element to add to the array
@position the position in the array to add the element
*/
if(!function_exists('array_insert')) {
function array_insert($array, $element, $position) {
// if the array is empty just add the element to it
if(empty($array)) {
$array[] = $element;
// if the position is a negative number
} elseif(is_numeric($position) && $position < 0) {
// if negative position after count
if(count($array) + $position < 0) {
$position = 0;
} else {
$position = count($array) + $position;
}
// try again with a positive position
$array = array_insert($array, $element, $position);
// if array position already set
} elseif(isset($array[$position])) {
// split array into two parts
$split1 = array_slice($array, 0, $position, true);
$split2 = array_slice($array, $position, null, true);
// add new array element at between two parts
$array = array_merge($split1, array($position => $element), $split2);
// if position not set add to end of array
} elseif(is_null($position)) {
$array[] = $element;
// if the position is not set
} elseif(!isset($array[$position])) {
$array[$position] = $element;
}
// clean up indexes
$array = array_values($array);
return $array;
}
}
这可以通过一种简单的方式完成,目标是 woocommerce_store_postcode
字段 ID 以在右侧的常规设置中插入 Store phone 自定义字段位置:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试和工作......你会得到类似的东西:
LoicTheAztec 代码工作正常,但我想找到一个没有循环的解决方案。这是我的版本:
function add_phone_number_settings($settings) {
// Search array for the id you want
$key = array_search('woocommerce_store_postcode', array_column($settings, 'id')) + 1;
$custom_setting[] = array(
'title' => __('Phone Number'),
'desc' => __("Enter shop phone number"),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => true,
);
// Merge with existing settings at the specified index
$new_settings = array_merge(array_slice($settings, 0, $key), $custom_setting, array_slice($settings, $key));
return $new_settings;
}
add_filter('woocommerce_general_settings', 'add_phone_number_settings');
我想在 Woocommerce 设置页面中添加一个 Phone 数字字段。我可以将字段添加到设置列表,但它会附加到整个选项卡的底部。理想情况下,我想在常规选项卡的 Store Address 部分中添加该字段。
这是我现在正在使用的(已删减):
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;
}
我尝试使用 array_splice
但无济于事。
如有任何建议,我们将不胜感激。
此代码将在商店地址部分的 post 代码元素之后添加您的 phone 号码。我借用了here.
的拼接函数add_filter('woocommerce_general_settings', 'woocommerce_general_settings_add_phone_number');
function woocommerce_general_settings_add_phone_number($settings) {
$phone_number = array(
'title' => __( 'Phone Number', 'woocommerce' ),
'desc' => __( 'Add your phone number.', 'woocommerce' ),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => false,
);
$array_pos = 0;
foreach ($settings as $key => $value) {
if ( $value['id'] == 'woocommerce_store_postcode' ) {
$array_pos = $key;
break;
}
}
$settings = array_insert( $settings, $phone_number, $array_pos + 1 );
return $settings;
}
/*
Array insert
@array the array to add an element to
@element the element to add to the array
@position the position in the array to add the element
*/
if(!function_exists('array_insert')) {
function array_insert($array, $element, $position) {
// if the array is empty just add the element to it
if(empty($array)) {
$array[] = $element;
// if the position is a negative number
} elseif(is_numeric($position) && $position < 0) {
// if negative position after count
if(count($array) + $position < 0) {
$position = 0;
} else {
$position = count($array) + $position;
}
// try again with a positive position
$array = array_insert($array, $element, $position);
// if array position already set
} elseif(isset($array[$position])) {
// split array into two parts
$split1 = array_slice($array, 0, $position, true);
$split2 = array_slice($array, $position, null, true);
// add new array element at between two parts
$array = array_merge($split1, array($position => $element), $split2);
// if position not set add to end of array
} elseif(is_null($position)) {
$array[] = $element;
// if the position is not set
} elseif(!isset($array[$position])) {
$array[$position] = $element;
}
// clean up indexes
$array = array_values($array);
return $array;
}
}
这可以通过一种简单的方式完成,目标是 woocommerce_store_postcode
字段 ID 以在右侧的常规设置中插入 Store phone 自定义字段位置:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
经过测试和工作......你会得到类似的东西:
LoicTheAztec 代码工作正常,但我想找到一个没有循环的解决方案。这是我的版本:
function add_phone_number_settings($settings) {
// Search array for the id you want
$key = array_search('woocommerce_store_postcode', array_column($settings, 'id')) + 1;
$custom_setting[] = array(
'title' => __('Phone Number'),
'desc' => __("Enter shop phone number"),
'id' => 'woocommerce_store_phone_number',
'default' => '',
'type' => 'text',
'desc_tip' => true,
);
// Merge with existing settings at the specified index
$new_settings = array_merge(array_slice($settings, 0, $key), $custom_setting, array_slice($settings, $key));
return $new_settings;
}
add_filter('woocommerce_general_settings', 'add_phone_number_settings');