将自定义字段添加到发货区域表单 - Woocommerce

Add custom field to shipping zone form - Woocommerce

提问人:rmalviya 提问时间:4/11/2018 最后编辑:Ruveermalviya 更新时间:6/22/2023 访问量:1850

问:

在创建新的运输区域时,我正在尝试将选择字段添加到 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_optionsecho

显然,我在这里做错了什么,可能与不正确的过滤器或其他任何东西挂钩。请帮忙。

php 钩子- 发货方式 woocommerce主题

评论

0赞 Saran 10/5/2020
你找到什么解决方案了吗?
1赞 bhanu 6/5/2021
虽然我无法找到你问题的完整答案,但我已经非常接近了。根据这个链接:github.com/woocommerce/woocommerce/issues/21964#issue-381983150。建议使用,.我能够将字段添加到表单中。并用于保存值,这对我来说是障碍。以这样一种方式添加字段以使其开始回发是很棘手的,因为这是一个 ajax 请求。我尝试了很多,但无论我做什么,我创建的字段都没有被发布。woocommerce_shipping_zone_after_methods_tablewoocommerce_before_shipping_zone_object_savezone_custom
0赞 Stephan Samuel 2/6/2022
如果您只看到 ,则表示您的计算结果未为 true。试着看看你从中得到了什么价值。它可能不是字符串,也可能不包含您认为它应该包含的值。echoifecho 'this runs for shipping_options and section is ' . $current_section;

答:

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' );