简体   繁体   English

仅在 Woocommerce 中隐藏特定运输类别的免费送货

[英]Hide free shipping for specific shipping classes exclusively in Woocommerce

We have a large variety of products in WooCommerce, some of which are not our own and supplied.我们在 WooCommerce 中有多种产品,其中一些不是我们自己的和提供的。 I am trying to set up a functionality where free shipping is disabled if the products in the cart belongs to "Extra" shipping class exclusively .我正在尝试设置一个功能,如果购物车中的产品属于“额外”运输class ,则禁用免费送货。

If there are any other products with it that are not in the shipping class 'Extra', free shipping would still apply.如果有任何其他产品不在运输 class 'Extra' 中,仍然适用免费送货。 So if 5 products are in cart all with shipping class 'Extra' and no other products, there is a $5 charge.因此,如果购物车中有 5 件产品都包含运费 class 'Extra' 而没有其他产品,则收取 5 美元的费用。 If there is any other product that is not in that shipping class, free shipping applies again.如果有任何其他产品不在该运输 class 中,则再次适用免费送货。

I've scoured the internet for solutions and this is what I got so far:我已经在互联网上搜索了解决方案,这就是我到目前为止得到的:

function hide_shipping_methods( $available_shipping_methods, $package ) {
    $shipping_classes = array( 'Extra', 'some-shipping-class-2' );
    $excluded_methods = array( 'free_shipping' );
    $found = $others = false;
    $shipping_class_exists = false;
    
    foreach( $package['contents'] as $key => $value )
        if ( in_array( $value['data']->get_shipping_class(), $shipping_classes ) ) {
            $shipping_class_exists = true;
            break;
        }else {
            $others = true; // NOT the shipping class
            break;
        }
    if ( $shipping_class_exists && !others) {
        $methods_to_exclude = array();
        foreach( $available_shipping_methods as $method => $method_obj )
            if ( in_array( $method_obj->method_id, $excluded_methods ) )
                $methods_to_exclude[] = $method;
        if ( $methods_to_exclude )
            foreach ( $methods_to_exclude as $method )
                unset( $available_shipping_methods[$method] );
    }
    return $available_shipping_methods;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_methods', 10, 2 );

However it doesn't seem to be working.但是,它似乎不起作用。 I have products already in the shipping class Extra , however whenever I add them to cart there is still free shipping.我已经在运送 class Extra中的产品,但是每当我将它们添加到购物车时,仍然可以免费送货。

There are some mistakes and your code can be simplified.有一些错误,您的代码可以简化。 To disable free shipping for specific shipping classes exclusively , try the following revisited code:专门禁用特定运输类别的免费送货,请尝试以下重新访问的代码:

add_filter( 'woocommerce_package_rates', 'hide_free_shipping_conditionally', 10, 2 );
function hide_free_shipping_conditionally( $rates, $package ) {
    // Define the targeted shipping classes slugs (not names)
    $targeted_classes = array( 'extra' ); 
    
    $found = $others = false; // Initializing
    
    // Loop through cart items for current shipping package
    foreach( $package['contents'] as $item ) {
        if ( in_array( $item['data']->get_shipping_class(), $targeted_classes ) ) {
            $found = true;
        } else {
            $others = true;
        }
    }
    
    // When there are only items from specific shipping classes exclusively
    if ( $found && ! $others ) {
        // Loop through shipping methods for current shipping package
        foreach( $rates as $rate_key => $rate ) {
            // Targetting Free shipping methods
            if ( 'free_shipping' === $rate->method_id ) {
                unset($rates[$rate_key]); // Remove free shipping option(s)
            } 
        }
    }
    return $rates;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work.它应该工作。

Doin't forget to empty your cart to refresh shipping cached data.不要忘记清空您的购物车以刷新运输缓存数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM