从运输选项中排除产品类别



我在wooccommerce有两种运输方式,统一费率标准(方法1(和统一费率特快专递(方法2(。两个产品类别,X和Z,只能使用方法1,所以如果购物车包含X或Z类别的任何商品,我想在结账时将方法2隐藏在运输选项中。

我搜索了Stackoverflow,在Wooccommerce中找到了这篇文章,为特定产品隐藏特定的运输方法,并试图应用这个答案,但没有成功。下面是我尝试的代码,我认为这些代码经过了适当的修改。

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

我发现上面的代码干扰了WooCommerce Advanced Free Shipping插件的操作。我取消激活了那个插件并删除了上面的代码。然后,我为Wooccommerce安装了Advanced Flat Rate Shipping,该插件允许我设置阻止这两个产品类别符合运输方法2的参数。

我做了两个重要的更改,更改了函数的名称以避免冲突,并更改了为特定测试删除的方法。

注意,我看到的方法都是小写字母,确保你的方法真的是第一个大写字母,这会有所不同;

请注意,我留下了一条注释,您可以使用它来正确查看id。

注意,我在取消设置之前放了一个if,以避免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 );

这在我这里的测试中运行得很好,你可能没有正确地给出名称,或者你的wooccommerce或php已经过时,所以网站消息遇到了困难或与白屏幕循环

最新更新