如何在运送方法(后端)中添加“自定义描述"字段 [英] How to add Custom Description field in Shipping methods (Backend)

查看:41
本文介绍了如何在运送方法(后端)中添加“自定义描述"字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运输方式下的运输区域"页面中添加一个自定义字段,它将是文本输入,用户将能够添加自定义消息,我将在前端显示该消息.

I want to add a custom field in Shipping Zone page under shipping method, it will be a text input, the user will able to add a custom message and I'll show that message in the front end.

我注意到它将数据保存在

wp_woocommerce_shipping_zone_methods 表中,该表没有任何额外的列可保存数据;所以我认为我必须使用自定义逻辑,但是我不知道钩子的名称.

I noticed it saves the data in wp_woocommerce_shipping_zone_methods table which doesn't have any extra column to save the data; so I think I have to use my custom logic, but I don't know the name of the hook(s).

所以我的问题是,是否有任何钩子会帮助/允许我

So my question is, is there is any hook which will help/allow me

  1. 添加自定义字段.
  2. 添加自定义列.

TL; DR:

推荐答案

add_action('woocommerce_init', 'shipping_instance_form_fields_filters');

function shipping_instance_form_fields_filters()
{
    $shipping_methods = WC()->shipping->get_shipping_methods();
    foreach($shipping_methods as $shipping_method) {
        add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
    }
}

function shipping_instance_form_add_extra_fields($settings)
{
    $settings['shipping_extra_field'] = [
        'title' => 'Shipping extra field',
        'type' => 'text', 
        'placeholder' => 'shipping',
        'description' => ''
    ];

    return $settings;
} 

感谢@Wprog_dy的想法,但是您的代码仅将字段添加到"flat_rate"传送方法中,因此您的功能确实非常复杂.

Thanks @Wprog_dy for idea, but your code only adds field to the 'flat_rate' shipping method and your function is really weirdly complicated.

我的示例将在所有送货方式中添加一个自定义字段

My example will add a custom field to all shipping methods

这篇关于如何在运送方法(后端)中添加“自定义描述"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆