将自定义字段添加到运输区域表单 - Woocommerce

问题描述 投票:0回答:2

我正在尝试在创建新的送货区域时将选择字段添加到 Woocommerce Shipping 选项卡和其中的 Shipping Zones 部分。我在寻找解决方案时在 Woocommerce 官方文档中找到了this

到目前为止我尝试过的是:

// 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;
}

但挂钩过滤器功能仅针对

shipping_options
部分运行,因为我只能在该部分的顶部看到
echo
输出。
用于运输设置的 Woocommerce 类具有用于获取设置的 this 方法。

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

php woocommerce hook-woocommerce shipping-method woocommerce-theming
2个回答
0
投票

不幸的是,本节没有定义任何钩子。您可以使用以下代码来添加该字段。 另一种方法是删除区域设置页面并使用 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' );

0
投票

我认为如果你

woocommerce_get_settings_shipping
过滤它可能会起作用。您可以尝试连接到
woocommerce_shipping_zones_settings
过滤器以将字段添加到“运输区域”部分。

// Hook into the 'woocommerce_shipping_zones_settings' filter
add_filter('woocommerce_shipping_zones_settings', 'add_custom_shipping_zone_field', 10, 2);

function add_custom_shipping_zone_field($settings, $zone_id) {
    // Add your custom field here
    $settings[] = array(
        'title'    => __('Zone City', 'woocommerce'),
        'type'     => 'title',
        'id'       => 'zone_city_title',
    );

    $settings[] = array(
        'title'    => __('Zone Cities', 'woocommerce'),
        'desc_tip' => __('Add all cities you want to apply this shipping region to.', 'woocommerce'),
        'id'       => 'wc_shipping_zone_cities_' . $zone_id, // Make the field unique for each zone
        'type'     => 'multiselect',
        'class'    => 'wc-enhanced-select',
        'css'      => 'width: 450px;',
        'options'  => array(
            'city1' => 'City 1',
            'city2' => 'City 2',
            'city3' => 'City 3',
            // Add more cities as needed
        ),
    );

    $settings[] = array(
        'type'     => 'sectionend',
        'id'       => 'zone_city_title',
    );

    return $settings;
}

这个想法是添加一个自定义多选字段,用于在“运输区域”部分中为每个运输区域指定城市。您可以根据您的要求自定义字段选项。只需将此代码复制并粘贴到主题的

functions.php
文件或自定义插件中即可。希望这对您有用!

© www.soinside.com 2019 - 2024. All rights reserved.