如果特定产品在 Woocommerce 的购物车中,则禁用某些状态 [英] Disable some states if specific products are in cart in Woocommerce
问题描述
使用woocommerce,我设法将州更改为爱尔兰这里的县,包括function.php中的以下代码
Using woocommerce, I managed to change the states to the counties here in Ireland, including the following code in the 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,我想删除几个县.
But I would like to remove a few counties if a specific product id is in the cart.
示例:如果产品 ID 581 和/或 590 在购物车中,则仅显示都柏林、卡文和卡洛州.
Example: If product id 581 and/or 590 is/are in the cart, display only Dublin, Cavan and Carlow states.
谢谢,
推荐答案
使用您的实际代码,您需要设置 2 个状态数组,一个是受限的,一个是已完成的.我们还需要检查产品 ID 581 和/或 590 的购物车项目.如果此产品之一在购物车中,我们设置受限状态数组,否则我们设置完整状态数组.
Using your actual code you need to set 2 arrays of states, one restricted and one completed. We will also need to check in cart items for product IDs 581 and/or 590. If one of this products is in cart we set the restricted array of states, if not we set the complete array of states.
代码:
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 文件中.
它应该有效......
It should works…
与产品类别相同,而不是产品 ID(基于作者的代码):
The same with Product Categories instead of Product IDs (based on author's code):
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 文件中.
现在它应该适用于产品变体(在可变产品上).
Now it should works for product variations (on variable products) too.
这篇关于如果特定产品在 Woocommerce 的购物车中,则禁用某些状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!