Woocommerce 基于签出页面上的自定义签出字段筛选发货方法

Woocommerce 基于签出页面上的自定义签出字段筛选发货方法,woocommerce,Woocommerce,我正在使用Woocommerce WordPress插件 在签出页面添加了一个名为-Address-Type的自定义字段,该字段是包含2个值的下拉列表。商业/商业及住宅 我一共有3种运输方式-国际统一费率、统一费率、免费运输 现在,我想要的条件是,当最终用户选择住宅时,他们将获得所有3种配送方式,但一旦用户选择业务选项,则隐藏/删除国际统一费率和统一费率配送方式(我的意思是仅显示免费配送) 这是我正在尝试的代码。让我知道我错在哪里 提前谢谢 在您的代码中,什么不起作用?当我在结帐页面上选择“业务

我正在使用Woocommerce WordPress插件

在签出页面添加了一个名为-Address-Type的自定义字段,该字段是包含2个值的下拉列表。商业/商业及住宅

我一共有3种运输方式-国际统一费率、统一费率、免费运输

现在,我想要的条件是,当最终用户选择住宅时,他们将获得所有3种配送方式,但一旦用户选择业务选项,则隐藏/删除国际统一费率和统一费率配送方式(我的意思是仅显示免费配送)

这是我正在尝试的代码。让我知道我错在哪里


提前谢谢

在您的代码中,什么不起作用?当我在结帐页面上选择“业务/商业”地址类型时,应该隐藏“固定费率和免费配送”配送方法,以便使用ajax调用进行选择。我想用woocomerce Filters过滤发货方法在WooCommerce package rate hook中,我得到了所有默认WooCommerce字段的值。但没有在过滤器中获取自定义添加字段的值(地址类型-函数“add_address_type_field”)。在您的代码中有什么不起作用?当我在结帐页面上选择“Business/Commercial”地址类型时,应隐藏“统一费率和免费配送”配送方法,以便使用ajax调用进行选择。我想用woocomerce Filters过滤发货方法在WooCommerce package rate hook中,我得到了所有默认WooCommerce字段的值。但无法在筛选器中获取自定义添加字段的值(地址类型-函数“add_address_type_field”)。
add_filter( 'woocommerce_checkout_fields' , 'add_address_type_field' );
function add_address_type_field( $fields ) {

 $address_type_field = array(
    'type'      => 'select',
    'label'     => __('Address Type', 'woocommerce'),
    'required'  => false,
    'class'     => array('address-field', 'form-row-wide', 'validate-addresstype'),
    'clear'     => true,
    'options'   => array(
                    'residential'   => 'Residential',
                    'business'      => 'Business/Commercial'
                    ),
 );

 $fields['billing']['billing_address_type']   = $address_type_field;
 $fields['shipping']['shipping_address_type'] = $address_type_field;

 return $fields;
}
 add_filter('woocommerce_package_rates', 'display_shipping_method_based_on_state', 10, 2);
 if(!function_exists('display_shipping_method_based_on_state')) {
function display_shipping_method_based_on_state($rates) {
    global $woocommerce;
    $state = WC()->customer->shipping_state;        
    $address_type = WC()->customer->shipping_address_type;        
    $free_shipping = $rates['free_shipping'];
    if($address_type == 'business')
    {
        $rates = array();
        $rates['free_shipping'] = $free_shipping;
    }
    return $rates;
}
}