Woocommerce 根据已选择的运送 class 在结帐时更改运送方式标题

Woocommerce Change shipping method title on checkout based on shipping class selected

我想根据产品的运费 class 更改我商店结账时显示的送货方式标题。

例如

运输方式名称目前是 Flat Rate,我有 2 个产品:

遗憾的是,我必须使用 classes 进行运输,因此其他方法将不起作用。

如有任何帮助,我们将不胜感激。

我觉得这个plugin may help you and also check this one

以下代码将根据您的 "Fragile" 运费 class 重命名您的统一运费:

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

代码:

add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
function change_shipping_method_name_based_on_shipping_class($rates, $package){
    // HERE set the shipping class for "Fragile"
    $shipping_class_id = 64;
    $found = false;

    // Check for the "Fragile" shipping class in cart items
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
            $found = true;
            break;
        }
    }

    // Loop through shipping methods
    foreach ( $rates as $rate_key => $rate ) {
        // Change "Flat rate" Shipping method label name
        if ( 'flat_rate' === $rate->method_id ) {
            if( $found )
                $rates[$rate_key]->label = __( 'Fragile shipping', 'woocommerce' );
            else
                $rates[$rate_key]->label = __( 'Standard shipping', 'woocommerce' );
        }
    }
    return $rates;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

Don't forget to re-enable "Enable debug mode" option in shipping settings.