如何在运输方式(后端)中添加自定义描述字段
2022-08-30 23:43:39
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”运输方式中,并且您的功能非常复杂。
我的示例将向所有运输方式添加一个自定义字段
这就是我如何以统一费率和免费送货的方式实现n字段custom descriptio
shipping methods
我的文件:function.php
add_filter( 'woocommerce_shipping_instance_form_fields_flat_rate', array( $this, 'add_extra_fields_in_flat_rate' ), 10, 1);
public function add_extra_fields_in_flat_rate($settings)
{
$counter = 0;
$arr = array();
foreach ($settings as $key => $value) <br>
{
if($key=='cost' && $counter==0)
{
$arr[$key] = $value;
$arr['shipping_extra_field'] = array(
'title' => __( 'Shipping Extra Field', 'woocommerce' ),
'type' => 'text',
'placeholder' => 'shipping',
'description' => ''
);
$counter++;
}
else
{
$arr[$key] = $value;
}
}
return $arr;
}