使用woocommerce,我设法将状态更改为爱尔兰的县,包括function.php中的以下代码
function wc_ie_counties_add_counties( $states ) {
$states['IE'] = array(
'Carlow' => 'Carlow',
'Cavan' => 'Cavan',
'Clare' => 'Clare',
'Cork' => 'Cork',
'Donegal' => 'Donegal',
'Dublin' => 'Dublin',
'Galway' => 'Galway',
'Kerry' => 'Kerry',
'Kildare' => 'Kildare',
'Kilkenny' => 'Kilkenny',
'Laois' => 'Laois',
'Leitrim' => 'Leitrim',
'Limerick' => 'Limerick',
'Longford' => 'Longford',
'Louth' => 'Louth',
'Mayo' => 'Mayo',
'Meath' => 'Meath',
'Monaghan' => 'Monaghan',
'Offaly' => 'Offaly',
'Roscommon' => 'Roscommon',
'Sligo' => 'Sligo',
'Tipperary' => 'Tipperary',
'Waterford' => 'Waterford',
'Westmeath' => 'Westmeath',
'Wexford' => 'Wexford',
'Wicklow' => 'Wicklow',
);
return $states;
}
add_filter( 'woocommerce_states', 'wc_ie_counties_add_counties' );
但如果特定产品ID在购物车中,我想删除几个县。
示例:如果产品ID 581和/或590在购物车中,则仅显示Dublin,Cavan和Carlow状态。
谢谢,
答案 0 :(得分:1)
使用您的实际代码,您需要设置2个状态数组,一个受限制,一个完成。我们还需要检查产品ID 581和/或590的购物车项目。如果其中一个产品在购物车中,我们设置受限制的状态数组,如果不是,我们设置完整的状态数组。
代码:
add_filter( 'woocommerce_states', 'wc_ie_counties_add_counties' );
function wc_ie_counties_add_counties( $states ) {
// HERE your product IDS
$products_ids = array( 581, 590 );
$cart = WC()->cart; // The Cart object
$found = false;
$states_partial = array(
'Carlow' => 'Carlow',
'Cavan' => 'Cavan',
'Dublin' => 'Dublin'
);
$states_complete = array(
'Carlow' => 'Carlow',
'Cavan' => 'Cavan',
'Clare' => 'Clare',
'Cork' => 'Cork',
'Donegal' => 'Donegal',
'Dublin' => 'Dublin',
'Galway' => 'Galway',
'Kerry' => 'Kerry',
'Kildare' => 'Kildare',
'Kilkenny' => 'Kilkenny',
'Laois' => 'Laois',
'Leitrim' => 'Leitrim',
'Limerick' => 'Limerick',
'Longford' => 'Longford',
'Louth' => 'Louth',
'Mayo' => 'Mayo',
'Meath' => 'Meath',
'Monaghan' => 'Monaghan',
'Offaly' => 'Offaly',
'Roscommon' => 'Roscommon',
'Sligo' => 'Sligo',
'Tipperary' => 'Tipperary',
'Waterford' => 'Waterford',
'Westmeath' => 'Westmeath',
'Wexford' => 'Wexford',
'Wicklow' => 'Wicklow'
);
// Checking cart items
foreach( $cart->get_cart() as $cart_item ) {
if( in_array( $cart_item['product_id'], $products_ids ) ){
$found = true;
break;
}
}
$states['IE'] = $found ? $states_partial : $states_complete;
return $states;
}
代码进入活动子主题(或活动主题)的function.php文件。
它应该有用......
与产品类别相同而不是产品ID(基于作者的代码):
add_action('woocommerce_states', 'my_check_category_in_cart');
function my_check_category_in_cart( $states ) {
// HERE your product categories
$products_categories = array( 'baths','curved-radiators','cabinets','mirrors','sinks',
'storage-units','toilets','vanity-units','column-radiators','curved-radiators',
'designer-radiators','flat-panel-radiators','heated-towel-radiators');
$is_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $products_categories, 'product_cat', $cart_item['product_id'] ) ) {
$is_in_cart = true;
break;
}
}
if ( $is_in_cart ) {
$states['IE'] = array(
'Carlow' => 'Carlow',
'Dublin' => 'Dublin',
'Kildare' => 'Kildare',
'Kilkenny' => 'Kilkenny',
'Laois' => 'Laois',
'Longford' => 'Longford',
'Louth' => 'Louth',
'Meath' => 'Meath',
'Offaly' => 'Offaly',
'Westmeath' => 'Westmeath',
'Wexford' => 'Wexford',
'Wicklow' => 'Wicklow'
);
// Display a custom notice
if( is_checkout() && WC()->customer->get_billing_country() == 'IE' )
wc_add_notice( 'One of the products below can only be delivered inside Leinster area', 'notice' );
} else {
$states['IE'] = array(
'Carlow' => 'Carlow',
'Cavan' => 'Cavan',
'Clare' => 'Clare',
'Cork' => 'Cork',
'Donegal' => 'Donegal',
'Dublin' => 'Dublin',
'Galway' => 'Galway',
'Kerry' => 'Kerry',
'Kildare' => 'Kildare',
'Kilkenny' => 'Kilkenny',
'Laois' => 'Laois',
'Leitrim' => 'Leitrim',
'Limerick' => 'Limerick',
'Longford' => 'Longford',
'Louth' => 'Louth',
'Mayo' => 'Mayo',
'Meath' => 'Meath',
'Monaghan' => 'Monaghan',
'Offaly' => 'Offaly',
'Roscommon' => 'Roscommon',
'Sligo' => 'Sligo',
'Tipperary' => 'Tipperary',
'Waterford' => 'Waterford',
'Westmeath' => 'Westmeath',
'Wexford' => 'Wexford',
'Wicklow' => 'Wicklow'
);
}
return $states;
}
代码进入活动子主题(或活动主题)的function.php文件。
现在它应该用于产品变体(在可变产品上) 。
答案 1 :(得分:0)
我使用了你的代码LoicTheAztec,它运行得很完美,但后来我意识到我需要花费很多时间来添加所有产品并在我加入新产品时更新它。
所以使用你的代码和我找到的其他一些代码。我想出了一个基于类别的东西。
minikube start --insecure-registry="10.0.4.0/24"
我不知道这是否是正确的方法,但它也有效。
非常感谢