php - 如何将自定义字段添加到 WooCommerce 设置中的“产品库存”选项卡?
问题描述
我正在寻找一种向 WooCommerce 添加新字段的方法 - 设置 - 产品 - 库存
见附图:
经过一些研究,我认为我应该使用woocommerce_inventory_settings
过滤器挂钩,但我不立即知道如何在实践中应用它?
解决方案
woocommerce_inventory_settings
可以class-wc-settings-products.php
在线找到该钩子(在 WooCommerce 4.4.1 中)81
所以你可以使用
// Add custom field: WooCommerce > Settings > Products > Iventory
function filter_woocommerce_inventory_settings( $settings ) {
$settings[] = array(
'title' => __( 'My title', 'woocommerce' ),
'type' => 'title',
'desc' => '',
'id' => 'product_inventory_custom_options',
);
$settings[] = array(
'title' => __( 'My message', 'woocommerce' ),
'id' => 'woocommerce_my_message',
'type' => 'text',
'default' => '',
'class' => '',
'css' => '',
'placeholder' => __( 'Enter my message', 'woocommerce' ),
'desc_tip' => __( 'This is the message that appears when..', 'woocommerce' ),
);
$settings[] = array(
'type' => 'sectionend',
'id' => 'product_inventory_custom_options',
);
return $settings;
}
add_filter( 'woocommerce_inventory_settings', 'filter_woocommerce_inventory_settings', 10, 1 );
要在代码的其他地方或您的网站上获取值,请使用get_option( string $option, mixed $default = false )
- 根据选项名称检索选项值。
// Get message from field
$get_option = get_option( 'woocommerce_my_message' );
推荐阅读
- node.js - AWS CodeBuild 上的 NodeSource SSL 问题
- java - 迭代器接受字符串,但我需要在偶数索引处获取元素?
- arrays - 如何将不可打印的字符转换为 C 中的十六进制值?
- html - 有没有办法在不按下其余内容的情况下将下拉菜单添加到我的导航栏的一部分?
- swift - 使用 .focused() 点击不同视图时如何移除 TextField 焦点?
- php - 如何使用 PHP preg_split 函数
- java - (重新)在改变背景的 JTextField 中绘制文本
- plotly - Plotly Dash 中具有日历布局的网格组件
- reactjs - 由于在它们之前返回,因此无法看到条件渲染
- c# - 如何使用 for 或 foreach 循环将枚举值存储在 c# 中的整数数组中?