在 WooCommerce 中隐藏某些运输 类 的运输方式

Hide shipping methods for some shipping classes in WooCommerce

基本上,当购物车中只有特定的运费 classes(包括 ID 40 到 46)时,我试图隐藏统一运费送货方式“flat_rate:8”。但是,当购物车中有运费 classes 47、48、49 的任何产品时,我想隐藏除此“flat_rate:8”之外的所有其他送货方式。 所有详细信息如下。

我有 9 种送货方式 class(从 ID 40 到 46):XXS、XS、S、... L、XL、XXL、XXXL 和 5 种送货方式(flate_rate3, local_pickup, flat_rate6, flat_rate7, flat_rate8).

在我的购物车中我有:

我只想提供 2 种送货方式(local_pickup 和 flate_rate8)。基本上,如果购物车中有 >= XL,我只希望出现这两种方法。 我能够使用这段代码来完成这 3 段代码:

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;

// HERE define your shipping class to find. 47 = shipping class XL
$class = 47;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}


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;

// HERE define your shipping class to find. 48 = shipping class XXL
$class = 48;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}

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;

// HERE define your shipping class to find. 49 = shipping class XXXL
$class = 49;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');

// Checking in cart items
foreach( $package['contents'] as $item ) {
    // If we find the shipping class
    if( $item['data']->get_shipping_class_id() == $class ){
        foreach( $method_key_ids as $method_key_id ){
            unset($rates[$method_key_id]); // Remove the targeted methods
        }
        break; // Stop the loop
    }
}
return $rates;
}

完成任务,现在只显示 2 种送货方式(local_pickup 和 flat_rate8)。

那么,我要做的是:

在我的购物车中我有:

我想删除 flat_rate8 并保留所有其他送货方式。 使用当前代码和设置,现在,当我的购物车中有 =< L 产品时,我拥有所有可用的方法。

我一直在尝试复制上面显示的代码来隐藏这个 flat_rate8 只有“小”产品在购物车中,但显然它不会工作,因为当我有一个混合购物车时(例如 XXXL 和 S)它将从选项中删除 flat_rate8。

我试过了,除了上面显示的那段代码外,还添加了这个:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_classd', 10, 2 );
function hide_shipping_method_based_on_shipping_classd( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

        $product_id = $package_item['product_id']; // Grab product_id
        $_product   = wc_get_product( $product_id ); // Get product info using that id

        if( $_product->get_shipping_class_id() != 47 ){ // If we DON'T find this shipping class ID (XL)
            unset($rates['flat_rate:8']); // Then remove this shipping method
            break; // Stop the loop, since we've already removed the shipping method from this package
        }
    }
    return $rates;
}

但是没用:

我一直在寻找许多主题,其中包括:

但我找不到解决问题的方法。

尝试以下代码,其中所有内容都合并到一个独特的挂钩函数中:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{   
    // HERE define your shipping class to find
    $classes_group1 = array(47, 48, 49);
    $classes_group2 = array(40, 41, 43, 44, 45, 46);
    
    // HERE define the shipping methods you want to hide
    $method_key_ids1 = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');
    $method_key_ids2 = array('flat_rate:8');
    
    $found_group1 = $found_group2 = false; // Initializing
    
    // Checking in cart items
    foreach( $package['contents'] as $item ) {
        $shipping_class = $item['data']->get_shipping_class_id();
        
        // Shipping Classes group 1
        if( in_array( $shipping_class, $classes_group1 ) ){
            foreach( $method_key_ids1 as $method_key_id ){
                // Remove the targeted methods 1
                unset($rates[$method_key_id]); 
                $found_group1 = true; // Flag
            }
        }
        // Shipping Classes group 2
        if( in_array( $shipping_class, $classes_group2 ) ){
            foreach( $method_key_ids2 as $method_key_id ){
                $found_group2 = true; // Flag
            }
        }
        
        // If Shipping Classes group 2 alone in cart
        if( ! $found_group1 && $found_group2 ){
            foreach( $method_key_ids2 as $method_key_id ){
                // Remove the targeted methods 2
                unset($rates[$method_key_id]);
            }
        }
    }
    return $rates;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。

Refresh shipping methods:

  • Empty the cart.
  • If needed, go to shipping areas settings, then disable / save and re-enable / save any shipping methods from a shipping zone.