也许我在某个地方设置错误而找不到,但是当选择了本地取货时,就没有其他的运输方式了。我已经尝试过许多代码变体,有些可以工作,有些则完全不起作用。
add_filter( 'woocommerce_package_rates', 'unset_wc_shipping_methods_when_cat', 10 ,2 );
function unset_wc_shipping_methods_when_cat ( $rates, $package ) {
$product_category = 'birth-pool-hire';
// loop through the cart
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
$local_pickup = $rates['local_pickup_plus'];
$rates = array();
$rates['local_pickup_plus'] = $local_pickup_plus;
}
return $rates;
}
这将正确删除固定费率,但您不能再选择提货地点(两种装运方式均显示为“本地提货:$0.00”,并且顶部装运方式缺少“选择提货地点”框。
https://gist.github.com/rynaldos/9de09f00765c83868fb7fbd731c3aa1b
Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+
我试过摆弄其他的变化,但都有相同的结果-根本不起作用或使本地拾取位置选择框消失。
我所需要的是,如果选择了本地提货(plus),则不应存在/提供其他装运方式。客户仍然可以选择他们喜欢的取件地点。
更新:下面的to代码集工作正常,可以在一定程度上禁用统一费率装运,但仍然允许本地提货,包括选择提货地点。
/**
* Hide shipping rates when local pickup is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function wc_hide_shipping_when_pickup_is_available( $rates ) {
$pickup = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'local_pickup_plus' === $rate->method_id ) {
$pickup[ $rate_id ] = $rate;
break;
}
}
return ! empty( $pickup ) ? $pickup : $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_hide_shipping_when_pickup_is_available', 100 );
https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 602;
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:1';
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
改自:
Hide shipping method for specific shipping classes in woocommerce
不过,我有点说,因为船运2仍然存在。相反,我得到
There are no shipping methods available. Please ensure that your address has been entered correctly, or contact us if you need any help.
如果输入地址或
Enter your full address to see shipping costs.