我在woocommerce中有两种运输方式,固定费用标准(方法1)和固定费用特快专递(方法2)。两种产品类别X和Z只能使用方法1,因此,如果购物车中包含类别X或Z中的任何物品,我想在结帐时从运送选择中隐藏方法2。
我搜索了Stackoverflow,发现了这篇帖子Hide specific shipping method for specific products in Woocommerce,并尝试套用了答案,但没有成功。我尝试过的代码以及我认为适当的修改如下。
function specific_products_shipping_methods( $rates, $package ) {
$product_ids = array( 39 ); // HERE set the product IDs in the array
$method_id = 'Express_Post:5'; // HERE set the shipping method ID
$found = false;
// Loop through cart items Checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) ){
$found = true;
break;
}
}
if ( $found )
unset( $rates[$method_id] );
return $rates;
}
运输方式2的ID为5,产品类别X的ID为39。我禁用,保存,启用和保存了运输方法2。将上述内容作为代码段添加到php文件时,它破坏了网站(“您的网站遇到技术难题”),或者在输入运输地址后,我将被卡住(飞轮),因此无法到达购物车检查代码是否有效。
[更新]
非常感谢@Kelvin Mariano的回复。为了避免出错,我多次尝试了几种不同的代码。我不再遇到任何错误,但是即使购物车中有不合格的物品,两种运输方式仍然会出现。
相关运送方法的回显输出为:
[flat_rate:5] => WC_Shipping_Rate Object
(
[data:protected] => Array
(`
[id] => flat_rate:5
[method_id] => flat_rate
[instance_id] => 5
[label] => Express Post
我尝试的代码是这样的:
function bz_specific_products_shipping_methods( $rates, $package ) {
$product_ids = array( 39 ); // HERE set the product IDs in the array
//$method_id = 'flat_rate:5'; // HERE set the shipping method ID
$method_id = 'flat_rate:5'; // HERE set the shipping method ID
$found = false;
/*
//please remove this comment to view all shipping methods and their information
`echo "<pre>";
print_r( $rates );
echo "</pre>";
exit();*/`
// Loop through cart items Checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) ){
$found = true;
break;
}
}
if ( $found ){
if( isset( $rates[$method_id] ) ){
unset( $rates[$method_id] );
}
}
return $rates;
}
//use the filter this is important
add_filter( 'woocommerce_package_rates','bz_specific_products_shipping_methods', 10, 2 );
更新2
Array
(
[flat_rate:1] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => flat_rate:1
[method_id] => flat_rate
[instance_id] => 1
[label] => Flat rate Standard
[cost] => 9.00
)
)
[flat_rate:5] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => flat_rate:5
[method_id] => flat_rate
[instance_id] => 5
[label] => Express Post
[cost] => 11.77
答案 0 :(得分:1)
我发现上述代码干扰了WooCommerce Advanced Free Shipping插件的运行。我停用了该插件并删除了上面的代码。然后,我安装了适用于Woocommerce的Advanced Flat Rate Shipping,该插件允许我设置参数,以防止这两个产品类别适用于运输方法2。
答案 1 :(得分:0)
我做了两个重要的更改,更改了函数的名称以避免冲突,并更改了要为特定测试删除的方法。
请注意,我所看到的方法带有小写字母,请确保您的方法确实是第一个大写字母,这将有所作为;
请注意,我在评论中留下了可用于正确查看ID的信息。
请注意,我将if设置为unset以避免php错误
function bz_specific_products_shipping_methods( $rates, $package ) {
$product_ids = array( 149 ); // HERE set the product IDs in the array
//$method_id = 'Express_Post:5'; // HERE set the shipping method ID
$method_id = 'free_shipping:2'; // HERE set the shipping method ID
$found = false;
/*
//please remove this comment to view all shipping methods and their information
echo "<pre>";
print_r( $rates );
echo "</pre>";
exit();*/
// Loop through cart items Checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( in_array( $cart_item['product_id'], $product_ids ) ){
$found = true;
break;
}
}
if ( $found ){
if( isset( $rates[$method_id] ) ){
unset( $rates[$method_id] );
}
}
return $rates;
}
//use the filter this is important
add_filter( 'woocommerce_package_rates', 'bz_specific_products_shipping_methods', 10, 2 );
这在我这里的测试中非常有效,您可能没有正确输入名称,或者您的woocommerce或php已过时,因此网站消息出现问题或出现白屏循环