问题描述
仅当购物车中有4种或以下特定送货类别的产品时,我才尝试取消两种送货方式。
运输方式:flat_rate:20和flat_rate:21
运输班:182
这就是我所拥有的:
add_filter( 'woocommerce_package_rates','hide_shipping_method_based_on_shipping_class',10,2 );
function hide_shipping_method_based_on_shipping_class( $rates,$package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping Class To Find
$class = 182;
// Number Of Shipping Class Items In Cart
$amount = 4;
// Shipping Methods To Hide
$method_key_ids = array('flat_rate:20','flat_rate:21');
// Checking In Cart Items
foreach( $package['contents'] as $item ) {
// If We Find The Shipping Class and Number of Items
if( $item['data']->get_shipping_class_id() == $class && count($package['contents']) <= $amount ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove Targeted Methods
}
break; // Stop The Loop
}
}
return $rates;
}
我想将以上逻辑与以下逻辑结合起来:
add_filter( 'woocommerce_package_rates',$package ) {
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids = array( // Shipping Method rates Ids To Hide
'wf_shipping_ups:07','wf_shipping_ups:08','wf_shipping_ups:11','wf_shipping_ups:54','wf_shipping_ups:65','wf_shipping_ups:70','wf_shipping_ups:74','free_shipping:2','request_shipping_quote'
);
$related_total_qty = 0;
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(),$targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods
foreach( $shipping_rates_ids as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
return $rates;
}
要创建以下逻辑:
1。如果购物车的运输类别181中有4件或更少的商品,请取消设置以下运输方式:
- 'flat_rate:20'
- 'flat_rate:21'
2。如果购物车中有181个运输类别中的5个或更多产品,请取消设置以下运输方式:
- 'wf_shipping_ups:07'
- 'wf_shipping_ups:08'
- 'wf_shipping_ups:11'
- 'wf_shipping_ups:54'
- 'wf_shipping_ups:65'
- 'wf_shipping_ups:70'
- 'wf_shipping_ups:74'
- 'free_shipping:2'
- 'request_shipping_quote'
如果我单独使用它们,两种代码都可以工作。但是当我尝试同时使用两者时出现错误。
我收到以下错误消息: 无法重新声明hide_shipping_method_based_on_shipping_class()(先前在/functions.PHP:272中声明)
解决方法
您应该为每个代码段使用不同的函数名称,但是最好的方法是将所有内容合并到一个唯一的函数中。
这是使其具有独特功能的方式(适用于特定运输方式的商品):
- 购物车中有4件或更少物品时隐藏某些运输方式
- 购物车中有4件或以下物品时,请隐藏其他运输方式
代码:
add_filter( 'woocommerce_package_rates','show_hide_shipping_methods_based_on_shipping_class',10,2 );
function show_hide_shipping_methods_based_on_shipping_class( $rates,$package ) {
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids1 = array( // Shipping Method rates Ids To Hide if more than 4 items are in cart
'wf_shipping_ups:07','wf_shipping_ups:08','wf_shipping_ups:11','wf_shipping_ups:54','wf_shipping_ups:65','wf_shipping_ups:70','wf_shipping_ups:74','free_shipping:2','request_shipping_quote',);
$shipping_rates_ids2 = array( // Shipping Method rates Ids to Hide if 4 or less items are in cart
'flat_rate:20','flat_rate:20',);
$related_total_qty = 0; // Initializing
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(),$targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods (more than 4 items)
foreach( $shipping_rates_ids1 as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
} else {
// Hide related defined shipping methods (4 or less items)
foreach( $shipping_rates_ids2 as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的functions.php文件中。未经测试,它应该可以工作。
刷新发货缓存:
- 此代码已保存在您的functions.php文件中。
- 在运输区域设置中,禁用/保存任何运输方式,然后启用回退/保存。
您已完成,您可以对其进行测试。
处理项目数而不是项目累积数量:
替换:
$related_total_qty += $cart_item['quantity'];
作者
$related_total_qty++;
,
我假设您已经通过依次编写两个代码段来组合它们。
由于您两次使用相同的函数名,因此出现错误:无法重新声明.....
因此,您可以尝试通过像这样重命名第二个代码段的函数名称来修复它-
add_filter( 'woocommerce_package_rates','hide_shipping_method_based_on_shipping_class_logic_2',2 );
function hide_shipping_method_based_on_shipping_class_logic_2( $rates,$package ) {
// other stuffs
}