我正在使用Woocommerce WordPress插件
在结帐页面添加了一个自定义字段,称为 - 地址类型,下拉列表包含2个值。商业/商业和住宅
我共有3种送货方式 - 国际统一费率,统一费率,免费送货
现在,条件是我想要最终用户选择住宅然后他们将获得所有3种送货方式,但一旦用户选择商业选项,然后隐藏/删除国际统一费率和统一费率送货方式(我的意思是只显示免费送货)。
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;
}
}
这是我正在尝试的代码。让我知道我哪里错了?
提前致谢!