使用Local Pickup Plus插件安装Woocommerce . 必须拾取产品A并将其设置为“必须拾取” . 产品B由它自己销售,也是与产品A同步销售 . 它设置为“可以提货”并且还提供统一运费 . 如果单独购买产品B,客户可以选择提货或统一运费 . 当产品B与产品A一起购买时,两者都应该只是皮卡 . 但是,当产品A和B在购物车中时,会显示两种送货方式 - 产品A的本地取货和产品B的固定费率 .

https://i.imgur.com/DYXMvW9.png

也许我在某处找不到设置错误,但是当选择本地取件时,不应该有其他送货方式 . 我尝试了许多代码变体,有些工作有些工作,有些工作根本没有 .

这导致WSOD:

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;

}

这样可以正确删除固定费率,但您无法再选择取件地点(两种送货方式均显示为"Local pickup: $0.00",并且顶部送货方式缺少选择取件地点框.https://gist.github.com/rynaldos/9de09f00765c83868fb7fbd731c3aa1b

我也尝试过这里的代码,将local_pickup改为local_pickup_plus,但这导致拾取位置框消失 - 仍然出现Shipping 2统一费率 . Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+

我试图摆弄其他变化,但都有相同的结果 - 根本没有工作或使本地拾取位置选择框消失 .

我需要的是,如果选择了本地取件(加号),则不存在/可用的其他运输方法 . 客户应该仍然可以选择他们喜欢的取件地点 . 在这一个上剥掉我的头发并生病了,所以非常感谢!

更新:以下代码集正常工作,有点禁用flatrate运输,但仍允许本地提取,包括选择提货地点 .

/**
 * 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. . 如果对购物车中的所有物品进行取件,则不应出现2号货物 .