如何在每种发货方式中向WooCommerce管理发货部分的发货方法添加备注?

我使用WoodMart主题,这仅用于管理用途(仅在管理面板中可见,无需在网站上显示).

我试过了,但不起作用

add_filter( 'woocommerce_shipping_instance_form_fields', 'add_shipping_comments', 10, 2 ); 
 
function add_shipping_comments( $fields, $instance ) { 
    if ( current_user_can( 'manage_options' ) ) { 
        $fields['shipping_comment'] = array( 
            'title'       => __( 'Shipping Comment', 'woocommerce' ), 
            'type'        => 'textarea', 
            'description' => __( 'Enter any additional comments for this shipping method.', 'woocommerce' ), 
            'default'     => "", 
        ); 
    } 
    return $fields;
}

推荐答案

使用以下选项,将WooCommerce Admin Shipping中的自定义字段添加到每个发货方法设置字段:

add_action('woocommerce_init', 'woocommerce_shipping_instances_form_fields_filters');
function woocommerce_shipping_instances_form_fields_filters(){
    foreach( WC()->shipping->get_shipping_methods() as $shipping_method ) {
        add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_methods_additional_custom_field');
    }
}

function shipping_methods_additional_custom_field( $settings ) {
    $settings['shipping_comment'] = array(
        'title'         => __('Shipping Comment', 'woocommerce'),
        'type'          => 'text', 
        'placeholder'   => __( 'Enter any additional comments for this shipping method.', 'woocommerce' ),
    );
    return $settings;
} 

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

enter image description here

Php相关问答推荐

模型中的Laravel自定义查询字段

根据在WooCommerce购物车和 checkout 中 Select 的送货方式显示快捷代码内容

如何减少Laravel分页中的链接数

只收取WooCommerce中最高的运费

如何在Livewire 3 Form类中做依赖注入?

在PHP中处理Linux输入事件(/dev/input/Event*)

将数据推入所有子数组条目

Wordpress,配置一周第一天选项

使用帖子 ID 更新 wp 帖子

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

Golang - Html 模板:如何创建导航项 - 子菜单?我找不到怎么做

将图像作为 PHP post 响应发送到浏览器

根据页面的最后修订日期设置 MediaWiki 内部链接的样式

如何在WooCommerce管理产品列表中显示运输类别列

如何通过额外的属性和本地化来使用 laravel 枚举

PHP 中是否有 is_closure() 函数?

PHP 将数组值插入到 csv 列

自定义错误页面不适用于 Apache 和 nginx 设置的 .htaccess

PHP - 如何获取发送到脚本的查询参数

如何获取最后一条消息属于 Laravel 中特定用户的对话列表