所以这是我的密码
function crudStatesOperations() {
global $wpdb;
$country_code = 'no';
$user_id = get_current_user_id();
$table_name = $wpdb->prefix . 'states';
if (isset($_POST['newsubmit'])) {
add_filter('woocommerce_states', function ($states) {
$states['BB'] = array(
'BB001' => 'St. George',
'BB002' => 'St. Michael',
'BB003' => 'Christ Church',
'BB004' => 'St. Philip',
'BB005' => 'St. Lucy',
'BB006' => 'St. Joseph',
'BB007' => 'St. Peter',
'BB008' => 'St. James',
'BB009' => 'St. Thomas',
'BB0010' => 'St. John',
'BB0011' => 'St. Andrew',
);
return $states;
});
}
}和add_filter(‘woocommerce_state’,'woo_custom_shipping_zones‘);它在我的函数中不起作用,但是如果我把它放在外部并重新激活它的工作
如何在函数内部使用此过滤器?
发布于 2021-04-09 19:41:00
您可以使用来解决问题:
// if the "$_POST['newsubmit']" field is set, update the "add_custom_states_woocommerce" option
function crudStatesOperations() {
global $wpdb;
$country_code = 'no';
$user_id = get_current_user_id();
$table_name = $wpdb->prefix . 'states';
if ( isset( $_POST['newsubmit'] ) ) {
update_option( 'add_custom_states_woocommerce', true );
}
}
// adds custom states based on "add_custom_states_woocommerce" option
add_filter( 'woocommerce_states', 'woo_custom_shipping_zones', 10, 1 );
function woo_custom_shipping_zones( $states ) {
if ( get_option( 'add_custom_states_woocommerce' ) ) {
$states['BB'] = array(
'BB001' => 'St. George',
'BB002' => 'St. Michael',
'BB003' => 'Christ Church',
'BB004' => 'St. Philip',
'BB005' => 'St. Lucy',
'BB006' => 'St. Joseph',
'BB007' => 'St. Peter',
'BB008' => 'St. James',
'BB009' => 'St. Thomas',
'BB0010' => 'St. John',
'BB0011' => 'St. Andrew',
);
}
return $states;
}密码应该能用。将其添加到活动主题的functions.php中。
https://stackoverflow.com/questions/67026896
复制相似问题