提问人:rmalviya 提问时间:4/11/2018 最后编辑:Ruveermalviya 更新时间:6/22/2023 访问量:1850
将自定义字段添加到发货区域表单 - Woocommerce
Add custom field to shipping zone form - Woocommerce
问:
在创建新的运输区域时,我正在尝试将选择字段添加到 Woocommerce 运输选项卡及其运输区域部分。在寻找解决方案时,我在 Woocommerce 的官方文档上发现了这一点。
到目前为止,我尝试过的是:
// Fires on 'plugins_loaded' action of WordPress
public function plugins_loaded_action() {
add_filter( 'woocommerce_get_settings_shipping', array( $this, 'shipping_zone_settings_add_city_field' ), 10, 2 );
}
public function shipping_zone_settings_add_city_field( $settings, $current_section ) {
echo 'this runs only for shipping_options section';
/**
* Check the current section for shipping zone
*/
if( $current_section === 'shipping_zones' ) {
// Add city field to settings
$settings[] = array(
array(
'name' => __( 'Zone City', 'woocommerce' ),
'type' => 'title',
'desc' => __( 'Specify city names for current shipping region', 'woocommerce' ),
'id' => 'shipping_zones',
),
array(
'name' => __( 'Zone Cities', 'woocommerce' ),
'desc_tip' => __( 'Add all cities you want to be apply this shipping region for.', 'woocommerce' ),
'id' => 'wc_shipping_zone_cities',
'type' => 'multiselect',
'desc' => __( 'Cities for this shipping region', 'woocommerce' ),
),
array(
'type' => 'sectionend',
'id' => 'shipping_zones',
),
);
}
return $settings;
}
但是挂钩过滤器函数仅在部分运行,因为我只能在该部分的顶部看到输出。
用于运输设置的 Woocommerce 类具有获取设置的方法。shipping_options
echo
显然,我在这里做错了什么,可能与不正确的过滤器或其他任何东西挂钩。请帮忙。
答:
0赞
Hamid Reza Yazdani
1/3/2023
#1
遗憾的是,此部分没有定义钩子。您可以使用以下代码添加字段。 另一种方法是删除区域设置页面并使用 WooCommerce 管理设置模板自行重建,这确实很耗时。
function ywp_add_city_field_to_shipping_zone() {
$cities = array(
'city-1' => 'city one name',
'city-2' => 'city two name',
'city-3' => 'city three name',
'city-4' => 'city four name',
);
$cities_opt = '';
foreach ( $cities as $value => $name ) {
$cities_opt .= sprintf(
'<option value="%s">%s</option>',
$value,
$name,
);
}
$field = '<br><select multiple="multiple" data-attribute="my-city" id="my-city" name="my-city" data-placeholder="Select cities from this location" class="wc-shipping-zone-region-select chosen_select">' . $cities_opt . '</select>';
?>
<script>jQuery(function ($) {
if ($('.wc-shipping-zone-postcodes-toggle').length) {
beforeEl = $('.wc-shipping-zone-postcodes-toggle')
} else {
beforeEl = $('.wc-shipping-zone-postcodes')
}
beforeEl.before('<?php echo $field;?>')
$('body').trigger('wc-enhanced-select-init')
})
</script>
<?php
}
add_action( 'woocommerce_shipping_zone_after_methods_table', 'ywp_add_city_field_to_shipping_zone' );
评论
woocommerce_shipping_zone_after_methods_table
woocommerce_before_shipping_zone_object_save
zone_custom
echo
if
echo 'this runs for shipping_options and section is ' . $current_section;