Hide some shipping rates based on taxonomy terms in WooCommerce 4.8+

旧巷老猫 提交于 2021-01-28 18:26:10

问题


I use this to unset shipping for product with specific tag:

function specific_products_shipping_methods( $rates, $package ){
    // etiquette cocolis
    $terms = array( 'cocolis' );
    $taxonomy = 'product_tag';

    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) )
        {
            unset( $rates['oik_weight_zone_shipping_49'] );
            unset( $rates['chrono10'] );
            unset( $rates['chrono13'] );
            add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );

function reset_default_shipping_method( $method, $available_methods ) {
    $default_method = 'per_product';
    if( array_key_exists($method, $available_methods ) )
        return $default_method;
    else
        return $method;
}

This works fine until update woo 4.8.0 Any idea what’s wrong ?

Thx,


回答1:


There are some mistakes in your code, try the following revisited and optimized code instead:

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ){

    $terms     = array('cocolis'); // Array of term slugs, names or ids
    $taxonomy  = 'product_tag'; // WooCommerce product tag taxonomy
    $rate_keys = array('oik_weight_zone_shipping_49', 'chrono10', 'chrono13'); // Shipping rates to be hidden
    $has_unset = false; // Initializing

    // Loop through cart items for the current shipping package
    foreach( $package['contents'] as $cart_item ) {
        // Targeting specific product tags
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ) {
            // Loop through shipping rates to be removed (hidden)
            foreach( $rate_keys as $rate_key ) {
                if (isset($rates[$rate_key]) ) {
                    unset($rates[$rate_key]);
                    $has_unset = true; // Flag as unset to enable the filter below
                }
            }
        }
    }
    if ( $has_unset ) {
        add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 3 );
    }
    return $rates;
}

function reset_default_shipping_method( $default, $rates, $chosen_method ) {
    $targeted_method = 'per_product'; // The shipping method rate to set as chosen one

    if( isset($rates[$targeted_method]) ) {
        $default = $targeted_method;
    }
    return $default;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works in WooCommerce 4.8+.

Don't forget to empty your cart to refresh WooCommerce shipping caches

Note: For WooCommerce categories, define the $taxonomy variable to product_cat instead.



来源:https://stackoverflow.com/questions/65576379/hide-some-shipping-rates-based-on-taxonomy-terms-in-woocommerce-4-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!